android實現(xiàn)圖片閃爍動畫效果的兩種實現(xiàn)方式(實用性高)
大家在使用APP的時候,有的APP在點擊語音搜索界面后,會出現(xiàn)一個小話筒,小話筒會類似雷達似得在閃爍,表示正在傾聽你說話的內(nèi)容(這個大家可以參照微軟的必應(yīng)APP),那么問題來了,這種動畫效果是如何實現(xiàn)的呢?其實實現(xiàn)這種動畫效果有很多種方法,最常見的是兩種:第一種就是插入n張圖片進行切換已達到如此目的,第二種就是通過改變一張圖片的透明度來達到閃爍的效果。下面就分別講一下通過這兩種方法如何實現(xiàn)。
第一種:通過n張圖片之間切換實現(xiàn)動畫效果
這種方法的原理很簡單,利用handler的延時機制在子線程中完成圖片切換,再在主線程展示。
1、首先我們要先寫一個線程池,在使用的時候方便調(diào)用。
package com.jereh.musicapplication.threadpool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; /** * Created by zhangdi on 2016/9/1. * 這是一個線程池的工具類,在用到線程的時候可以直接類名加方法名使用 */ public class ThreadPoolManager { /** 線程執(zhí)行器 **/ private static ExecutorService executorService = null; /** 固定5個線程 **/ private static int nThreads = 5; /** 單例 **/ private static ThreadPoolManager taskExecutorPool = null; /** 初始化線程池 **/ static { taskExecutorPool = new ThreadPoolManager(nThreads * getNumCores()); } /** 構(gòu)造函數(shù) **/ private ThreadPoolManager(int threads) { //executorService = Executors.newFixedThreadPool(threads); executorService = Executors.newScheduledThreadPool(threads); } /** * 取得單例 * * @return */ public static ThreadPoolManager getInstance() { return taskExecutorPool; } /** * 取得線程執(zhí)行器 * * @return */ public ExecutorService getExecutorService() { return executorService; } /** * 取得周期性線程執(zhí)行器 * @return */ public ScheduledExecutorService getScheduledExcutorService(){ return (ScheduledExecutorService)executorService; } /** * 獲得手機cup個數(shù) * @return */ public static int getNumCores() { int threadCount = Runtime.getRuntime().availableProcessors(); return threadCount; } }
2、下一步就是在xml文件中插入一個布局
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fl"/>
3、然后就是在java代碼中編輯切換圖片了:
package com.jereh.musicapplication; import android.graphics.drawable.Drawable; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.FrameLayout; import com.jereh.musicapplication.threadpool.ThreadPoolManager; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit; public class FrameActivity extends AppCompatActivity { private Timer timer; FrameLayout frameLayout; Drawable drawable; android.os.Handler handler = new android.os.Handler(){ int i = 0; @Override public void handleMessage(Message msg) { if (msg.what==1){ i++; move(i%4); } super.handleMessage(msg); } }; void move(int i){ drawable = getResources().getDrawable(R.mipmap.ic_launcher,null); Drawable drawable1 = getResources().getDrawable(R.mipmap.dd1,null); Drawable drawable2 = getResources().getDrawable(R.mipmap.dd2,null); Drawable drawable3 = getResources().getDrawable(R.mipmap.dd3,null); switch (i){ case 0: frameLayout.setForeground(drawable); break; case 1: frameLayout.setForeground(drawable1); break; case 2: frameLayout.setForeground(drawable2); break; case 3: frameLayout.setForeground(drawable3); break; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_frame); frameLayout = (FrameLayout)findViewById(R.id.fl); timer = new Timer(); // timer.schedule(new TimerTask() { // @Override // public void run() { // handler.sendEmptyMessage(1); // } // },0,500);//第二個參數(shù)是隔多少秒之后開始顯示,第三個是隔多久顯示下一個 ThreadPoolManager .getInstance() .getScheduledExcutorService() .scheduleAtFixedRate(new Runnable() { @Override public void run() { handler.sendEmptyMessage(1); } },0,500, TimeUnit.MILLISECONDS);//第二個參數(shù)是隔多少秒之后開始顯示,第三個是隔多久顯示下一個 } @Override protected void onDestroy() { timer.cancel(); super.onDestroy(); } }
這里我寫了兩種方式,第一種是用Timer類來實現(xiàn),后來發(fā)現(xiàn)使用自定義的線程池更好,大家如果不想在定義一個線程池的話,可以直接使用Timer類來實現(xiàn)同樣的效果,至此使用第一種級n張圖片切換實現(xiàn)動畫效果的代碼就完成了。這種方式有一個弊端就是得需要n張圖片,那么要是只有單張圖片又該怎么辦呢,那么就可以使用下面這種方法了。
第二種:通過改變圖片透明度實現(xiàn)動畫效果
1、首先我們先封裝兩個動畫方法,第一個是從不透明到完全透明,第二個是完全透明到不透明
/** * 透明效果 * @return */ public Animation getAlphaAnimationIn() { //實例化 AlphaAnimation 主要是改變透明度 //透明度 從 1-不透明 0-完全透明 Animation animation = new AlphaAnimation(1.0f, 0); //設(shè)置動畫插值器 被用來修飾動畫效果,定義動畫的變化率 animation.setInterpolator(new DecelerateInterpolator()); //設(shè)置動畫執(zhí)行時間 animation.setDuration(2000); return animation; } public Animation getAlphaAnimationOut() { //實例化 AlphaAnimation 主要是改變透明度 //透明度 從 1-不透明 0-完全透明 Animation animation = new AlphaAnimation(0, 1.0f); //設(shè)置動畫插值器 被用來修飾動畫效果,定義動畫的變化率 animation.setInterpolator(new DecelerateInterpolator()); //設(shè)置動畫執(zhí)行時間 animation.setDuration(2000); return animation; }
2、分別給這兩個方法設(shè)置監(jiān)聽,即第一個動畫完成立刻執(zhí)行第二個動畫,第二個動畫完成在立刻執(zhí)行第一個動畫以實現(xiàn)動畫循環(huán)播放的效果
voiceState1.setAnimation(animationIn); voiceState1.setAnimation(animationOut); /** * 監(jiān)聽動畫實現(xiàn)動畫間的切換 */ animationOut.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { voiceState1.startAnimation(animationIn); } @Override public void onAnimationRepeat(Animation animation) { } }); animationIn.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { voiceState1.startAnimation(animationOut); } @Override public void onAnimationRepeat(Animation animation) { } });
至此使用一張圖片通過改變其透明度實現(xiàn)閃爍效果就完成了。
以上所述是小編給大家介紹的android實現(xiàn)圖片閃爍動畫效果的兩種實現(xiàn)方式(實用性高),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android Force Close 出現(xiàn)的異常原因分析及解決方法
本文給大家講解Android Force Close 出現(xiàn)的異常原因分析及解決方法,forceclose意為強行關(guān)閉,當(dāng)前應(yīng)用程序發(fā)生了沖突。對android force close異常分析感興趣的朋友一起通過本文學(xué)習(xí)吧2016-08-08Android 中okhttp自定義Interceptor(緩存攔截器)
這篇文章主要介紹了Android 中okhttp自定義Interceptor(緩存攔截器)的相關(guān)資料,需要的朋友可以參考下2017-03-03Android亮度調(diào)節(jié)的幾種實現(xiàn)方法
本篇文章詳細介紹了Android亮度調(diào)節(jié)的幾種實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-11-11Android自動編輯文本框(AutoCompleteTextView)使用方法詳解
這篇文章主要為大家詳細介紹了Android自動編輯文本框AutoCompleteTextView的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android組件必學(xué)之TabHost使用方法詳解
這篇文章主要為大家詳細介紹了Android組件中的TabHost組件使用方法,如何利用TabHost定義Tab標(biāo)簽樣式,感興趣的小伙伴們可以參考一下2016-05-05IDEA打包jar-解決找不到或無法加載主類 main的問題
這篇文章主要介紹了IDEA打包jar-解決找不到或無法加載主類 main的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Android中EditText光標(biāo)在4.0中的bug及解決方法
這篇文章主要介紹了Android中EditText光標(biāo)在4.0中的bug及解決方法,簡單分析了Android4.0版本中EditText光標(biāo)消息的原因及相應(yīng)的解決方法,需要的朋友可以參考下2016-01-01