Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving bitmap on canvas

I have an imageView, canvas, and a button.. when I click the button a bitmap gets drawn on the canvas

I want to move that bitmap using my onTouch ( drag the bitmap to 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 gets created on 0,0 but I can't drag it etc ..

like image 248
John Sir. Alex Avatar asked Mar 31 '13 09:03

John Sir. Alex


1 Answers

 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
 }
 }
 }

At the star bitmap is draw at x=0 and y=0; On drag x and y changes so call inavlidate to refresh drate. On touch up draw the bitmap at the dragged position x and y and call invalidate to refresh draw.

Bitmap drawn at x=0 y=0. on drag and drop //x= 345 y=530. The resulting snap shot is attached.

You need to make sure your image does not look like going out of screen at the screen edges Check if x is within screenwidth - bitmapwidth and y is less than screenheight - bitmapheight. i have not included those 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);
like image 130
Raghunandan Avatar answered Oct 08 '22 14:10

Raghunandan