自定義view中最終要的莫過(guò)于貝塞爾曲線的學(xué)習(xí)和掌握窒朋,抽空寫(xiě)了個(gè)練手的小玩意醋拧。先上效果圖
注:本文章不展開(kāi)介紹貝塞爾曲線(建議先簡(jiǎn)單認(rèn)識(shí)貝塞爾曲線再食用)
效果圖
廢話不多說(shuō)丈积,直接上代碼:
LikeStarView
public class LikeStarView extends View {
public static final String TAG = "LikeStarView";
private Bitmap mBitmap;
private Point mPoint, mStartPoint, mEndPoint;
public LikeStarView(Context context) {
this(context, null);
}
public LikeStarView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public LikeStarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
//初始化弊添。就加載一個(gè)圖片鬼悠。
Resources resources = context.getResources();
mBitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_fav);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, mPoint.x, mPoint.y, new Paint());
super.onDraw(canvas);
}
public void startanim() {
if (mStartPoint == null) {
return;
}
//沒(méi)有做適配删性,下面500,201等數(shù)字可以根據(jù)實(shí)際傳入
mEndPoint = new Point(mStartPoint.x, mStartPoint.y - 500);//結(jié)束的點(diǎn)焕窝,起始點(diǎn)就是點(diǎn)擊的點(diǎn)
Random random = new Random();
int result = random.nextInt(201) - 100; //隨機(jī)左右偏移控制點(diǎn)位置
Point controllPoint = new Point(mStartPoint.x + result, mStartPoint.y - 250);//貝塞爾曲線的控制點(diǎn)
LikeStarEvaluator likeStarEvaluator = new LikeStarEvaluator(controllPoint);
ValueAnimator animator = ValueAnimator.ofObject(likeStarEvaluator, mStartPoint, mEndPoint);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Point point = (Point) animation.getAnimatedValue();
mPoint.x = point.x;
mPoint.y = point.y;
//計(jì)算透明度
setAlpha((float) (point.y - mEndPoint.y) / (float) 500);
invalidate();
}
});
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
// 移除圖像
ViewGroup viewGroup = (ViewGroup) getParent();
viewGroup.removeView(LikeStarView.this);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animator.setDuration(2000);
animator.setInterpolator(new LinearInterpolator());
animator.start();
}
//設(shè)置初始點(diǎn)
public void setstartpoint(Point point) {
this.mStartPoint = point;
this.mPoint = point;
}
}
LikeStarEvaluator:
class LikeStarEvaluator implements TypeEvaluator<Point> {
//傳入控制點(diǎn)蹬挺,用于計(jì)算路徑
private Point controllPoint;
public LikeStarEvaluator(Point controllPoint) {
this.controllPoint = controllPoint;
}
@Override
public Point evaluate(float t, Point startValue, Point endValue) {
//這兩個(gè)是二階貝塞爾曲線計(jì)算路徑上的點(diǎn)公式,這個(gè)類(lèi)通用于用于計(jì)算二階曲線
int x = (int) ((1 - t) * (1 - t) * startValue.x + 2 * t * (1 - t) * controllPoint.x + t * t * endValue.x);
int y = (int) ((1 - t) * (1 - t) * startValue.y + 2 * t * (1 - t) * controllPoint.y + t * t * endValue.y);
return new Point(x, y);//返回獲取path上的坐標(biāo)點(diǎn)
}
}
LikeStarActivity:(展示view)
public class LikeStarActivity extends AppCompatActivity implements View.OnTouchListener{
private LinearLayout mTouchView;
public static final String TAG = "LikeStarActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_like_star);
mTouchView = (LinearLayout) findViewById(R.id.touchview);
mTouchView.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
LikeStarView likeStarView = new LikeStarView(LikeStarActivity.this);
//獲取起始坐標(biāo)
int pointX = (int) event.getRawX();
int pointY = (int) event.getRawY();
likeStarView.setstartpoint(new Point(pointX,pointY));
//添加自定義view
ViewGroup rootView = (ViewGroup) LikeStarActivity.this.getWindow().getDecorView();
rootView.addView(likeStarView);
likeStarView.startanim();
break;
}
return super.onTouchEvent(event);
}
}
對(duì)它掂,就是這么簡(jiǎn)單巴帮。例子比較簡(jiǎn)單粗糙,只用于學(xué)習(xí)交流虐秋,實(shí)際使用還需要適配屏幕尺寸,添加隨機(jī)顏色愛(ài)心等榕茧。