如何使用动画使视图像android中的气泡一样浮动?

时间:2014-04-18 12:36:18

标签: android canvas android-animation

我想在屏幕上像气泡一样浮动4圈动画,我已经尝试了很多,但我没有任何解决方案,请任何人帮助我,提前谢谢。

我尝试使用下面的代码,这是一个圆圈,我想让它为4个圆圈。

public class AnimatedView extends ImageView{

private Context mContext;
int x = -1;
int y = -1;
private int xVelocity = 10;
private int yVelocity = 5;
private Handler h;
private final int FRAME_RATE = 30;


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

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

protected void onDraw(Canvas c) {  

    BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);  
    if (x<0 && y <0) {
        x = this.getWidth()/2;
        y = this.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);  

    h.postDelayed(r, FRAME_RATE);


} 

}

1 个答案:

答案 0 :(得分:0)

根据以下

改进您的代码

公共类AnimatedView扩展了ImageView {

private Context mContext;
int x = -1;
int y = -1;
private int xVelocity = 10;
private int yVelocity = 5;
private Handler h;
private  int FRAME_RATE = 30;
private boolean showAnimation=true;
int width  ,height;

public AnimatedView(Context context, AttributeSet attrs)  {
    super(context, attrs);
    mContext = context;
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    width = displayMetrics.widthPixels;
    height = displayMetrics.heightPixels;
    h = new Handler();
    loadStateFromAttrs(attrs);

}
private void loadStateFromAttrs(AttributeSet attributeSet) {
    if (attributeSet == null) {
        return; // quick exit
    }

    TypedArray a = null;
    try {
        a = getContext().obtainStyledAttributes(attributeSet, R.styleable.AnimatedView);
        FRAME_RATE = a.getInt(R.styleable.AnimatedView_FRAME_RATE, FRAME_RATE);

    } finally {
        if (a != null) {
            a.recycle(); // ensure this is always called
        }
    }
}
private Runnable r = new Runnable() {
    @Override
    public void run() {

        invalidate();
    }
};

protected void onDraw(Canvas c) {


    //BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
    BitmapDrawable ball = (BitmapDrawable) getDrawable();
    if(showAnimation)
    {
        if (x<0 && y <0) {
            x = width/2;
            y = height/2;
        } else {
            x += xVelocity;
            y += yVelocity;
            if ((x > width - ball.getBitmap().getWidth()) || (x < 0)) {
                xVelocity = xVelocity*-1;
            }
            if ((y > height - ball.getBitmap().getHeight()) || (y < 0)) {
                yVelocity = yVelocity*-1;
            }
        }
    }

    c.drawBitmap(ball.getBitmap(), x, y, null);
    if(showAnimation)
    h.postDelayed(r, FRAME_RATE);

}
public void startAnimation()
{
    showAnimation=true;
}
public void stopAnimation()
{
    showAnimation=false;
}

}