Tôi đang theo dõi để vẽ theo chế độ xem của riêng mình (R.id.view) nhưng mã này dường như không có bất kỳ ảnh hưởng nào. Nó không cho phép tôi vẽ gì cả.Android - Cách vẽ trên chế độ xem
public class MainActivity extends Activity implements OnTouchListener
{
Path mPath;
Canvas canvas;
Paint mPaint;
MaskFilter mEmboss;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view=(View)findViewById(R.id.view1);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
canvas = null;
//view.draw(canvas);
mPath = new Path();
Bitmap mBitmap;
//Paint mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mBitmap = Bitmap.createBitmap(210, 170, Bitmap.Config.ARGB_8888);
canvas = new Canvas(mBitmap);
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mPaint);
//canvas.drawPaint(mPaint);
view.draw(canvas);
canvas.drawPath(mPath, mPaint);
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
0.4f, 6, 3.5f);
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
Log.d("x", String.valueOf(x));
Log.d("y", String.valueOf(y));
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("a","down");
touch_start(x, y);
//invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
// invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
// invalidate();
break;
}
return true;
}
});
// mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
canvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
}
Tôi phải làm gì để sử dụng sơn ngón tay freehanf theo quan điểm của riêng tôi?
** Tham khảo câu trả lời của tôi [Nhấp vào đây] (http://stackoverflow.com/a/41135685/4629101) ** –