[Mobile Development] Android ripple animation effect implementation

Source: Internet
Author: User

Today, I implement the animation effect when searching in wifi. We usually use multiple images for Frame Animation. Here I useTween animation: Only one image is operated, and the Wi-Fi scan animation effect is realized. If you are interested, you can see it!

:

650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12091B910-0.png "title =" device-2013-12-01-180738.png "width =" 192 "height =" 320 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" width: 192px; height: 320px; "alt =" 180752274.png"/>


Here we use the custom Layout Method activity_main.xml

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <com. zhf. android_ripple.AnimationFrameLayout android: id = "@ + id/search_animation_wf_main" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </com. zhf. >>< Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentRight = "true" android: text = "enable ripple Animation"/> </RelativeLayout>


AnimationFrameLayout class:

Package com. zhf. android_ripple; import java. lang. ref. softReference; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. util. attributeSet; import android. util. log; import android. view. layoutInflater; import android. view. view; import android. view. animation. accelerateDecelerateInterpolator; import android. view. animation. alphaAnimation; import and Roid. view. animation. animation; import android. view. animation. animation. animationListener; import android. view. animation. animationSet; import android. view. animation. scaleAnimation; import android. widget. frameLayout; import android. widget. imageView; public class AnimationFrameLayout extends FrameLayout {private SoftReference <Bitmap> m_bitmapRipple; // soft reference of Ripple images) private ImageView [] m_imageVRadars; // ImageVi Ew array public AnimationFrameLayout (Context context) {super (context); init ();} public AnimationFrameLayout (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init ();} public AnimationFrameLayout (Context context, AttributeSet attrs) {super (context, attrs); init ();} /** initialize **/private void init () {loadRadarBitmap (); m_imageVRadars = new ImageView [3]; View v = Lay OutInflater. from (getContext ()). inflate (R. layout. wt_search_device_anima, this); m_imageVRadars [0] = (ImageView) v. findViewById (R. id. radar_ray_1); m_imageVRadars [1] = (ImageView) v. findViewById (R. id. radar_ray_2); m_imageVRadars [2] = (ImageView) v. findViewById (R. id. radar_ray_3);}/** load image **/private void loadRadarBitmap () {try {// obtain the ripple image m_bitmapRipple = new SoftReference <Bitmap> (BitmapFactory. decod EStream (getContext (). getResources (). openRawResource (R. drawable. wifi_body_ripple);} catch (Exception localException) {Log. e ("WTSearchAnimationFrameLayout", Log. getStackTraceString (localException);} catch (OutOfMemoryError localOutOfMemoryError) {Log. e ("WTSearchAnimationFrameLayout", Log. getStackTraceString (localOutOfMemoryError); System. gc (); // reclaim}/** reset, stop animation **/public void stopAnimatio N () {for (int I = 0; I <m_imageVRadars.length; ++ I) {if (m_bitmapRipple! = Null) {Bitmap localBitmap = m_bitmapRipple.get (); // obtain the object if (localBitmap! = Null &&! LocalBitmap. isRecycled () {// reclaim image resources localBitmap. recycle ();} m_bitmapRipple = null; ImageView localImageView = m_imageVRadars [I]; localImageView. setImageBitmap (null); // sets localImageView to null. setVisibility (View. GONE); localImageView. clearAnimation (); // cancel animation }}/ ** start animation **/public void startAnimation () {if (m_bitmapRipple = null) {loadRadarBitmap ();} for (int I = 0; I <m_imageVRadars.length; I ++) {ImageView localImageView; long ltime; while (true) {localImageView = m_imageVRadars [I]; localImageView. setImageBitmap (m_bitmapRipple.get (); // obtain the image localImageView. setVisibility (View. VISIBLE); // enlarge ltime = 333L * I; if (localImageView. getAnimation () = null) {break;} localImageView. getAnimation (). start ();} ScaleAnimation localScaleAnimation = new ScaleAnimation (1.0f, 14.0f, 1.0f, 14.0f, 1, 0.5f, 1, 0.5f); localScaleAnimation. setRepeatCount (-1); // The animation repeats AlphaAnimation animation = new AlphaAnimation (1.0f, 0.2f); AnimationSet localAnimationSet = new AnimationSet (true); // true: use the same accelerator localAnimationSet. addAnimation (localScaleAnimation); localAnimationSet. addAnimation (localAlphaAnimation); // Add the two animation effects. // set the localAnimationSet attribute. setDuration (1000L); // duration localAnimationSet. setFillEnabled (true); localAnimationSet. setFillBefore (true); // The control is kept before the animation starts. setStartOffset (ltime); // After the animation effect is postponed for ltime seconds, start localAnimationSet. setInterpolator (new AccelerateDecelerateInterpolator (); localAnimationSet. setAnimationListener (new MySearchAnimationHandler (this, localImageView); // bind the listener // set the animation set to localImageView. setAnimation (localAnimationSet); localImageView. startAnimation (localAnimationSet); // enable animation}/** animation listener class **/public class implements AnimationListener {private ImageView m_imageVRadar; public listener (incluparamimageview, ImageView m_imageVRadar) {super (); this. m_imageVRadar = m_imageVRadar;} @ Override public void onAnimationStart (Animation animation) {// TODO Auto-generated method stub} @ Override public void onAnimationEnd (Animation animation) {this. m_imageVRadar.setVisibility (View. GONE) ;}@ Override public void onAnimationRepeat (Animation animation) {animation. setStartOffset (0L );}}}

Important:

1. In this class, we use soft references for image operations to prevent OOM.

2. As for the animation, use ImageView [] to load the image in sequence and adjust the size and transparency of each ImageView.


Animation LayoutWt_search_device_anima.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <ImageView        android:id="@+id/radar_ray_1"        android:layout_width="70.0dip"        android:layout_height="70.0dip"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:layout_marginLeft="10.0dip" />    <ImageView        android:id="@+id/radar_ray_2"        android:layout_width="70.0dip"        android:layout_height="70.0dip"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:layout_marginLeft="10.0dip" />    <ImageView        android:id="@+id/radar_ray_3"        android:layout_width="70.0dip"        android:layout_height="70.0dip"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:layout_marginLeft="10.0dip" /></RelativeLayout>

Main Program Portal:

Package com. zhf. android_ripple; import android. OS. bundle; import android. app. activity; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity {public Button m_btn1; AnimationFrameLayout afl; // animation layout @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); afl = (AnimationFrameLayout) findViewById (R. id. search_animation_wf_main); // animation m_btn1 = (Button) findViewById (R. id. button1); m_btn1.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {afl. startAnimation () ;}}) ;}@ Override protected void onDestroy () {// TODO Auto-generated method stub super. onDestroy (); afl. stopAnimation ();}}


OK! Source code added! Try it!






This article from the "crazy Snail" blog, please be sure to keep this source http://smallwoniu.blog.51cto.com/3911954/1334425

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.