How can I move an image from one point to another using Android Canvas

后端 未结 3 1475
慢半拍i
慢半拍i 2021-02-13 02:32

I\'m developing a game, and in this game I have to move an image on a Canvas from one point to another in any direction, not just vertical or horizontal.

How can I mo

3条回答
  •  星月不相逢
    2021-02-13 02:39

    I couldn't understand what you actually want but here's something I've tried.

        interface coordinateListener
    {
        public void currentPosition(float x,float y);
    }
    
    public class ImageView extends View{
        int width;
        int height;
        RectF rect = new RectF();
        float x=0f,y=0f;
        float dX,dY;
        float mStartX,mStartY;
        float mEndX,mEndY;
        Paint paint = new Paint();
        Bitmap mBitmap;
        TranslateAnimation anim;
        View view;
        coordinateListener mListener;
        public ImageView(Context context) {
            super(context);
            init();
        }
    
        public ImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public ImageView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
    public void init()
    {
        view = this;
    }
    @Override
    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        rect.set(0,0,width,height);
    }
    
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        if(mBitmap!=null) {
            canvas.drawBitmap(mBitmap,0,0,paint);
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        int action = event.getAction() & MotionEvent.ACTION_MASK;
    
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                dX = this.getX() - event.getRawX();
                dY = this.getY() - event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                y = event.getRawY();
                x = event.getRawX();
                y += dY;
                x+=dX;
    
                this.setY(y);
                this.setX(x);
    
                mListener = (coordinateListener)getContext(); 
                mListener.currentPosition(x,y);
    
                invalidate();
                break;
        }
        return true;
    }
    
    public void startCoordinates(float x,float y)
    {
        mStartX = x;
        mStartY = y;
    }
    
    public void endCoordinates(float x,float y)
    {
        mEndX = x;
        mEndY = y;
    }
    
    public void startTranslation(long duration)
    {
        anim = new TranslateAnimation(mStartX,mEndX,mStartY,mEndY);
        anim.setDuration(duration);
        anim.setFillAfter(true);
    
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
    
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
                view.setX((int)mEndX);
                view.setY((int)mEndY);
                animation.setFillAfter(false);
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
    
            }
        });
    
        this.startAnimation(anim);
    }
    
    }
    

    You can either drag it from one position to another or you can use Translate to move it...like this,

    view.startCoordinates(0f,0f);
    view.endCoordinates(500f,0f);
    view.startTranslation(3000);
    

提交回复
热议问题