package org.ntlab.irisclient; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import androidx.annotation.Nullable; import java.util.ArrayList; public class DrawingCanvas extends View { private Canvas canvas; private Bitmap bitmap; private Path path; private Paint paint; private int curw; private int curh; private int curoldw; private int curoldh; public DrawingCanvas(Context context, @Nullable AttributeSet attrs) { super(context, attrs); //1)コンストラクタ(≒必需品) path = new Path();//線を引いたり、図形を描いたり、要するにグラフィック paint = new Paint();//筆の種類 paint.setColor(Color.BLACK);//色の指定 paint.setStyle(Paint.Style.STROKE);//線をひく paint.setStrokeWidth(20);//幅 } //2)onDraw(描画の準備/プロペラが回りだした状態) @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(bitmap, 0, 0, null); canvas.drawPath(path,paint); } @Override protected void onSizeChanged( int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); curw = w; curh = h; curoldw = oldw; curoldh = oldh; bitmap = Bitmap.createBitmap( w, h, Bitmap.Config.ARGB_8888); canvas = new Canvas(bitmap); } //3)実際の操縦 (条件分岐:押したとき、動かしたとき、放した時) @Override public boolean onTouchEvent(MotionEvent event) { //(3-1)座標を取得(x座標、y座標) float x = event.getX(); float y = event.getY(); //(3-2)タッチの処理 switch (event.getAction()){ case MotionEvent.ACTION_DOWN://タッチした瞬間 path.reset(); path.moveTo(x,y); invalidate(); break; case MotionEvent.ACTION_MOVE://動かしている間 path.lineTo(x,y); invalidate(); break; case MotionEvent.ACTION_UP://手を離す path.lineTo(x, y); canvas.drawPath(path, paint); path.reset(); invalidate(); break; } //return super.onTouchEvent(event); return true; } //4)クリア処理 public void clearCanvas(){ onSizeChanged(curw, curh, curoldw, curoldh); path.reset(); invalidate(); } //筆の色を変更 public void setColor(int color){ paint.setColor(color); } //筆の幅を変更 public void setPenWidth(int size){ paint.setStrokeWidth(size); } }