[英]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.