Android implements image Flash Animation and Android image Flash Animation

Source: Internet
Author: User

Android implements image Flash Animation and Android image Flash Animation

When you use an APP, some apps will have a small microphone after you click the voice search interface. The microphone will flash like a radar, it means you are listening to what you are talking about (you can refer to Microsoft's Bing APP). The question is, how can this animation effect be achieved? In fact, there are many ways to achieve this animation effect, the most common of which is two: the first is to insert n images to switch to achieve this goal, the second is to change the transparency of an image to achieve the flickering effect. The following describes how to implement these two methods.

First: Achieve the animation effect by switching between n images

The principle of this method is very simple. The handler delay mechanism is used to complete the image switching in the Child thread, and then displayed in the main thread.

1. First, we need to write a thread pool for convenient calling.

1 package com. jereh. musicapplication. threadpool; 2 3 import java. util. concurrent. executorService; 4 import java. util. concurrent. executors; 5 import java. util. concurrent. scheduledExecutorService; 6 7/** 8 * Created by zhangdi on 2016/9/1. 9 * This is a tool class for a thread pool, when using a thread, you can directly add the class name and method name using 10 */11 public class ThreadPoolManager {12/** thread executor **/13 private static ExecutorService executorService = null; 14/** fixed five threads **/15 private static int nThreads = 5; 16/** Singleton **/17 private static ThreadPoolManager taskExecutorPool = null; 18 19 20 21/** initialize thread pool **/22 static {23 taskExecutorPool = new ThreadPoolManager (nThreads * getNumCores ()); 24} 25 26/** constructor **/27 private ThreadPoolManager (int threads) {28 // executorService = Executors. newFixedThreadPool (threads); 29 executorService = Executors. newScheduledThreadPool (threads); 30} 31 32/** 33 * obtain Singleton 34*35 * @ return36 */37 public static ThreadPoolManager getInstance () {38 return taskExecutorPool; 39} 40 41/** 42 * Get thread executor 43*44 * @ return45 */46 public ExecutorService getExecutorService () {47 return executorService; 48} 49 50/** 51 * Get the periodical thread executor 52 * @ return53 */54 public ScheduledExecutorService getScheduledExcutorService () {55 return (ScheduledExecutorService) executorService; 56} 57 58/** 59 * Get the number of cell phone cups 60 * @ return61 */62 public static int getNumCores () {63 int threadCount = Runtime. getRuntime (). availableProcessors (); 64 return threadCount; 65} 66 67}

2. The next step is to insert a layout in the xml file.

1 <FrameLayout2         android:layout_width="match_parent"3         android:layout_height="match_parent"4         android:id="@+id/fl"/>

3. Edit the switching image in the java code:

1 package com. jereh. musicapplication; 2 3 import android. graphics. drawable. drawable; 4 import android. OS. message; 5 import android. support. v7.app. appCompatActivity; 6 import android. OS. bundle; 7 import android. widget. frameLayout; 8 9 import com. jereh. musicapplication. threadpool. threadPoolManager; 10 11 import java. util. timer; 12 import java. util. timerTask; 13 import java. util. concurrent. timeUnit; 14 15 public class FrameActivity extends AppCompatActivity {16 17 private Timer timer; 18 FrameLayout frameLayout; 19 Drawable drawable; 20 android. OS. handler handler = new android. OS. handler () {21 int I = 0; 22 @ Override23 public void handleMessage (Message msg) {24 if (msg. what = 1) {25 I ++; 26 move (I % 4); 27} 28 super. handleMessage (msg); 29} 30}; 31 void move (int I) {32 drawable = getResources (). getDrawable (R. mipmap. ic_launcher, null); 33 Drawable drawable1 = getResources (). getDrawable (R. mipmap. dd1, null); 34 Drawable drawable2 = getResources (). getDrawable (R. mipmap. dd2, null); 35 Drawable drawable3 = getResources (). getDrawable (R. mipmap. dd3, null); 36 switch (I) {37 case 0: 38 frameLayout. setForeground (drawable); 39 break; 40 case 1: 41 frameLayout. setForeground (drawable1); 42 break; 43 case 2: 44 frameLayout. setForeground (drawable2); 45 break; 46 case :47 frameLayout. setForeground (drawable3); 48 break; 49} 50} 51 @ Override52 protected void onCreate (Bundle savedInstanceState) {53 super. onCreate (savedInstanceState); 54 setContentView (R. layout. activity_frame); 55 frameLayout = (FrameLayout) findViewById (R. id. fl); 56 timer = new Timer (); 57 // timer. schedule (new TimerTask () {58 // @ Override59 // public void run () {60 // handler. sendEmptyMessage (1); 61 //} 62 //}, 0,500); // The second parameter is displayed after several seconds, the third is how long the next 63 64 ThreadPoolManager65 will be displayed. getInstance () 66. getScheduledExcutorService () 67. scheduleAtFixedRate (new Runnable () {68 @ Override69 public void run () {70 handler. sendEmptyMessage (1); 71} 72 }, 0,500, TimeUnit. MILLISECONDS); // The second parameter is displayed every second, and the third parameter is the next 73} 74 75 76 @ Override77 protected void onDestroy () {78 timer. cancel (); 79 super. onDestroy (); 80 81} 82}

Here I have written two methods. The first method is to use the Timer class for implementation. Later I found that it is better to use a custom thread pool. If you do not want to define a thread pool, you can directly use the Timer class to achieve the same effect. Now you can use the first class of n images to switch the code to achieve the animation effect. One drawback of this method is that we need n images. What should we do if we only have one image? Then we can use the following method.

Second: Achieve the animation effect by changing the image transparency

1. First, we encapsulate two animation methods. The first is from transparent to completely transparent, and the second is from completely transparent to transparent.

1/** 2 * transparent effect 3 * @ return 4 */5 public Animation getAlphaAnimationIn () {6 // instantiate AlphaAnimation mainly to change transparency 7 // transparency from 1-Opacity 0-completely transparent 8 Animation animation = new AlphaAnimation (1.0f, 0 ); 9 // set the animation interpolation to modify the animation effect, and define the animation's change rate of 10 animation. setInterpolator (new DecelerateInterpolator (); 11 // set the animation execution time to 12 animation. setDuration (2000); 13 return animation; 14} 15 public Animation getAlphaAnimationOut () {16 // instantiate AlphaAnimation mainly to change transparency 17 // transparency from 1-Opacity 0-completely transparent 18 Animation animation = new AlphaAnimation (0, 1.0f ); 19 // set the animation interpolation to modify the animation effect, and define the animation's Change Rate 20 animation. setInterpolator (new DecelerateInterpolator (); 21 // set the animation execution time to 22 animation. setDuration (2000); 23 return animation; 24}

2. Set the listener for the two methods respectively, that is, the first animation completes the second animation immediately, and the second animation completes the first animation immediately to achieve the animation loop playback effect.

1 voiceState1.setAnimation (animationIn); 2 voiceState1.setAnimation (animationOut); 3/** 4 * Listen to the animation to switch between animations 5 */6 animationOut. setAnimationListener (new Animation. animationListener () {7 @ Override 8 public void onAnimationStart (Animation animation) {9 10} 11 12 @ Override13 public void onAnimationEnd (Animation animation) {14 seconds (animationIn ); 15} 16 17 @ Override18 public void onAnimationRepeat (Animation animation) {19 20} 21}); 22 animationIn. setAnimationListener (new Animation. animationListener () {23 @ Override24 public void onAnimationStart (Animation animation) {25 26} 27 28 @ Override29 public void onAnimationEnd (Animation animation) {30 seconds (animationOut ); 31} 32 33 @ Override34 public void onAnimationRepeat (Animation animation) {35 36} 37 });

So far, you can use an image to achieve the flickering effect by changing its transparency.

These two methods are very practical in implementing the animation flash effect. I hope they can help you learn. If you feel that this article is not bad, I would like to recommend it to you, if I feel that I am not writing well somewhere, I hope you can comment and correct it. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.