这个和弹幕是一样的原理,代码如下:
package com.example.star;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import java.util.Random;
/**
* Created by Adminis on 2016/8/7.
*/
public class StarView extends RelativeLayout {
private static final String TAG = "StarView";
private int mWidth;
private int mHeight;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
setImageAnim();
mHandler.sendEmptyMessageDelayed(0,100);
}
};
public StarView(Context context) {
super(context);
}
public StarView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public StarView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if(w!=0&&h!=0){
this.mWidth = w;
this.mHeight = h;
Log.e(TAG,"mHeight="+mHeight);
setImageAnim();
mHandler.sendEmptyMessageDelayed(0,1000);
}
}
private void setImageAnim() {
final ImageView iv = new ImageView(getContext());
iv.setImageResource(R.mipmap.star);
LayoutParams params = new LayoutParams(40, 40);
Random random = new Random();
params.leftMargin=random.nextInt(mWidth-40)+1;
this.addView(iv, params);//添加到相对布局中
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator animation = ObjectAnimator.ofFloat(iv,"translationY",mHeight,0);
animatorSet.playTogether(animation);
animatorSet.setDuration(1000);
animatorSet.start();
animatorSet.addListener(new Animator.AnimatorListener(){
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
iv.clearAnimation();
removeView(iv);
}
@Override
public void onAnimationCancel(Animator animation) {
iv.clearAnimation();
removeView(iv);
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
if(!hasWindowFocus){
mHandler.removeCallbacksAndMessages(null);
}
}
}
效果;

如果要实现那种曲线的话,就要使用到贝尔塞曲线了,可惜我不懂!