android實現圖片閃爍動畫效果的兩種實現方式(實用性高)_Android

來源:互聯網
上載者:User

大家在使用APP的時候,有的APP在點擊語音搜尋介面後,會出現一個小話筒,小話筒會類似雷達似得在閃爍,表示正在傾聽你說話的內容(這個大家可以參照微軟的必應APP),那麼問題來了,這種動畫效果是如何?的呢?其實實現這種動畫效果有很多種方法,最常見的是兩種:第一種就是插入n張圖片進行切換已達到如此目的,第二種就是通過改變一張圖片的透明度來達到閃爍的效果。下面就分別講一下通過這兩種方法如何?。

第一種:通過n張圖片之間切換實現動畫效果

  這種方法的原理很簡單,利用handler的延時機制在子線程中完成圖片切換,再在主線程展示。

  1、首先我們要先寫一個線程池,在使用的時候方便調用。

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 {/** 線程執行器 **/private static ExecutorService executorService = null;/** 固定5個線程 **/private static int nThreads = 5;/** 單例 **/private static ThreadPoolManager taskExecutorPool = null;/** 初始化線程池 **/static {taskExecutorPool = new ThreadPoolManager(nThreads * getNumCores());}/** 建構函式 **/private ThreadPoolManager(int threads) {//executorService = Executors.newFixedThreadPool(threads);executorService = Executors.newScheduledThreadPool(threads);}/*** 取得單例** @return*/public static ThreadPoolManager getInstance() {return taskExecutorPool;}/*** 取得線程執行器** @return*/public ExecutorService getExecutorService() {return executorService;}/*** 取得周期性線程執行器* @return*/public ScheduledExecutorService getScheduledExcutorService(){return (ScheduledExecutorService)executorService;}/*** 獲得手機cup個數* @return*/public static int getNumCores() {int threadCount = Runtime.getRuntime().availableProcessors();return threadCount;}}

2、下一步就是在xml檔案中插入一個布局

<FrameLayoutandroid: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;@Overridepublic 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;}}@Overrideprotected 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);//第二個參數是隔多少秒之後開始顯示,第三個是隔多久顯示下一個ThreadPoolManager.getInstance().getScheduledExcutorService().scheduleAtFixedRate(new Runnable() {@Overridepublic void run() {handler.sendEmptyMessage(1);}},0,500, TimeUnit.MILLISECONDS);//第二個參數是隔多少秒之後開始顯示,第三個是隔多久顯示下一個}@Overrideprotected void onDestroy() {timer.cancel();super.onDestroy();}}

這裡我寫了兩種方式,第一種是用Timer類來實現,後來發現使用自訂的線程池更好,大家如果不想在定義一個線程池的話,可以直接使用Timer類來實現同樣的效果,至此使用第一種級n張圖片切換實現動畫效果的代碼就完成了。這種方式有一個弊端就是得需要n張圖片,那麼要是只有單張圖片又該怎麼辦呢,那麼就可以使用下面這種方法了。

第二種:通過改變圖片透明度實現動畫效果

  1、首先我們先封裝兩個動畫方法,第一個是從不透明到完全透明,第二個是完全透明到不透明

/*** 透明效果* @return*/public Animation getAlphaAnimationIn() {//執行個體化 AlphaAnimation 主要是改變透明度//透明度 從 1-不透明 0-完全透明Animation animation = new AlphaAnimation(1.0f, 0);//設定動畫插值器 被用來修飾動畫效果,定義動畫的變動率animation.setInterpolator(new DecelerateInterpolator());//設定動畫執行時間animation.setDuration(2000);return animation;}public Animation getAlphaAnimationOut() {//執行個體化 AlphaAnimation 主要是改變透明度//透明度 從 1-不透明 0-完全透明Animation animation = new AlphaAnimation(0, 1.0f);//設定動畫插值器 被用來修飾動畫效果,定義動畫的變動率animation.setInterpolator(new DecelerateInterpolator());//設定動畫執行時間animation.setDuration(2000);return animation;}

2、分別給這兩個方法設定監聽,即第一個動畫完成立刻執行第二個動畫,第二個動畫完成在立刻執行第一個動畫以實現動畫迴圈播放的效果

voiceState1.setAnimation(animationIn);voiceState1.setAnimation(animationOut);/*** 監聽動畫實現動畫間的切換*/animationOut.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {voiceState1.startAnimation(animationIn);}@Overridepublic void onAnimationRepeat(Animation animation) {}});animationIn.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {voiceState1.startAnimation(animationOut);}@Overridepublic void onAnimationRepeat(Animation animation) {}});

至此使用一張圖片通過改變其透明度實現閃爍效果就完成了。

以上所述是小編給大家介紹的android實現圖片閃爍動畫效果的兩種實現方式(實用性高),希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.