Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touch and drag image in android

I am working on some example in which i want to drag the image corresponding to touch in Android. Does anybody have an idea about how I can do it?

like image 764
dharam Avatar asked Nov 23 '10 12:11

dharam


People also ask

Does Android have drag and drop?

The Android drag and drop framework enables you to add interactive drag and drop capabilities to your app. With drag and drop, users can copy or move text, images, objects—any content that can be represented by a URI—from one View to another within an app or, in multi-window mode, between apps.

How do I drag and drop on Android tablet?

A double tap acts as a double click. A double tap and hold allows you to grab and then drag.


1 Answers

public class TouchBall extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int w=getWindowManager().getDefaultDisplay().getWidth()-25;
    int h=getWindowManager().getDefaultDisplay().getHeight()-25;

    BallView ballView=new BallView(this,w,h);
    setContentView(ballView);
}


}
public class BallView extends SurfaceView implements SurfaceHolder.Callback {

    private Bitmap bitmap ;
    private MyThread thread;
    private int x=20,y=20;int width,height;

    public BallView(Context context,int w,int h) {
        super(context);

        width=w;
        height=h;
        thread=new MyThread(getHolder(),this);
        getHolder().addCallback(this);
        setFocusable(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.ball_green);
        canvas.drawColor(Color.BLUE);//To make background 
        canvas.drawBitmap(bitmap,x-(bitmap.getWidth()/2),y-(bitmap.getHeight()/2),null);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        x=(int)event.getX();
        y=(int)event.getY();

        if(x<25)
                x=25;
         if(x> width)   
                x=width;
         if(y <25)
                y=25;
         if(y > 405)
                y=405;      
        return true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        thread.startrun(true);
        thread.start();

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {


        thread.startrun(false);
        thread.stop();

    }   
 }

thread:

public class MyThread extends Thread {

private SurfaceHolder msurfaceHolder;
private BallView mballView;
private boolean mrun =false;

public MyThread(SurfaceHolder holder, BallView ballView) {

    msurfaceHolder = holder;
    mballView=ballView;
}

public void startrun(boolean run) {

    mrun=run;
}

@Override
public void run() {

    super.run();
     Canvas canvas;
     while (mrun) {
        canvas=null;
         try {
             canvas = msurfaceHolder.lockCanvas(null);
              synchronized (msurfaceHolder) {
               mballView.onDraw(canvas);
             }
         } finally {
                 if (canvas != null) {
                 msurfaceHolder.unlockCanvasAndPost(canvas);
             }
         }
     }
  }

}
like image 86
SBK Avatar answered Sep 30 '22 19:09

SBK