Touch and drag image in android

前端 未结 2 1695
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 17:53

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?

2条回答
  •  执念已碎
    2020-11-27 18:16

    As a slight modification to the TouchBall answer - if you really don't have a game loop - in other words, the only changes to the screen are directly due to user input - then it might make more sense to leave out the thread. Otherwise it is just constantly looping and redrawing even if nothing has changed. So:

    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 int x=20,y=20;int width,height;
    
        public BallView(Context context,int w,int h) {
            super(context);
    
            width=w;
            height=h;
            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;
    
            updateBall();
    
            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) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
        }
    
        private void updateBall() {
            Canvas canvas = null;
            try {
                canvas = getHolder().lockCanvas(null);
                synchronized (getHolder()) {
                    this.onDraw(canvas);
                }
            }
            finally {
                if (canvas != null) {
                    getHolder().unlockCanvasAndPost(canvas);
                }
            }
        }   
    }
    

    Admittedly, I am new to Android development, so I may be missing something here.

提交回复
热议问题