Android - Comment dessiner sur la vue

Je suis tring pour peindre sur mon propre point de vue (R. id.vue) mais ce code ne semble pas avoir d'effet. Il ne m'autorise pas à tirer quoi que ce soit.

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();
}
}

Que dois-je faire pour utiliser le freehanf de la peinture au doigt sur mon propre point de vue?

Consulter ma réponse Cliquez ici

OriginalL'auteur Aakash Anuj | 2012-12-13