我正在开发一款游戏,在游戏中,我需要将一个图像沿着Canvas向任意方向移动,而不仅仅是垂直或水平。
我应该如何实现这种方式的移动?
我正在开发一款游戏,在游戏中,我需要将一个图像沿着Canvas向任意方向移动,而不仅仅是垂直或水平。
我应该如何实现这种方式的移动?
在接受了一堂数学讲座后,发现这很容易解决。首先,我们需要获取目标移动的角度。
float deltaX = targetX - startX;
float deltaY = targetY - startY;
float angle = Math.atan2( deltaY, deltaX );
startX/Y可以是当前的X/Y坐标。
现在我们已经计算出角度,可以将其应用于当前坐标:
currentX += speed * Math.cos(angle);//Using cos
currentY += speed * Math.sin(angle);//or sin
处理如何增加X和Y的计算。
如果需要设置自定义速度,请使用速度作为自定义变量。如果不需要更多速度,请删除该变量。
将X/Y应用于对象以移动对象:
c.drawBitmap(bm, x, y, null);
例子:
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);
(...)
}
我不明白您真正想要什么,但这是我尝试过的东西。
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);
}
}
您可以将其从一个位置拖动到另一个位置,也可以使用翻译来移动它...就像这样:
view.startCoordinates(0f,0f);
view.endCoordinates(500f,0f);
view.startTranslation(3000);
看看这个库。它应该帮助你移动,我认为还可以拖动像图像等对象 - PhysicsLayout。在这里,您可以看到沿着两个方向移动 - X,Y。如果您想包括Z移动,则只有一种实现方法,您应该使用简单的缩放。
如果您想要更多东西,有很多强大而漂亮的框架和环境。