Android仿抖音點(diǎn)擊效果

前言

學(xué)習(xí)自定義view,想找點(diǎn)東西耍一下,剛好看到抖音的點(diǎn)贊效果不錯(cuò)橱健,嘗試一下。

抖音效果:


抖音效果

話不多說(shuō)沙廉,先上代碼:

public class Love extends RelativeLayout {
    private Context mContext;
    float[] num = {-30, -20, 0, 20, 30};//隨機(jī)心形圖片角度

    public Love(Context context) {
        super(context);
        initView(context);
    }

    public Love(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public Love(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        ImageView imageView = new ImageView(mContext);
        LayoutParams params = new LayoutParams(100, 100);
        params.leftMargin = getWidth() - 200;
        params.topMargin = getHeight() / 2 - 300;
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));
        imageView.setLayoutParams(params);
        addView(imageView);

        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "這里是點(diǎn)擊愛心的動(dòng)畫拘荡,待展示", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        final ImageView imageView = new ImageView(mContext);
        LayoutParams params = new LayoutParams(300, 300);
        params.leftMargin = (int) event.getX() - 150;
        params.topMargin = (int) event.getY() - 300;
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));
        imageView.setLayoutParams(params);
        addView(imageView);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0))
                .with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0))
                .with(rotation(imageView, 0, 0, num[new Random().nextInt(4)]))
                .with(alpha(imageView, 0, 1, 100, 0))
                .with(scale(imageView, "scaleX", 0.9f, 1, 50, 150))
                .with(scale(imageView, "scaleY", 0.9f, 1, 50, 150))
                .with(translationY(imageView, 0, -600, 800, 400))
                .with(alpha(imageView, 1, 0, 300, 400))
                .with(scale(imageView, "scaleX", 1, 3f, 700, 400))
                .with(scale(imageView, "scaleY", 1, 3f, 700, 400));

        animatorSet.start();
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeViewInLayout(imageView);
            }
        });
        return super.onTouchEvent(event);
    }

    public static ObjectAnimator scale(View view, String propertyName, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , propertyName
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator translationX(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationX"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator translationY(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationY"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator alpha(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "alpha"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator rotation(View view, long time, long delayTime, float... values) {
        ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", values);
        rotation.setDuration(time);
        rotation.setStartDelay(delayTime);
        rotation.setInterpolator(new TimeInterpolator() {
            @Override
            public float getInterpolation(float input) {
                return input;
            }
        });
        return rotation;
    }
    }
實(shí)現(xiàn)思路

在點(diǎn)擊時(shí)觸發(fā)將心形的圖片add到整個(gè)view中,然后在執(zhí)行動(dòng)畫撬陵。主要的處理邏輯都在onTouchEvent()事件中珊皿,下面我們來(lái)詳細(xì)講解一下思路和代碼:

 @Override
    public boolean onTouchEvent(MotionEvent event) {

        final ImageView imageView = new ImageView(mContext);
        LayoutParams params = new LayoutParams(300, 300);
        params.leftMargin = (int) event.getX() - 150;
        params.topMargin = (int) event.getY() - 300;
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));
        imageView.setLayoutParams(params);
        addView(imageView);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0))
                .with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0))
                .with(rotation(imageView, 0, 0, num[new Random().nextInt(4)]))
                .with(alpha(imageView, 0, 1, 100, 0))
                .with(scale(imageView, "scaleX", 0.9f, 1, 50, 150))
                .with(scale(imageView, "scaleY", 0.9f, 1, 50, 150))
                .with(translationY(imageView, 0, -600, 800, 400))
                .with(alpha(imageView, 1, 0, 300, 400))
                .with(scale(imageView, "scaleX", 1, 3f, 700, 400))
                .with(scale(imageView, "scaleY", 1, 3f, 700, 400));

        animatorSet.start();
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeViewInLayout(imageView);
            }
        });
        return super.onTouchEvent(event);
    }
  • 首先,我們需要在觸摸事件中做監(jiān)聽巨税,當(dāng)有觸摸時(shí)蟋定,創(chuàng)建一個(gè)展示心形圖片的ImageView。
final ImageView imageView = new ImageView(mContext);
    imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));//設(shè)置紅色心形圖片
  • 設(shè)置圖片展示的位置草添,是需要在手指觸摸的位置上方驶兜,即觸摸點(diǎn)是心形的下方角的位置。所以我們需要將ImageView設(shè)置到手指的位置
 LayoutParams params = new LayoutParams(300, 300);
 params.leftMargin = (int) event.getX() - 150;
 params.topMargin = (int) event.getY() - 300;
 imageView.setLayoutParams(params);
  • 給imageView add到父view中远寸。
