亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android貝塞爾曲線實(shí)現(xiàn)直播點(diǎn)贊效果

 更新時(shí)間:2020年07月29日 12:50:48   作者:Hensen_  
這篇文章主要為大家詳細(xì)介紹了Android貝塞爾曲線實(shí)現(xiàn)直播點(diǎn)贊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)直播點(diǎn)贊效果的具體代碼,供大家參考,具體內(nèi)容如下

效果展示

原理分析

點(diǎn)贊效果最主要的難點(diǎn)和原理在于貝塞爾曲線動(dòng)畫的生成,我們通過圖片主要講解貝塞爾曲線動(dòng)畫

1、需要找到貝塞爾曲線的四個(gè)點(diǎn)
2、通過三級(jí)貝塞爾曲線的公式計(jì)算,獲取貝塞爾曲線的軌跡路徑點(diǎn)
3、通過設(shè)置點(diǎn)贊圖片X,Y坐標(biāo),從而形成點(diǎn)贊的效果

實(shí)現(xiàn)步驟

1、初始化變量

//1、繼承RelativeLayout
public class ChristmasView extends RelativeLayout implements View.OnClickListener {

 private Context context;
 //2、準(zhǔn)備幾張點(diǎn)贊圖片
 private int[] christmas_drawable = {R.drawable.christmas01, R.drawable.christmas02, R.drawable.christmas03
 , R.drawable.christmas04, R.drawable.christmas05, R.drawable.christmas06};
 //隨機(jī)數(shù)種子
 private Random random = new Random();
 //View的寬高
 private int width, height;
 //圖片的寬高
 private int drawableWidth, drawableHeight;

 public ChristmasView(Context context) {
 this(context, null);
 }

 public ChristmasView(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 }

 public ChristmasView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);

 this.context = context;
 //3、設(shè)置點(diǎn)擊事件
 setOnClickListener(this);
 //4、獲取點(diǎn)贊圖片的寬高
 Drawable drawable = ContextCompat.getDrawable(context, R.drawable.christmas01);
 drawableWidth = drawable.getIntrinsicWidth();
 drawableHeight = drawable.getIntrinsicHeight();
 }
}

@Override
public void onClick(View v) {
 //5、點(diǎn)擊增加點(diǎn)贊圖片
 addChristmas(context);
}

2、點(diǎn)贊效果的實(shí)現(xiàn)

private void addChristmas(Context context) {
 /**
 * 1、點(diǎn)擊一次增加一張圖片在底部
 */
 final ImageView imageView = new ImageView(context);
 imageView.setBackgroundResource(christmas_drawable[random.nextInt(christmas_drawable.length - 1)]);
 RelativeLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
 ViewGroup.LayoutParams.WRAP_CONTENT);
 params.addRule(ALIGN_PARENT_BOTTOM);
 params.addRule(CENTER_HORIZONTAL);
 imageView.setLayoutParams(params);
 addView(imageView);

 //2、開始執(zhí)行點(diǎn)贊效果
 AnimatorSet animatorSet = getAnimatorSet(imageView);
 animatorSet.addListener(new AnimatorListenerAdapter() {
 @Override
 public void onAnimationEnd(Animator animation) {
 //3、動(dòng)畫執(zhí)行后移除View
 removeView(imageView);
 }
 });
 animatorSet.start();
}

3、動(dòng)畫實(shí)現(xiàn)

private AnimatorSet getAnimatorSet(ImageView imageView) {
 AnimatorSet enter = new AnimatorSet();

 //1、縮放動(dòng)畫
 AnimatorSet scaleAnimator = new AnimatorSet();
 ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.3f, 1f);
 ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 0.3f, 1f);
 ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 0.3f, 1f);
 scaleAnimator.setDuration(300);
 scaleAnimator.playTogether(alpha, scaleX, scaleY);

 //2、貝塞爾動(dòng)畫
 ValueAnimator bezierAnimator = getBezierAnimator(imageView);

 //3、兩個(gè)動(dòng)畫按順序播放
 enter.playSequentially(scaleAnimator, bezierAnimator);
 return enter;
}

4、貝塞爾曲線動(dòng)畫

