0

My Activity:

 public class myMainActivity extends AppCompatActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.MyLayout);

     }
 }

I want to draw by finger on the background of my layout like so:

enter image description here

Nikolay Fominyh
  • 8,946
  • 8
  • 66
  • 102
Rux77
  • 19
  • 1

1 Answers1

0

You need to have a paint class first

paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[]{15.0f, 15.0f}, 0));

and implement the following onTouchListener

public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();

switch (action) {
  case MotionEvent.ACTION_DOWN:


  downx = event.getX();
  downy = event.getY();
  clipPath = new Path();
  clipPath.moveTo(downx, downy);
  tdownx = downx;
  tdowny = downy;
  smallx = downx;
  smally = downy;
  largex = downx;
  largey = downy;
  lastTouchDown = System.currentTimeMillis();
   break;

  case MotionEvent.ACTION_MOVE: 
  upx = event.getX();
  upy = event.getY();
 cropModelArrayList.add(new CropModel(upx, upy));
 clipPath = new Path();
 clipPath.moveTo(tdownx,tdowny);
  for(int i = 0; i<cropModelArrayList.size();i++){ clipPath.lineTo(cropModelArrayList.get(i).getY(),cropModelArrayList.get(i).getX(   ));
    }
   canvas.drawPath(clipPath, paint);
   im_crop_image_view.invalidate();
   downx = upx;
   downy = upy;
   break;
   case MotionEvent.ACTION_UP:
if (System.currentTimeMillis() - lastTouchDown < CLICK_ACTION_THRESHHOLD) {

 cropModelArrayList.clear();
 initcanvas();

 int cx = (screen_width - bmp.getWidth()) >> 1;
 int cy = (screen_height - bmp.getHeight()) >> 1;
 canvas.drawBitmap(bmp, cx, cy, null);
 im_crop_image_view.setImageBitmap(alteredBitmap);

 } else {
 if (upx != upy) {
 upx = event.getX();
 upy = event.getY();


 canvas.drawLine(downx, downy, upx, upy, paint);
clipPath.lineTo(upx, upy);
yourview.invalidate();

   }

  }
  break;
  return true;
   }
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Ramees
  • 68
  • 4
  • 16