package net.dixq.painter; import android.graphics.Color; public class Def { public static final class color { public final static int red = Color.argb(255, 240, 20, 20); public final static int green = Color.argb(255, 20, 240, 20); public final static int blue = Color.argb(255, 20, 20, 240); public final static int erase = Color.argb(255, 255, 255, 255); } }
package net.dixq.painter; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PointF; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.ImageView; public class PaintView extends ImageView { private Paint mPaint = new Paint(); //ペンの設定 private int mPenColor = Def.color.red; //ペンの現在の選択色 private final int THICKNESS = 10; //太さ private PointF mStartPt = new PointF(); //始点 private PointF mEndPt = new PointF(); //終点 private Bitmap mBmp = null; //ViewにセットするBmp private Canvas mCanvas = null; //キャンバス public PaintView(Context context, AttributeSet attrs) { super(context, attrs); mPaint.setColor(mPenColor); // 色設定 mPaint.setStrokeWidth(THICKNESS); // 太さ設定 mPaint.setStrokeCap(Paint.Cap.ROUND); // 角を円く } @Override protected void onMeasure(int wSpec, int hSpec) { super.onMeasure(wSpec, hSpec); int w = MeasureSpec.getSize(wSpec);//幅を計算 int h = MeasureSpec.getSize(hSpec);//高さを計算 mBmp = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);//Bmpを作る mCanvas = new Canvas(mBmp);//キャンバスにセット mCanvas.drawColor(Color.WHITE);//白で塗りつぶし this.setImageBitmap(mBmp);//mBmpをImageViewの面に設定する } @Override public boolean onTouchEvent(MotionEvent e) { // タッチ時に呼ばれる switch (e.getAction()) { case MotionEvent.ACTION_DOWN: // 画面がタッチされたときの動作 mStartPt.set(e.getX()-1, e.getY()-1);// チョン押しでも描画されるように-1 DrawLine(e); break; case MotionEvent.ACTION_MOVE: // タッチしたまま移動したときの動作 DrawLine(e); break; case MotionEvent.ACTION_UP: // タッチが離されたときの動作 DrawLine(e); break; } return true; } //線を引く private void DrawLine( MotionEvent e ){ mEndPt.set(e.getX(),e.getY()); mCanvas.drawLine(mStartPt.x, mStartPt.y, mEndPt.x, mEndPt.y, mPaint); mStartPt.set( mEndPt ); invalidate(); //再描画イベントを起こす } }
Portions of this page are modifications
based on work created and shared by Google and used according to terms
described in the Creative Commons 3.0 Attribution License.
- Remical Soft -