addView(imageView);
  • 設(shè)置imageView動(dòng)畫
 AnimatorSet animatorSet = new AnimatorSet();
 animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0))//縮放動(dòng)畫抄淑,X軸2倍縮小至0.9倍
                .with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0))//縮放動(dòng)畫,Y軸2倍縮小至0.9倍
                .with(rotation(imageView, 0, 0, num[new Random().nextInt(4)]))//旋轉(zhuǎn)動(dòng)畫驰后,隨機(jī)旋轉(zhuǎn)角度num={-30.-20肆资,0,20灶芝,30}
                .with(alpha(imageView, 0, 1, 100, 0))//漸變透明度動(dòng)畫迅耘,透明度從0-1.
                .with(scale(imageView, "scaleX", 0.9f, 1, 50, 150))//縮放動(dòng)畫贱枣,X軸0.9倍縮小至1倍
                .with(scale(imageView, "scaleY", 0.9f, 1, 50, 150))//縮放動(dòng)畫,Y軸0.9倍縮小至1倍
                .with(translationY(imageView, 0, -600, 800, 400))//平移動(dòng)畫颤专,Y軸從0向上移動(dòng)600單位
                .with(alpha(imageView, 1, 0, 300, 400))//透明度動(dòng)畫纽哥,從1-0
                .with(scale(imageView, "scaleX", 1, 3f, 700, 400))//縮放動(dòng)畫,X軸1倍放大至3倍
                .with(scale(imageView, "scaleY", 1, 3f, 700, 400));//縮放動(dòng)畫栖秕,Y軸1倍放大至3倍
animatorSet.start();
  • 當(dāng)然春塌,我們不可能無(wú)限制的增加view,在view消失之后簇捍,需要手動(dòng)的移除改ImageView只壳。
animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeViewInLayout(imageView);
            }
        });

效果如下:


這里寫圖片描述

右側(cè)愛心點(diǎn)贊的效果容我先搞出來(lái),下期在上咯暑塑!


有問(wèn)題和想法吼句,歡迎私信或者評(píng)論留言,一起學(xué)習(xí)事格,一起進(jìn)步

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末惕艳,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子驹愚,更是在濱河造成了極大的恐慌远搪,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件逢捺,死亡現(xiàn)場(chǎng)離奇詭異谁鳍,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)劫瞳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門倘潜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人志于,你說(shuō)我怎么就攤上這事窍荧。” “怎么了恨憎?”我有些...
    開封第一講書人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵蕊退,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我憔恳,道長(zhǎng)瓤荔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任钥组,我火速辦了婚禮输硝,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘程梦。我一直安慰自己点把,他們只是感情好橘荠,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著郎逃,像睡著了一般哥童。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上褒翰,一...
    開封第一講書人閱讀 51,562評(píng)論 1 305
  • 那天贮懈,我揣著相機(jī)與錄音,去河邊找鬼优训。 笑死朵你,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的揣非。 我是一名探鬼主播抡医,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼早敬!你這毒婦竟也來(lái)了忌傻?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤搁嗓,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后箱靴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體腺逛,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年衡怀,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了棍矛。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡抛杨,死狀恐怖够委,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情怖现,我是刑警寧澤茁帽,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站屈嗤,受9級(jí)特大地震影響潘拨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜饶号,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一铁追、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧茫船,春花似錦琅束、人聲如沸扭屁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)料滥。三九已至,卻和暖如春埋泵,著一層夾襖步出監(jiān)牢的瞬間幔欧,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工丽声, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留礁蔗,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓雁社,卻偏偏與公主長(zhǎng)得像浴井,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子霉撵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,139評(píng)論 25 707
  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 29,386評(píng)論 8 265
  • 重點(diǎn)參考鏈接: View Programming Guide for iOS https://developer....
    Kevin_Junbaozi閱讀 4,443評(píng)論 0 15
  • 狗有靈性磺浙,人對(duì)狗的文化偏愛有加,狗年拾趣收集之徒坡。 1撕氧、狗在跳高上有一技之長(zhǎng),因此狗急能跳墻喇完。 2伦泥、狗有見義勇為的美...
    Wangyifang閱讀 157評(píng)論 0 0
  • 知道你住院的那一刻不脯,我心里很難受。眼淚也不自覺(jué)的留了下來(lái)刻诊。在你看來(lái)我就是這么沒(méi)用防楷,這么愛哭≡蜓模可是不會(huì)表達(dá)的我复局,只能...
    蘋果味小仙女閱讀 165評(píng)論 0 0