简体   繁体   English

如何使用Android Canvas将图像从一个点移动到另一个点

[英]How can I move an image from one point to another using Android 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. 我正在开发一款游戏,在这款游戏中,我必须将Canvas上的图像从一个点移动到另一个点,而不仅仅是垂直或水平。

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, 你可以将它从一个位置拖到另一个位置,或者你可以使用Translate移动它......像这样,

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

Take a look into this library. 看看这个图书馆。 It should help you with moving, and I think dragging objects like image, etc - PhysicsLayout . 它应该可以帮助你移动,我想拖动像图像等物体 - PhysicsLayout Here you can see moving with Two directions - X, Y. If you want to move including Z, there is only single way to implement it, you should use simple scaling. 在这里你可以看到两个方向移动 - X,Y。如果你想移动包括Z,只有一种方法来实现它,你应该使用简单的缩放。

If you want something more, there are a lot of powerful and pretty nice Frameworks, Environment. 如果你想要更多东西,那么有许多强大而漂亮的框架,环境。

在此输入图像描述

After getting a math lecture, it turns out that this is easy to solve. 在完成数学讲座后,事实证明这很容易解决。 First, we need to get the angle which the target will be moving at. 首先,我们需要获得目标将要移动的角度。

float deltaX = targetX - startX;
float deltaY = targetY - startY;
float angle = Math.atan2( deltaY, deltaX );

startX/Y can be current X/Y. startX / Y可以是当前的X / Y.

Now that we have calculated the angle, we can apply it to the current coordinates: 现在我们已经计算了角度,我们可以将它应用到当前坐标:

currentX += speed * Math.cos(angle);//Using cos
currentY += speed * Math.sin(angle);//or sin

to handle the calculations of how much X and Y will be increased by. 处理X和Y将增加多少的计算。 Using speed as a custom variable if you need to have a custom speed set as well. 如果您还需要设置自定义速度,请将速度用作自定义变量。 If you don't need more speed, remove the variable. 如果您不需要更快的速度,请删除变量。

And to move the object, apply X/Y to the object: 要移动对象,请将X / Y应用于对象:

c.drawBitmap(bm, x, y, null);

Example: 例:

int speed = 10;
int x, y;
int targetX = 100, targetY = 600;
int startX = 900, startY = 100;
public void render(Canvas c){
    super.draw(c);
    float deltaX = targetX - startX;
    float deltaY = targetY - startY;
    float angle = Math.atan2( deltaY, deltaX );
    x += speed * Math.cos(angle);//Using cos
    y += speed * Math.sin(angle);//or sin
    c.drawBitmap(bm, x, y, null);
   (...)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想将图标从一个位置点移动到另一点,图标应在Android中将一个位置移动到另一个位置 - I want to move icon from one location point to another point where icon should move one location to other in android 如何从扩展线程的类开始在JPanel中创建动画(从一个点移动一个圆圈到另一个点)? - How do I start an animation (move a circle from one point to another) in a JPanel from a class that extends thread? 如何将引用从一个对象移动到另一个对象? - How can I move a reference from one object to another? 如何使用OpenJPA将对象从一个表“移动”到另一个表? - How can I “move” an object from one table to another using OpenJPA? 如何通过坐标使对象从一个点移动到另一个点? - How to make an object move from one point to another via coordinates? 如何将物体从一个点以固定角度均匀地移动到另一点? - How to move an object uniformly from one point to another at a fixed angle? 如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio - How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio 如何使用Javafx canvas在另一个Circle上移动Point? - How to move a Point around another Circle use Javafx canvas? 如何从一个活动转移到活动之外的另一个活动 - how to move from an activity to another one from outside an activity android How to move from one activity to another ie Java Class to Kotlin Class in an Android Java Project? - How to move from one activity to another i.e Java Class to Kotlin Class in an Android Java Project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM