Android -- 自定義ViewGroup+貝塞爾+屬性動畫實現仿QQ點贊效果
阿新 • • 發佈:2018-12-30
private void init(final Context context) { mStarDrawable = new ArrayList<>(); mInterpolators = new ArrayList<>(); mStartPoint = new PointF(); mEndPoint = new PointF(); mControllPointOne = new PointF(); mControllPointTwo = new PointF(); //初始化圖片資源 mStarDrawable.add(getResources().getDrawable(R.mipmap.heart_red)); mStarDrawable.add(getResources().getDrawable(R.mipmap.heart_blue)); mStarDrawable.add(getResources().getDrawable(R.mipmap.heart_yellow)); mStarDrawable.add(getResources().getDrawable(R.mipmap.heart_green)); //初始化插補器 mInterpolators.add(new LinearInterpolator()); mInterpolators.add(new AccelerateDecelerateInterpolator()); mInterpolators.add(new AccelerateInterpolator()); mInterpolators.add(new DecelerateInterpolator()); ImageView image_heard = new ImageView(context); image_heard.setImageDrawable(mStarDrawable.get(0)); image_heard.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); image_heard.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //點選之後開始動畫,新增紅心到佈局檔案並開始動畫 final ImageView image_random = new ImageView(context); image_random.setImageDrawable(mStarDrawable.get(random.nextInt(4))); image_random.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); addView(image_random); invalidate(); //開始做動畫效果 BezierTypeEvaluator bezierTypeEvaluator = new BezierTypeEvaluator(mControllPointOne, mControllPointTwo); ValueAnimator valueAnimator = ValueAnimator.ofObject(bezierTypeEvaluator, mStartPoint, endPointRandom); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { PointF pointF = (PointF) animation.getAnimatedValue(); image_random.setX(pointF.x); image_random.setY(pointF.y); } }); valueAnimator.setDuration(2000); valueAnimator.start(); } }); addView(image_heard); }