如何使用Android Canvas将图像从一个点移动到另一个点 [英] How can I move an image from one point to another using Android Canvas

查看:232
本文介绍了如何使用Android Canvas将图像从一个点移动到另一个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款游戏,在这个游戏中,我必须将Canvas上的图像从一个点移动到另一个点,而不仅仅是垂直或水平。

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 move that image in this manner?

推荐答案

我无法理解你真正想要的东西,但这是我尝试过的东西。

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);

这篇关于如何使用Android Canvas将图像从一个点移动到另一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