2010-10-14 12 views

Respuesta

0

Eche un vistazo a esta biblioteca. Debería ayudarlo con el movimiento, y creo que arrastrar objetos como imágenes, etc. - PhysicsLayout. Aquí puedes ver cómo te mueves con dos direcciones: X, Y. Si quieres moverte incluyendo Z, solo hay una forma única de implementarlo, debes usar una escala simple.

Si quieres algo más, hay muchos Frameworks potentes y bastante bonitos, Entorno.

enter image description here

+0

El objetivo aquí es conseguir que se mueva en línea recta desde la posición actual a una posición objetivo. Míralo cruzar un cuadrado al otro lado. Es un comportamiento del juego y es posible sin complementos o complementos – Zoe

+0

Lo que trato de decir es que esta respuesta se ocupa de la física relacionada con el movimiento, pero no de cómo hacerlo en el sentido de una navegación automática sobre el lienzo. – Zoe

+0

Todavía intento entender) Por ejemplo, al usar el método de View.java - setTranslationX (float x), toma las coordenadas y simplemente vuelve a dibujar usando Canvas. ¿Desea implementar el lienzo de dibujo según la posición? – GensaGames

0

Después de conseguir una conferencia de matemáticas, resulta que esto es fácil de resolver. Primero, necesitamos obtener el ángulo al cual se moverá el objetivo.

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

startX/Y puede ser X/Y actual.

Ahora que hemos calculado el ángulo, podemos aplicarlo a las coordenadas actuales:

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

para manejar los cálculos de la cantidad de X e Y se incrementarán en. Usar la velocidad como una variable personalizada si necesita tener un conjunto de velocidad personalizado también. Si no necesita más velocidad, elimine la variable.

Y para mover el objeto, aplique X/Y para el objeto:

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

Ejemplo:

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); 
    (...) 
} 
1

que no podía entender lo que realmente quiere, pero esto es algo que yo' lo intenté.

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

} 

Usted puede arrastrarlo desde una posición a otra o puede utilizar Traducir al moverlo ... como este,

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

No es una vista real. Está utilizando la vista de superficie y el mapa de bits. Para hacerlo – Zoe

Cuestiones relacionadas