androidstudioを使用してアプリを作成しています
drawrectを使用してタップした位置に四角形を描画したいのですがサイトや本をみてもよくわかりません
分かりやすい解説やサイトなどがあると教えていただけると助かります
1月5日20:30分追記
DrawingView
1import android.content.Context; 2import android.content.ReceiverCallNotAllowedException; 3import android.graphics.Canvas; 4import android.graphics.Color; 5import android.graphics.Paint; 6import android.graphics.Path; 7import android.graphics.Rect; 8import android.util.AttributeSet; 9import android.view.MotionEvent; 10import android.view.View; 11import java.util.ArrayList; 12import java.util.List; 13 14 15public class DrawingView extends View { 16 int touchX; //タッチした際のX 17 int touchY; //タッチした際のY 18 19 // 履歴 20 private List<DrawLine> lines; 21 // 現在、描いている線の情報 22 private Paint paint; 23 private Path path; 24 private Rect rect; 25 private Paint paint1; 26 27 // 線の履歴(座標+色) 28 class DrawLine { 29 private Paint paint; 30 private Path path; 31 private Rect rect; 32 private Paint paint1; 33 34 35 DrawLine(Path path, Paint paint, Rect rect) { 36 this.paint = new Paint(paint); 37 this.path = new Path(path); 38 this.rect = new Rect(rect); 39 //this.paint1 = new Paint(paint); 40 } 41 42 43 void draw(Canvas canvas) { 44 canvas.drawPath(this.path, this.paint); 45 } 46 } 47 48 public DrawingView(Context context) { 49 super(context); 50 } 51 52 53 54 public DrawingView(Context context, AttributeSet attrs) { 55 super(context, attrs); 56 57 this.path = new Path(); 58 59 this.paint = new Paint(); 60 61 this.rect = new Rect(); 62 63 64 65 this.paint.setStyle(Paint.Style.STROKE); 66 // this.paint.setStrokeJoin(Paint.Join.MITER);//これがあると角が丸くなる 67 this.paint.setStrokeCap(Paint.Cap.SQUARE); 68 this.paint.setAntiAlias(true); 69 this.paint.setStrokeWidth(30); 70 71 72 this.lines = new ArrayList<DrawLine>(); 73 74 75 } 76 77 @Override 78 protected void onDraw(Canvas canvas) { 79 super.onDraw(canvas); 80 81 82 // キャンバスをクリア 83 canvas.drawColor(Color.WHITE); 84 // 履歴から線を描画 85 for (DrawLine line : this.lines) { 86 line.draw(canvas); 87 } 88 // 現在、描いている線を描画 89 canvas.drawPath(this.path, this.paint); 90 91 Paint paint1 = new Paint(Color.WHITE); 92 paint1.setColor(Color.BLACK); 93 paint1.setStyle(Paint.Style.STROKE);//ペン先に合わせて色が一緒に代わってしまうので保留 94 95 for (int i = 0; i < 101; i++) { 96 for (int j = 0; j < 101; j++) { 97 int a = 30; 98 Rect rect = new Rect(a * i, a * j, a * (i + j) - 1, a * (i + j) - 1); 99 100 canvas.drawRect(rect, paint1); 101 102 } 103 } 104} 105 106 107 108 @Override 109 public boolean onTouchEvent(MotionEvent event) { 110 float x = event.getX(); 111 float y = event.getY(); 112 113 switch (event.getAction()) { 114 case MotionEvent.ACTION_DOWN: 115 this.path.moveTo(x, y); 116 this.path.lineTo(x, y); 117 break; 118 case MotionEvent.ACTION_MOVE: 119 this.path.lineTo(x, y); 120 break; 121 case MotionEvent.ACTION_UP: 122 this.path.lineTo(x, y); 123 // 指を離したので、履歴に追加する 124 this.lines.add(new DrawLine(this.path, this.paint, this.rect)); 125 // パスをリセットする 126 // これを忘れると、全ての線の色が変わってしまう 127 this.path.reset(); 128 break; 129 130 } 131 invalidate(); 132 return true; 133 } 134 135 public void delete() { 136 // 履歴をクリア 137 this.lines.clear(); 138 // 現在の線をクリア 139 this.path.reset(); 140 invalidate(); 141 } 142 143 public void setPen(int color){ 144 this.paint.setColor(color); 145 } 146 147 public void setStrokeWidth(float width){ 148 this.paint.setStrokeWidth(width); 149 150 } 151} 152
こちら該当箇所にコードを埋め込んで確認してみましたが、線を引く処理が優先されているのか、アプリケーションエラーでエミュレータが起動しなくなってしまいました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/04 15:27
2019/01/05 03:47
2019/01/05 10:55
2019/01/05 12:04
2019/01/05 12:10
2019/01/05 12:41
2019/01/07 12:41
2019/01/07 14:17
2019/01/07 23:33
2019/01/07 23:33
2019/01/08 09:54
2019/01/08 09:55