它需要一個(gè)估值器,不斷的計(jì)算它的運(yùn)行軌跡,從起始點(diǎn)到終點(diǎn)開始計(jì)算,當(dāng)中也需要中間另外的兩個(gè)點(diǎn)進(jìn)行輔助計(jì)算,這些都是由貝塞爾曲線的公式所決定的

/**
 * 貝塞爾曲線估值器:計(jì)算動(dòng)畫的執(zhí)行軌跡
 *
 * @params 傳入貝塞爾曲線需要的四個(gè)點(diǎn)
 * @return 通過計(jì)算返回貝塞爾曲線的坐標(biāo)
 */
public class BezierEvaluator implements TypeEvaluator<PointF> {

 private PointF point1;
 private PointF point2;

 public BezierEvaluator(PointF point1, PointF point2) {
 this.point1 = point1;
 this.point2 = point2;
 }

 @Override
 public PointF evaluate(float t, PointF point0, PointF point3) {
 PointF point = new PointF();
 //t 取值為 [0,1]

 /**
 * 三階貝塞爾公式
 *
 * B(t)=(1 - t)^3 P0
 * + 3 t (1 - t)^2 P1
 * + 3 t^2 (1 - t) P2
 * + t^3 P3
 */
 point.x = point0.x * (1 - t) * (1 - t) * (1 - t)
 + 3 * point1.x * t * (1 - t) * (1 - t)
 + 3 * point2.x * t * t * (1 - t)
 + point3.x * t * t * t;

 /**
 * 三階貝塞爾公式
 *
 * B(t)=(1 - t)^3 P0
 * + 3 t (1 - t)^2 P1
 * + 3 t^2 (1 - t) P2
 * + t^3 P3
 */
 point.y = point0.y * (1 - t) * (1 - t) * (1 - t)
 + 3 * point1.y * t * (1 - t) * (1 - t)
 + 3 * point2.y * t * t * (1 - t)
 + point3.y * t * t * t;

 return point;
 }
}

在不斷的計(jì)算過程中,我們就可以一直獲取它的軌跡點(diǎn),從而執(zhí)行我們的屬性動(dòng)畫,實(shí)現(xiàn)貝塞爾曲線動(dòng)畫

/**
 * 貝塞爾動(dòng)畫
 *
 * @return
 */
private ValueAnimator getBezierAnimator(final ImageView imageView) {
 //1、構(gòu)建貝塞爾曲線的四個(gè)點(diǎn)
 PointF point0 = new PointF((width - drawableWidth) / 2, height - drawableHeight);
 PointF point1 = new PointF(random.nextInt(width), random.nextInt(height / 2));
 PointF point2 = new PointF(random.nextInt(width), random.nextInt(height / 2) + height / 2);
 PointF point3 = new PointF(random.nextInt(width - drawableWidth), 0);

 //2、創(chuàng)建貝塞爾屬性動(dòng)畫
 BezierEvaluator evaluator = new BezierEvaluator(point1, point2);
 final ValueAnimator valueAnimator = ObjectAnimator.ofObject(evaluator, point0, point3);
 valueAnimator.setInterpolator(new LinearInterpolator());
 valueAnimator.setDuration(3000);
 //3、監(jiān)聽貝塞爾曲線估值器返回值
 valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
 @Override
 public void onAnimationUpdate(ValueAnimator animation) {
 //4、獲取BezierEvaluator中evaluate()返回的運(yùn)行軌跡坐標(biāo)點(diǎn),設(shè)置點(diǎn)贊圖片路線
 PointF pointF = (PointF) animation.getAnimatedValue();
 imageView.setX(pointF.x);
 imageView.setY(pointF.y);
 //6、獲取BezierEvaluator中evaluate()返回的參數(shù)t,設(shè)置消失動(dòng)畫
 float t = animation.getAnimatedFraction();
 imageView.setAlpha(1 - t + 0.2f);
 }
 });
 return valueAnimator;
}

5、View的使用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <com.handsome.boke2.CustomView.ChristmasView
 android:layout_width="100dp"
 android:layout_height="200dp"
 android:layout_alignParentBottom="true"
 android:layout_alignParentRight="true" />
</RelativeLayout>

6、源碼下載

Android貝塞爾曲線實(shí)現(xiàn)直播點(diǎn)贊效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論