简体   繁体   中英

Android and onTouch

I have class where I draw image. This is code:

 public class CanvasdrawActivity extends Activity implements OnTouchListener {
      ImageView imageView;
      Bitmap bitmap;
      Bitmap bitmap2;
      Canvas canvas;
      Paint paint;
      boolean oneClick=true;
      float downx = 0,downy = 0,upx = 0,upy = 0;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView) this.findViewById(R.id.imageView1);
        Display currentDisplay = getWindowManager().getDefaultDisplay();
        float dw = currentDisplay.getWidth();
        float dh = currentDisplay.getHeight();

        bitmap = Bitmap.createBitmap((int) dw, (int) dh,
            Bitmap.Config.ARGB_8888);
        bitmap2=BitmapFactory.decodeResource(getResources(),
                R.drawable.star_bez_nog);

        canvas = new Canvas(bitmap);

        imageView.setImageBitmap(bitmap);
        imageView.setOnTouchListener(this);
      }

      public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            if(oneClick){
          downx = event.getX();
          downy = event.getY();
          canvas.drawBitmap(bitmap2, downx, downy, null);
          imageView.invalidate();
          oneClick=false;
            }
          break;

        }
        return true;
      }
    }

When I click first time image was drawn. How I can do that when I click in other place this old picture disapear and new is drawing?

In ontouch clear the canvas every time like this..

Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawPaint(paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC));

after this draw your bitmap at required location..

canvas.drawBitmap(bitmap2, downx, downy, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM