Android implementation of the picture flashing animation effect of two ways (practical high) _android

Source: Internet
Author: User

When you're using the app, some apps click on the voice search interface, there will be a small microphone, the small microphone will be similar to the radar flashing, said to be listening to the content of your speech (this one can refer to Microsoft's Bing app), then the question, the animation effect is how to achieve it? In fact, there are many ways to achieve this animation effect, the most common is two: the first is to insert N pictures to switch has been achieved so, the second is by changing the transparency of a picture to achieve the flashing effect. Let's talk about how to do this in two ways.

The first: Through the N-picture switch to achieve animation effect

The principle of this method is very simple, using handler delay mechanism in the child thread to complete the picture switch, and then display in the main thread.

1, first we have to write a thread pool, in the use of convenient call.

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. * This is a thread pool of the tool class, when used in the thread can be directly class name plus method name using the/public class Threadpoolmanager {/** thread executor **/private static Executorservice execut
Orservice = null;
/** Fixed 5 threads **/private static int nthreads = 5;
/** Single example **/private static Threadpoolmanager taskexecutorpool = null; /** initialization thread pool **/static {Taskexecutorpool = new Threadpoolmanager (Nthreads * getnumcores ());/** Constructor **/Private THREADP Oolmanager (int threads) {//executorservice = Executors.newfixedthreadpool (threads); Executorservice =
Executors.newscheduledthreadpool (threads); /** * Obtained single example * * @return/public static Threadpoolmanager getinstance () {return taskexecutorpool;}/** * Get the thread executor * * return/Public Executorservice Getexecutorservice () {return executorservice;}/** * Get periodic thread executor * @return/public Sch EduledeXecutorservice Getscheduledexcutorservice () {return (Scheduledexecutorservice) Executorservice}/** * Get phone Cup number * return */public static int getnumcores () {int threadcount = Runtime.getruntime (). Availableprocessors (); return Threadcou
nt }
}

2, the next step is to insert a layout in the XML file

<framelayout
android:layout_width= "match_parent"
android:layout_height= "Match_parent"
android: Id= "@+id/fl"/>

3, then is in the Java code to edit the switch picture:

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 (msg) {if (M
Sg.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); /The second parameter is the number of seconds after the start of the display, and the third is how often the next Threadpoolmanager. getinstance (). Getscheduledexcutorservice (). scheduleatfixedrate (New Runnable () {@Override public void run () {handler.sendemptymessage (1);}},0,500, timeunit.milliseconds);//The second argument is multiple After less seconds to start showing, the third is how often to display the next} @Override protected void OnDestroy () {timer.cancel (); Super.ondestroy ();}}

Here I've written two ways, the first is to use the timer class to achieve, and later found that using a custom thread pool better, if you do not want to define a thread pool, you can directly use the Timer class to achieve the same effect, so that the first level of n-picture switching to achieve animation effect of the code is completed. The disadvantage of this approach is that you need n pictures, so if you have only one picture, then you can use the following method.

The second: animation effect by changing the transparency of the picture

  1, first we encapsulate two animation methods, the first is from opaque to completely transparent, the second is completely transparent to opaque

/**
* Transparent effect
* @return/public
Animation Getalphaanimationin () {
//instantiation alphaanimation mainly to change transparency
//Transparency from 1-opaque 0-Fully transparent
Animation Animation = new Alphaanimation (1.0f, 0);
Setting an animation interpolation is used to modify the animation effect, defining the change rate of the animation
Animation.setinterpolator (New Decelerateinterpolator ());
Set animation execution Time
(animation.setduration);
return animation;
}
Public Animation getalphaanimationout () {
//instantiation alphaanimation is primarily a change in transparency
/transparency from 1-opaque 0-Fully transparent
Animation Animation = new Alphaanimation (0, 1.0f);
Setting an animation interpolation is used to modify the animation effect, defining the change rate of the animation
Animation.setinterpolator (New Decelerateinterpolator ());
Set animation execution Time
(animation.setduration);
return animation;
}

2, respectively to the two methods to set up listening, that is, the first animation to complete the second animation immediately, the second animation completed in the immediate execution of the first animation to achieve animation loop playback effect

Voicestate1.setanimation (animationin);
Voicestate1.setanimation (animationout);
/**
* Monitor animation to achieve the switch between animation * *
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) {
}
});

This is accomplished by using a picture to change its transparency to achieve a flicker effect.

The above is a small set to introduce the Android animation effect of the implementation of the two ways (practical high), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.