Move bitmap to canvas

I have an image, a canvas and a button. when i click the button, the bitmap is drawn on the canvas

I want to move this bitmap using my onTouch (drag the bitmap anywhere on the canvas).

s.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Bitmap workingBitmap = Bitmap.createBitmap(currentBitmap); workingBitmap = Bitmap.createBitmap(workingBitmap); Canvas c = new Canvas(workingBitmap); brightBitmap = BitmapFactory.decodeResource(getResources(), sIds.mSmileyIds[position], null); brightBitmap = Bitmap.createScaledBitmap(brightBitmap, 100, 100, false); chosenSmiley = brightBitmap; if (chosenSmiley != null) { try { c.drawBitmap(chosenSmiley, posX, posY, null); } catch (Exception e) { e.printStackTrace(); } } iv.setImageBitmap(workingBitmap); } public boolean onTouchEvent(MotionEvent event) { int eventaction = event.getAction(); switch (eventaction) { case MotionEvent.ACTION_DOWN: // finger touches the screen break; case MotionEvent.ACTION_MOVE: // finger moves on the screen lastX = (int) event.getX(); lastY = (int) event.getY(); Log.e("my xname", lastX + " Coords of lastX"); Log.e("my xname", lastY + " Coords of lastY"); brightBitmap = Bitmap.createScaledBitmap(brightBitmap, lastX, lastY, false); break; case MotionEvent.ACTION_UP: // finger leaves the screen break; } // tell the system that we handled the event and no further processing is required return true; } }); 

this is my current code, the bitmap is created at 0.0, but I cannot drag it, etc.

+4
source share
1 answer
  public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); DrawingView dv = new DrawingView(this); setContentView(dv); } class DrawingView extends View{ Bitmap bitmap; float x,y; public DrawingView(Context context) { super(context); bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher); } public boolean onTouchEvent(MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN: { } break; case MotionEvent.ACTION_MOVE: { x=(int)event.getX(); y=(int)event.getY(); invalidate(); } break; case MotionEvent.ACTION_UP: x=(int)event.getX(); y=(int)event.getY(); System.out.println(".................."+x+"......"+y); //x= 345 y=530 invalidate(); break; } return true; } @Override public void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.CYAN); canvas.drawBitmap(bitmap, x, y, paint); //originally bitmap draw at x=o and y=0 } } } 

In a star raster image, draw at x = 0 and y = 0; When dragging x and y, the changes cause inavlidate to update the drate. When you touch up, display the bitmap in the draggable position x and y and call invalidate to update the draw.

A bitmap drawn at x = 0 y = 0. when dragging // x = 345 y = 530. The resulting click is anchored.

You need to make sure your image does not seem to exit the screen at the edges of the screen. Check if x is within the width of the screen - bitmap and y is less than shielding - bitmap. I did not include these conditions in the code.

enter image description here

EDIT:

  setContentView(R.layout.main); LinearLayout ll = (LinearLayout) findViewById(R.id.ll); DrawingView dv= new DrawingView(this); ll.addView(dv); 
+6
source

Source: https://habr.com/ru/post/988159/


All Articles