雙擊飛小心心 仿抖音點贊

2021-08-23 14:26:01 字數 4584 閱讀 5068

自定義手勢識別

class myongesturelistener extends gesturedetector.******ongesturelistener else else {

y1 = (int)y;

if (mlove !=null) {

mlove.addloveview(x1, y1);

new乙個gesturedetector

gesturedetector = new gesturedetector(getcontext(), new myongesturelistener());

在ontouchevent時候使用自定義的gesturedetector 記得要 return true

@override

public boolean ontouchevent(motionevent event) {

gesturedetector.ontouchevent(event);

return true;

自定義relativelayout

public class love extends relativelayout {

private context context;

private layoutparams params;

private drawable icons = new drawable[4];

private interpolator interpolators = new interpolator[4];

private int mwidth;

private int mheight;

public love(context context, attributeset attrs) {

super(context, attrs);

this.context = context;

initview();

private void initview() {

// 資源

icons[0] = getresources().getdrawable(r.drawable.heart_red);

icons[1] = getresources().getdrawable(r.drawable.heart_red);

icons[2] = getresources().getdrawable(r.drawable.heart_red);

icons[3] = getresources().getdrawable(r.drawable.heart_red);

// 插值器

interpolators[0] = new acceleratedecelerateinterpolator(); // 在動畫開始與結束的地方速率改變比較慢,在中間的時候加速

interpolators[1] = new accelerateinterpolator();  // 在動畫開始的地方速率改變比較慢,然後開始加速

interpolators[2] = new decelerateinterpolator(); // 在動畫開始的地方快然後慢

interpolators[3] = new linearinterpolator();  // 以常量速率改變

public void addloveview(float x, float y) {

mwidth = (int) (x - 100);

mheight = (int) (y - 100);

final imageview iv = new imageview(context);

params = new layoutparams(200, 200);

iv.setlayoutparams(params);

iv.setimagedrawable(icons[new random().nextint(4)]);

addview(iv);

// 開啟動畫,並且用完銷毀

animatorset set = getanimatorset(iv);

set.start();

set.addlistener(new animatorlisteneradapter() {

@override

public void onanimationend(animator animation) {

// todo auto-generated method stub

super.onanimationend(animation);

removeview(iv);

* 獲取動畫集合

* @param iv

private animatorset getanimatorset(imageview iv) {

// 1.alpha動畫

objectanimator alpha = objectanimator.offloat(iv, "alpha", 0.3f, 1f);

// 2.縮放動畫

objectanimator scalex = objectanimator.offloat(iv, "scalex", 0.5f, 1.5f);

objectanimator scaley = objectanimator.offloat(iv, "scaley", 0.5f, 1.5f);

// 動畫集合

animatorset set = new animatorset();

set.playtogether(alpha, scalex, scaley);

set.setduration(2000);

// 貝塞爾曲線動畫

valueanimator bzier = getbzieranimator(iv);

animatorset set2 = new animatorset();

set2.playtogether(set, bzier);

set2.settarget(iv);

return set2;

* 貝塞爾動畫

private valueanimator getbzieranimator(final imageview iv) {

// todo auto-generated method stub

pointf pointfs = getpointfs(iv); // 4個點的座標

basevaluator evaluator = new basevaluator(pointfs[1], pointfs[2]);

valueanimator valueanim = valueanimator.ofobject(evaluator, pointfs[0], pointfs[3]);

valueanim.addupdatelistener(new valueanimator.animatorupdatelistener() {

@override

public void onanimationupdate(valueanimator animation) {

// todo auto-generated method stub

pointf p = (pointf) animation.getanimatedvalue();

iv.setx(p.x);

iv.sety(p.y);

iv.setalpha(1 - animation.getanimatedfraction()); // 透明度

valueanim.settarget(iv);

valueanim.setduration(2000);

valueanim.setinterpolator(interpolators[new random().nextint(4)]);

return valueanim;

private pointf getpointfs(imageview iv) {

// todo auto-generated method stub

pointf pointfs = new pointf[4];

pointfs[0] = new pointf(); // p0

pointfs[0].x = ((int) mwidth);

pointfs[0].y = mheight;

pointfs[1] = new pointf(); // p1

pointfs[1].x = new random().nextint(mwidth);

pointfs[1].y = new random().nextint(mheight / 2) + mheight / 2 + params.height;

pointfs[2] = new pointf(); // p2

pointfs[2].x = new random().nextint(mwidth);

pointfs[2].y = new random().nextint(mheight / 2);

pointfs[3] = new pointf(); // p3

pointfs[3].x = new random().nextint(mwidth);

pointfs[3].y = 0;

return pointfs;

仿抖音點贊效果

玩過抖音的人應該都知道抖音的點讚效果挺酷炫的,而作為碼農我們一定想知道它是怎麼實現的。先上效果圖 實現原理非常的簡單,直接上 created by csc on 2018 6 11.information 仿抖音點贊功能 class love context context relativelayo...

仿抖音點贊按鈕

每乙個圖形,都是通過一點點拼接到一起的,而每乙個動畫亦然,只需要將動畫和圖形進行拆解,就不難了。模仿下抖音點贊按鈕的動畫效果。拆解一下動畫效果。1 通過uibezierpath實現愛心的上半部分。let rect cgrect x 10,y 10,width frame.width 20,heigh...

React Native 仿抖音點贊特效

前言 任何一款應用無疑都或多或少的使用到動畫效果,它對於提公升使用者體驗有著無比重要的作用。react native同樣提供了豐富的動畫api供開發者呼叫,而對於此部分知識的掌握無疑是rn高階的必經之路,本文通過案例帶大家實踐掌握animated art等動畫及繪圖知識。animatedart手勢系...