2014-01-28 140 views
0

我有一個animatedview.java啓動時,它看起來像後續圖像圖像動畫點擊圖像

this image move from bottom to top and top to bottom when it installed into device

我是儘量使這個應用程序爲跟蹤要求 這就是我想在圖像上單擊,然後只將開始動畫,否則它會等待我的點擊,這個我寫這樣的代碼,

Animatedview.java

public class AnimatedView extends ImageView{ 

static int count=0; 
private Context mContext; 
int x = 150; 
int y = 450; 
private float a,b; 
private int xVelocity = 10; 
private int yVelocity = 15; 
private Handler h; 
private final int FRAME_RATE = 25; 
BitmapDrawable ball; 
boolean touching; 
boolean dm_touched = false; 

int bm_x = 0, bm_y = 0, bm_offsetx, bm_offsety,bm_w,bm_h; 

public AnimatedView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mContext = context; 
    h = new Handler(); 

} 
private Runnable r = new Runnable() { 
    @Override 
    public void run() { 
     if(touching = true) 
     invalidate(); 
    } 
}; 

@Override 
protected void onDraw(Canvas c) { 

    int z= c.getHeight()/2; 
    Log.e("bharat","z is"+z); 
    BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball); 
    if (x<0 && y <0) { 
     //x = this.getWidth()/2; 
     y = c.getHeight()/2; 


    } else { 
     //x += xVelocity; 
     y += yVelocity; 
     if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) { 
      xVelocity = xVelocity*-1; 
     } 
     if (((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0))) { 
      yVelocity = yVelocity*-1; 
     } 
    } 
    c.drawBitmap(ball.getBitmap(), x, y, null); 
    Log.e("sarat",""+touching); 
    if(touching){ 
    h.postDelayed(r, FRAME_RATE);  
    bm_w=ball.getBitmap().getWidth(); 
    bm_h=ball.getBitmap().getHeight(); 
    } 
} 
@Override 
public boolean onTouchEvent(MotionEvent event) { 
    Log.d("bharat","ontouch called"); 
    int touchType = event.getAction(); 

    switch(touchType){ 
     case MotionEvent.ACTION_MOVE: 
      a = event.getX(); 
      b = event.getY(); 
      touching = true; 
      if (dm_touched) { 
       x = (int) a - bm_offsetx; 
       y = (int) b - bm_offsety; 
      } 
      break; 

     case MotionEvent.ACTION_DOWN: 
      //x and y give you your touch coordinates 
       a = event.getX(); 
       b = event.getY(); 
       touching = true; 
       Log.d("bharat","action_down called"); 
       if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 


        count++; 
        Log.i("bharat",""+count); 


       } 

       dm_touched = true; 
     case MotionEvent.ACTION_UP: 
      default: 

       dm_touched = true; 
       touching = true; 


      } 

      return true; 
    } 
    } 

但是,我不能讓我的要求,請幫我

回答

1

您可以嘗試以下,這是從我的項目:

<?xml version="1.0" encoding="utf-8"?> 
    <set xmlns:android="http://schemas.android.com/apk/res/android"> 

    <alpha 
    android:fromAlpha = "1.0" 
    android:toAlpha = "0.5" 
    android:duration = "300"> 
    </alpha> 
    <scale 
    android:fromXScale = "1" 
    android:toXScale = "0.9" 
    android:fromYScale = "1" 
    android:toYScale = "0.9" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration = "50"> 
    </scale> 
    </set> 

我不知道這是否是正確的方法,但定義如上所述的動畫伎倆。現在我們只需要給

public void onClick(View v) { 
v.startAnimation(AnimationUtils.loadAnimation(Context, R.anim.image_click)); 
//Your other coding if present 
} 

希望它可以幫助你。

+0

通過使用XML風格我無法做更遠的動畫,請你鍛鍊我的代碼 – vishnub1626