欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android積分簽到上移消失動(dòng)畫效果

 更新時(shí)間:2018年05月14日 15:36:31   作者:帥氣大果果  
這篇文章主要介紹了Android積分簽到上移消失動(dòng)畫效果大致思路:動(dòng)畫部分,由一個(gè)垂直的平移和一個(gè)透明度變化的兩個(gè)動(dòng)畫組成。然后通過AnimationSet將兩個(gè)動(dòng)畫添加到集合,然后開始播放動(dòng)畫。

還記得以前在某云的時(shí)候,有次需求是一個(gè)積分簽到,要求點(diǎn)擊簽到按鈕然后有一個(gè)動(dòng)畫效果,比如+30積分然后慢慢往上移動(dòng)在消失。那會(huì)不會(huì)做就想著改下需求,直接去掉了動(dòng)畫效果,而今時(shí)隔很久又遇到同樣的問題,比較蛋疼的是我清楚記得當(dāng)時(shí)做過這個(gè)功能,但是自己沒有做出來,當(dāng)然現(xiàn)在做還是不會(huì)。自己當(dāng)年省寫的代碼含淚也要補(bǔ)上。這次吸取教訓(xùn),實(shí)現(xiàn)這個(gè)效果。

大致思路:動(dòng)畫部分,由一個(gè)垂直的平移和一個(gè)透明度變化的兩個(gè)動(dòng)畫組成。然后通過AnimationSet將兩個(gè)動(dòng)畫添加到集合,然后開始播放動(dòng)畫。

 更新UI部分,用的是Handler發(fā)送消息更新UI

下面看代碼:

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
 private ImageView mSignIn;
 private ImageView redDot;
 private TextView signSuccess;
 private AnimationSet set;
 private String isSign;
 private TextView textView;
 private Handler mHandler = new Handler() {
 private int i=100;
 public void handleMessage(android.os.Message msg) {
  switch (msg.what) {
  case 1:// 簽到
   i = i+100;
   mSignIn.setImageResource(R.drawable.icon_signed);//已簽到
   redDot.setVisibility(View.GONE);//圓點(diǎn)隱藏
   // start平移和漸變動(dòng)畫
   signSuccess.startAnimation(set);
   signSuccess.setVisibility(View.GONE);
   textView.setText("當(dāng)前積分:"+i);
   // mSignIn.setClickable(false);
   break;

  default:
   break;
  }
 }
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 mSignIn = (ImageView) findViewById(R.id.iv_sign);//簽到
 redDot = (ImageView) findViewById(R.id.iv_redpoint);//顯示未簽到的紅圓點(diǎn)
 textView = (TextView) findViewById(R.id.tv_score);//積分
 //簽到添加積分動(dòng)畫文本
 signSuccess = (TextView) findViewById(R.id.iv_sign_success);
 //  獲取簽到成功圖片的位置
 int left = signSuccess.getLeft();
 int top = signSuccess.getTop();
 // 創(chuàng)建平移和漸變的動(dòng)畫集合
 // 定義一個(gè)平移動(dòng)畫對(duì)象
 TranslateAnimation translate = new TranslateAnimation(left, left, top, top - 100);
 translate.setDuration(2000);
 //translate.setRepeatCount(1);
 // 漸變動(dòng)畫
 AlphaAnimation alpha = new AlphaAnimation(1, 0);
 alpha.setDuration(2000);
 alpha.setFillAfter(true);
 // 創(chuàng)建動(dòng)畫集合,將平移動(dòng)畫和漸變動(dòng)畫添加到集合中,一起start
 set = new AnimationSet(false);
 set.addAnimation(translate);
 set.addAnimation(alpha);
 }
 /**
 * 簽到
 * @param v
 */
 public void signIn(View v) {
   // if (!TextUtils.isEmpty(isSign)) {
    // if ("0".equals(isSign)) {// 0代表未簽到
    signSuccess.setVisibility(View.VISIBLE);
    // mHandler.sendEmptyMessage(1);
    Message message = new Message();
    message.what = 1;
    mHandler.sendMessage(message);
    // }
   // }
 }

}

其中

TranslateAnimation translate = new TranslateAnimation(left, left, top, top - 100);

接收四個(gè)參數(shù),我們點(diǎn)擊去看他的源碼

/**
 * Constructor to use when building a TranslateAnimation from code
 *
 * @param fromXDelta Change in X coordinate to apply at the start of the
 * animation
 * @param toXDelta Change in X coordinate to apply at the end of the
 * animation
 * @param fromYDelta Change in Y coordinate to apply at the start of the
 * animation
 * @param toYDelta Change in Y coordinate to apply at the end of the
 * animation
 */
 public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
 mFromXValue = fromXDelta;
 mToXValue = toXDelta;
 mFromYValue = fromYDelta;
 mToYValue = toYDelta;
 }

看到了TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta),從參數(shù)字面意思都能理解,在結(jié)合我們傳遞進(jìn)去的參數(shù),就是水平?jīng)]有變化,垂直位移。

AlphaAnimation alpha = new AlphaAnimation(1, 0);

是透明度變化1代表不透明,0代表完全透明,取值float

為了顯示效果這里可以多次點(diǎn)擊,實(shí)際項(xiàng)目中是點(diǎn)擊簽到一般是只能點(diǎn)擊一次。這個(gè)需要注意一下修改。

源碼

相關(guān)文章

  • flutter實(shí)現(xiàn)更新彈窗內(nèi)容例子(親測(cè)有效)

    flutter實(shí)現(xiàn)更新彈窗內(nèi)容例子(親測(cè)有效)

    Flutter是一款移動(dòng)應(yīng)用程序SDK,包含框架、widget和工具,這篇文章給大家介紹flutter實(shí)現(xiàn)更新彈窗內(nèi)容例子,親測(cè)可以使用,需要的朋友參考下吧
    2021-04-04
  • 最新評(píng)論