PCスペック
Windows7 64bit
メモリ 8GB
プロセッサ Intel Core i7-3610
デモ用端末(XPERIA OS:7.0 API 24)
Android Studio 3.4
Android Studioにて、今後、撮影した写真の必要な部分だけなぞって保存するアプリを開発するために、OnTouchListenerについて詳しく調べています。
そこで、お絵かきアプリなら「描画」を「なぞる」ことによって実現させていると思うので、以下のサイトを参考に同じアプリを実現したいと思いました。
https://qiita.com/InoueDaiki/items/3b51aaabdaad1c2227a3
ですが、上記のサイトの方は、ソースコードを全て載せているのですが、断片的でございまして、(Javaなら特にメソッドの書く順番なんか関係ない?)それっぽくソースコードを書いた結果、
アプリを実行して画面を指でなぞってみても線が描けません。
なぞっているのに、画面が飯能しないことについて、どんな原因が考えられるでしょうか・・??
↓MainActivity.java
java
1package com.example.paintview; 2 3import android.os.Bundle; 4import android.support.design.widget.FloatingActionButton; 5import android.support.design.widget.Snackbar; 6import android.support.v7.app.AppCompatActivity; 7import android.support.v7.widget.Toolbar; 8import android.view.View; 9import android.view.Menu; 10import android.view.MenuItem; 11 12public class MainActivity extends AppCompatActivity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 19 20 Toolbar toolbar = findViewById(R.id.toolbar); 21 setSupportActionBar(toolbar); 22 23 FloatingActionButton fab = findViewById(R.id.fab); 24 fab.setOnClickListener(new View.OnClickListener() { 25 @Override 26 public void onClick(View view) { 27 //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show(); 28 PaintView paintView = (PaintView)findViewById(R.id.view); 29 paintView.clear(); 30 } 31 }); 32 } 33 34 @Override 35 public boolean onCreateOptionsMenu(Menu menu) { 36 // Inflate the menu; this adds items to the action bar if it is present. 37 getMenuInflater().inflate(R.menu.menu_main, menu); 38 return true; 39 } 40 41 @Override 42 public boolean onOptionsItemSelected(MenuItem item) { 43 // Handle action bar item clicks here. The action bar will 44 // automatically handle clicks on the Home/Up button, so long 45 // as you specify a parent activity in AndroidManifest.xml. 46 int id = item.getItemId(); 47 48 //noinspection SimplifiableIfStatement 49 if (id == R.id.action_settings) { 50 return true; 51 } 52 53 return super.onOptionsItemSelected(item); 54 } 55} 56
↓PaintView.java
java
1 2package com.example.paintview; 3 4import android.content.Context; 5import android.graphics.Canvas; 6import android.graphics.Paint; 7import android.graphics.Path; 8import android.util.AttributeSet; 9import android.view.MotionEvent; 10import android.view.View; 11 12public class PaintView extends View { 13 14 private Paint paint; 15 private Path path; 16 17 18 19 public PaintView(Context context) { 20 this(context, null); 21 22 } 23 24 public PaintView(Context context, AttributeSet attrs) { 25 26 super(context, attrs); 27 path = new Path(); 28 paint = new Paint(); 29 paint.setColor(0xFF000000); 30 paint.setStyle(Paint.Style.STROKE); 31 paint.setStrokeJoin(Paint.Join.ROUND); 32 paint.setStrokeCap(Paint.Cap.ROUND); 33 paint.setStrokeWidth(10); 34 } 35 36 @Override 37 protected void onDraw(Canvas canvas) { 38 canvas.drawPath(path, paint); 39 } 40 41 @Override 42 public boolean onTouchEvent(MotionEvent event) { 43 float x = event.getX(); 44 float y = event.getY(); 45 46 switch (event.getAction()) { 47 case MotionEvent.ACTION_DOWN: 48 path.moveTo(x, y); 49 invalidate(); 50 break; 51 case MotionEvent.ACTION_MOVE: 52 path.lineTo(x, y); 53 invalidate(); 54 break; 55 case MotionEvent.ACTION_UP: 56 path.lineTo(x, y); 57 invalidate(); 58 break; 59 } 60 return true; 61 } 62 63 public void clear(){ 64 path.reset(); 65 invalidate(); 66 } 67 68}
↓activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <android.support.design.widget.AppBarLayout 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:theme="@style/AppTheme.AppBarOverlay"> 13 14 <android.support.v7.widget.Toolbar 15 android:id="@+id/toolbar" 16 android:layout_width="match_parent" 17 android:layout_height="?attr/actionBarSize" 18 android:background="?attr/colorPrimary" 19 app:popupTheme="@style/AppTheme.PopupOverlay" /> 20 21 </android.support.design.widget.AppBarLayout> 22 23 <include layout="@layout/content_main" /> 24 25 <android.support.design.widget.FloatingActionButton 26 android:id="@+id/fab" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:layout_gravity="bottom|end" 30 android:layout_margin="@dimen/fab_margin" 31 app:srcCompat="@android:drawable/ic_dialog_email" /> 32 33 34 35</android.support.design.widget.CoordinatorLayout>
↓content_main/xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 app:layout_behavior="@string/appbar_scrolling_view_behavior" 8 tools:context=".MainActivity" 9 tools:showIn="@layout/activity_main"> 10 11 12 <view 13 class="com.example.paintview.PaintView" 14 id="@+id/view" 15 android:layout_width="match_parent" 16 android:layout_height="match_parent" 17 tools:layout_editor_absoluteX="45dp" 18 tools:layout_editor_absoluteY="421dp" /> 19</android.support.constraint.ConstraintLayout>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。