本文實(shí)例為大家分享了Android實(shí)現(xiàn)直播點(diǎn)贊效果的具體代碼,供大家參考,具體內(nèi)容如下
十年的綿竹網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整綿竹建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“綿竹網(wǎng)站設(shè)計(jì)”,“綿竹網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
效果展示
原理分析
點(diǎn)贊效果最主要的難點(diǎn)和原理在于貝塞爾曲線動(dòng)畫的生成,我們通過(guò)圖片主要講解貝塞爾曲線動(dòng)畫
1、需要找到貝塞爾曲線的四個(gè)點(diǎn)
2、通過(guò)三級(jí)貝塞爾曲線的公式計(jì)算,獲取貝塞爾曲線的軌跡路徑點(diǎn)
3、通過(guò)設(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、開(kāi)始執(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)開(kāi)始計(jì)算,當(dāng)中也需要中間另外的兩個(gè)點(diǎn)進(jìn)行輔助計(jì)算,這些都是由貝塞爾曲線的公式所決定的
/** * 貝塞爾曲線估值器:計(jì)算動(dòng)畫的執(zhí)行軌跡 * * @params 傳入貝塞爾曲線需要的四個(gè)點(diǎn) * @return 通過(guò)計(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ì)算過(guò)程中,我們就可以一直獲取它的軌跡點(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)聽(tīng)貝塞爾曲線估值器返回值 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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享文章:Android貝塞爾曲線實(shí)現(xiàn)直播點(diǎn)贊效果
本文來(lái)源:http://www.rwnh.cn/article14/psggge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、面包屑導(dǎo)航、標(biāo)簽優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容