通过单击图像开始图像动画

时间:2014-01-28 11:28:49

标签: android android-animation

我有一个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 个答案:

答案 0 :(得分: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
 }

希望它对你有所帮助。