2 votes

Android : Dessinez une image sur le canevas à l'endroit où l'utilisateur touche

J'ai donc un canevas, et je voudrais qu'une certaine image (disons image.png) apparaisse là où l'utilisateur touche. Que dois-je utiliser ? OnTouch ?

Quelqu'un peut-il m'aider ? Voici ma classe de toile ci-dessous. En ce moment, j'ai quelques images et formes aléatoires dessinées, mais elles arrivent tout de suite, pas quand quelqu'un touche comme je le voudrais.

Merci d'avance !

public class MyView extends View

private Canvas canvas;
private Bitmap bitmap;

public MyView(Context context) {
    super(context);

    }    

protected void onSizeChanged(int curw, int curh, int oldw, int oldh) {
    if (bitmap != null) {
        bitmap .recycle();
    }
    canvas= new Canvas();
    bitmap = Bitmap.createBitmap(curw, curh, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);

}
public void destroy() {
    if (bitmap != null) {
        bitmap.recycle();
    }
}

public void onTouch(Canvas canvas) {
    Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    canvas.drawColor(Color.TRANSPARENT);
    canvas.drawBitmap(_scratch, 0, 0, null);
}

public void onDraw(Canvas canvas) {
  //draw onto the canvas

     Bitmap _scratch1 = BitmapFactory.decodeResource(getResources(), R.drawable.spacecat);
     canvas.drawColor(Color.TRANSPARENT);
     canvas.drawBitmap(_scratch1, 0, 0, null);

     Paint myPaint = new Paint();
     myPaint.setStrokeWidth(4);
     myPaint.setColor(0xFF097286);
     canvas.drawCircle(240, 40, 30, myPaint);
     myPaint.setColor(0xFFF07222);
     Point p1 = new Point();
     Point p2 = new Point();
     p1.x = 0;
     p1.y = 0;
     p2.x = 40;
     p2.y = 55;
     canvas.drawLine(p1.x, p1.y, p2.x, p2.y, myPaint);
     float[] pts = new float[8];
     pts[0] = 100;
     pts[1] = 5;
     pts[2] = 97;
     pts[3] = 9;
     pts[4] = 90;
     pts[5] = 15;
     pts[6] = 84;
     pts[7] = 20;
     myPaint.setColor(0xFF40FF40);
     myPaint.setStrokeWidth(9);
     myPaint.setAntiAlias(true);
     canvas.drawPoints(pts, myPaint);

     myPaint.setColor(0xFFF0FF00);
     myPaint.setStrokeWidth(4);
     myPaint.setStyle(Paint.Style.STROKE);
     canvas.drawCircle(110, 150, 100, myPaint);

}}

3voto

Isaiah Points 306

J'ai écrit un exemple pour vous :

public class MyView extends View {

    boolean touching = false;

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);    //To change body of overridden methods use File | Settings | File Templates.

        if (touching) {
            //You can draw other thing here.  just draw bitmap for example.
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
            Rect source = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            Rect bitmapRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            canvas.drawBitmap(bitmap, source, bitmapRect, new Paint());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touching = true;
                invalidate();
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                touching = false;
                invalidate();
                break;
        }
        return super.onTouchEvent(event);    //To change body of overridden methods use File | Settings | File Templates.
    }
}

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X