Android仿Iphone图标抖动效果

来源:互联网 发布:折800淘宝入驻条件 编辑:程序博客网 时间:2024/05/20 14:28

http://blog.csdn.net/long33long/article/details/7693671

最近闲来无聊,研究了一下IPhone桌面图标的抖动,网上有一个类似的事例,但是我看来效果实在不佳,自己也来写一个玩玩,当然代码很乱,杂乱无章,不满意的别骂我啊,当然也欢迎一起交流啊,嚯嚯

首先是JAVA代码ShakeTestActivity.java

[java] view plaincopy
  1. package com.android.shake;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.DisplayMetrics;  
  6. import android.view.animation.Animation;  
  7. import android.view.animation.Animation.AnimationListener;  
  8. import android.view.animation.RotateAnimation;  
  9. import android.widget.TextView;  
  10. import android.view.View;  
  11. import android.view.Window;  
  12.   
  13. public class ShakeTestActivity extends Activity implements View.OnClickListener {  
  14.     private TextView mtv0;  
  15.     private TextView mtv1;  
  16.     private TextView mtv2;  
  17.     private TextView mtv3;  
  18.     private TextView mtv4;  
  19.     private TextView mtv5;  
  20.     private TextView mtv6;  
  21.     private TextView mtv7;  
  22.     private TextView mtv8;  
  23.     private TextView mtv9;  
  24.     private TextView mtv10;  
  25.     private TextView mtv11;  
  26.     private TextView mtv12;  
  27.     private TextView mtv13;  
  28.     private TextView mtv14;  
  29.     private TextView mtv15;  
  30.     private TextView mtv16;  
  31.     private TextView mtv17;  
  32.     private TextView mtv18;  
  33.     private TextView mtv19;  
  34.   
  35.     private boolean mNeedShake = false;  
  36.     private boolean mStartShake = false;  
  37.   
  38.     private static final int ICON_WIDTH = 80;  
  39.     private static final int ICON_HEIGHT = 94;  
  40.     private static final float DEGREE_0 = 1.8f;  
  41.     private static final float DEGREE_1 = -2.0f;  
  42.     private static final float DEGREE_2 = 2.0f;  
  43.     private static final float DEGREE_3 = -1.5f;  
  44.     private static final float DEGREE_4 = 1.5f;  
  45.     private static final int ANIMATION_DURATION = 80;  
  46.   
  47.     private int mCount = 0;  
  48.   
  49.     float mDensity;  
  50.   
  51.     /** Called when the activity is first created. */  
  52.     @Override  
  53.     public void onCreate(Bundle savedInstanceState) {  
  54.         super.onCreate(savedInstanceState);  
  55.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  56.         setContentView(R.layout.main);  
  57.         DisplayMetrics dm = new DisplayMetrics();  
  58.         getWindowManager().getDefaultDisplay().getMetrics(dm);  
  59.         if (dm != null) {  
  60.             mDensity = dm.density;  
  61.         }  
  62.         mtv0 = (TextView) findViewById(R.id.tv0);  
  63.         mtv0.setOnClickListener(this);  
  64.         mtv1 = (TextView) findViewById(R.id.tv1);  
  65.         mtv1.setOnClickListener(this);  
  66.         mtv2 = (TextView) findViewById(R.id.tv2);  
  67.         mtv2.setOnClickListener(this);  
  68.         mtv3 = (TextView) findViewById(R.id.tv3);  
  69.         mtv3.setOnClickListener(this);  
  70.         mtv4 = (TextView) findViewById(R.id.tv4);  
  71.         mtv4.setOnClickListener(this);  
  72.         mtv5 = (TextView) findViewById(R.id.tv5);  
  73.         mtv5.setOnClickListener(this);  
  74.         mtv6 = (TextView) findViewById(R.id.tv6);  
  75.         mtv6.setOnClickListener(this);  
  76.         mtv7 = (TextView) findViewById(R.id.tv7);  
  77.         mtv7.setOnClickListener(this);  
  78.         mtv8 = (TextView) findViewById(R.id.tv8);  
  79.         mtv8.setOnClickListener(this);  
  80.         mtv9 = (TextView) findViewById(R.id.tv9);  
  81.         mtv9.setOnClickListener(this);  
  82.         mtv10 = (TextView) findViewById(R.id.tv10);  
  83.         mtv10.setOnClickListener(this);  
  84.         mtv11 = (TextView) findViewById(R.id.tv11);  
  85.         mtv11.setOnClickListener(this);  
  86.         mtv12 = (TextView) findViewById(R.id.tv12);  
  87.         mtv12.setOnClickListener(this);  
  88.         mtv13 = (TextView) findViewById(R.id.tv13);  
  89.         mtv13.setOnClickListener(this);  
  90.         mtv14 = (TextView) findViewById(R.id.tv14);  
  91.         mtv14.setOnClickListener(this);  
  92.         mtv15 = (TextView) findViewById(R.id.tv15);  
  93.         mtv15.setOnClickListener(this);  
  94.         mtv16 = (TextView) findViewById(R.id.tv16);  
  95.         mtv16.setOnClickListener(this);  
  96.         mtv17 = (TextView) findViewById(R.id.tv17);  
  97.         mtv17.setOnClickListener(this);  
  98.         mtv18 = (TextView) findViewById(R.id.tv18);  
  99.         mtv18.setOnClickListener(this);  
  100.         mtv19 = (TextView) findViewById(R.id.tv19);  
  101.         mtv19.setOnClickListener(this);  
  102.   
  103.     }  
  104.   
  105.     @Override  
  106.     public void onClick(View v) {  
  107.   
  108.         if (!mStartShake) {  
  109.             mStartShake = true;  
  110.             mNeedShake = true;  
  111.             shakeAnimation(mtv0);  
  112.             shakeAnimation(mtv1);  
  113.             shakeAnimation(mtv2);  
  114.             shakeAnimation(mtv3);  
  115.             shakeAnimation(mtv4);  
  116.             shakeAnimation(mtv5);  
  117.             shakeAnimation(mtv6);  
  118.             shakeAnimation(mtv7);  
  119.             shakeAnimation(mtv8);  
  120.             shakeAnimation(mtv9);  
  121.             shakeAnimation(mtv10);  
  122.             shakeAnimation(mtv11);  
  123.             shakeAnimation(mtv12);  
  124.             shakeAnimation(mtv13);  
  125.             shakeAnimation(mtv14);  
  126.             shakeAnimation(mtv15);  
  127.             shakeAnimation(mtv16);  
  128.             shakeAnimation(mtv17);  
  129.             shakeAnimation(mtv18);  
  130.             shakeAnimation(mtv19);  
  131.         }  
  132.     }  
  133.   
  134.     private void shakeAnimation(final View v) {  
  135.         float rotate = 0;  
  136.         int c = mCount++ % 5;  
  137.         if (c == 0) {  
  138.             rotate = DEGREE_0;  
  139.         } else if (c == 1) {  
  140.             rotate = DEGREE_1;  
  141.         } else if (c == 2) {  
  142.             rotate = DEGREE_2;  
  143.         } else if (c == 3) {  
  144.             rotate = DEGREE_3;  
  145.         } else {  
  146.             rotate = DEGREE_4;  
  147.         }  
  148.         final RotateAnimation mra = new RotateAnimation(rotate, -rotate, ICON_WIDTH * mDensity / 2, ICON_HEIGHT * mDensity / 2);  
  149.         final RotateAnimation mrb = new RotateAnimation(-rotate, rotate, ICON_WIDTH * mDensity / 2, ICON_HEIGHT * mDensity / 2);  
  150.   
  151.         mra.setDuration(ANIMATION_DURATION);  
  152.         mrb.setDuration(ANIMATION_DURATION);  
  153.   
  154.         mra.setAnimationListener(new AnimationListener() {  
  155.             @Override  
  156.             public void onAnimationEnd(Animation animation) {  
  157.                 if (mNeedShake) {  
  158.                     mra.reset();  
  159.                     v.startAnimation(mrb);  
  160.                 }  
  161.             }  
  162.   
  163.             @Override  
  164.             public void onAnimationRepeat(Animation animation) {  
  165.   
  166.             }  
  167.   
  168.             @Override  
  169.             public void onAnimationStart(Animation animation) {  
  170.   
  171.             }  
  172.   
  173.         });  
  174.   
  175.         mrb.setAnimationListener(new AnimationListener() {  
  176.             @Override  
  177.             public void onAnimationEnd(Animation animation) {  
  178.                 if (mNeedShake) {  
  179.                     mrb.reset();  
  180.                     v.startAnimation(mra);  
  181.                 }  
  182.             }  
  183.   
  184.             @Override  
  185.             public void onAnimationRepeat(Animation animation) {  
  186.   
  187.             }  
  188.   
  189.             @Override  
  190.             public void onAnimationStart(Animation animation) {  
  191.   
  192.             }  
  193.   
  194.         });  
  195.         v.startAnimation(mra);  
  196.     }  
  197.   
  198.     @Override  
  199.     public void onBackPressed() {  
  200.         if (!mNeedShake) {  
  201.             super.onBackPressed();  
  202.         } else {  
  203.             mNeedShake = false;  
  204.             mCount = 0;  
  205.             mStartShake = false;  
  206.         }  
  207.     }  
  208. }  

然后是main.xml文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal">  
  11.         <TextView  
  12.             android:id="@+id/tv0"  
  13.             android:layout_width="80dip"  
  14.             android:layout_height="94dip"  
  15.             android:drawableTop="@drawable/ic_launcher"  
  16.             android:text="@string/hello" />  
  17.         <TextView  
  18.             android:id="@+id/tv1"  
  19.             android:layout_width="80dip"  
  20.             android:layout_height="94dip"  
  21.             android:drawableTop="@drawable/ic_launcher"  
  22.             android:text="@string/hello" />  
  23.         <TextView  
  24.             android:id="@+id/tv2"  
  25.             android:layout_width="80dip"  
  26.             android:layout_height="94dip"  
  27.             android:drawableTop="@drawable/ic_launcher"  
  28.             android:text="@string/hello" />  
  29.         <TextView  
  30.             android:id="@+id/tv3"  
  31.             android:layout_width="80dip"  
  32.             android:layout_height="94dip"  
  33.             android:drawableTop="@drawable/ic_launcher"  
  34.             android:text="@string/hello" />  
  35.     </LinearLayout>  
  36.       
  37.     <LinearLayout  
  38.         android:layout_width="fill_parent"  
  39.         android:layout_height="wrap_content"  
  40.         android:orientation="horizontal">  
  41.         <TextView  
  42.             android:id="@+id/tv4"  
  43.             android:layout_width="80dip"  
  44.             android:layout_height="94dip"  
  45.             android:drawableTop="@drawable/ic_launcher"  
  46.             android:text="@string/hello" />  
  47.         <TextView  
  48.             android:id="@+id/tv5"  
  49.             android:layout_width="80dip"  
  50.             android:layout_height="94dip"  
  51.             android:drawableTop="@drawable/ic_launcher"  
  52.             android:text="@string/hello" />  
  53.         <TextView  
  54.             android:id="@+id/tv6"  
  55.             android:layout_width="80dip"  
  56.             android:layout_height="94dip"  
  57.             android:drawableTop="@drawable/ic_launcher"  
  58.             android:text="@string/hello" />  
  59.         <TextView  
  60.             android:id="@+id/tv7"  
  61.             android:layout_width="80dip"  
  62.             android:layout_height="94dip"  
  63.             android:drawableTop="@drawable/ic_launcher"  
  64.             android:text="@string/hello" />  
  65.     </LinearLayout>  
  66.     <LinearLayout  
  67.         android:layout_width="fill_parent"  
  68.         android:layout_height="wrap_content"  
  69.         android:orientation="horizontal">  
  70.         <TextView  
  71.             android:id="@+id/tv8"  
  72.             android:layout_width="80dip"  
  73.             android:layout_height="94dip"  
  74.             android:drawableTop="@drawable/ic_launcher"  
  75.             android:text="@string/hello" />  
  76.         <TextView  
  77.             android:id="@+id/tv9"  
  78.             android:layout_width="80dip"  
  79.             android:layout_height="94dip"  
  80.             android:drawableTop="@drawable/ic_launcher"  
  81.             android:text="@string/hello" />  
  82.         <TextView  
  83.             android:id="@+id/tv10"  
  84.             android:layout_width="80dip"  
  85.             android:layout_height="94dip"  
  86.             android:drawableTop="@drawable/ic_launcher"  
  87.             android:text="@string/hello" />  
  88.         <TextView  
  89.             android:id="@+id/tv11"  
  90.             android:layout_width="80dip"  
  91.             android:layout_height="94dip"  
  92.             android:drawableTop="@drawable/ic_launcher"  
  93.             android:text="@string/hello" />  
  94.     </LinearLayout>  
  95.       
  96.     <LinearLayout  
  97.         android:layout_width="fill_parent"  
  98.         android:layout_height="wrap_content"  
  99.         android:orientation="horizontal">  
  100.         <TextView  
  101.             android:id="@+id/tv12"  
  102.             android:layout_width="80dip"  
  103.             android:layout_height="94dip"  
  104.             android:drawableTop="@drawable/ic_launcher"  
  105.             android:text="@string/hello" />  
  106.         <TextView  
  107.             android:id="@+id/tv13"  
  108.             android:layout_width="80dip"  
  109.             android:layout_height="94dip"  
  110.             android:drawableTop="@drawable/ic_launcher"  
  111.             android:text="@string/hello" />  
  112.         <TextView  
  113.             android:id="@+id/tv14"  
  114.             android:layout_width="80dip"  
  115.             android:layout_height="94dip"  
  116.             android:drawableTop="@drawable/ic_launcher"  
  117.             android:text="@string/hello" />  
  118.         <TextView  
  119.             android:id="@+id/tv15"  
  120.             android:layout_width="80dip"  
  121.             android:layout_height="94dip"  
  122.             android:drawableTop="@drawable/ic_launcher"  
  123.             android:text="@string/hello" />  
  124.     </LinearLayout>  
  125.       
  126.     <LinearLayout  
  127.         android:layout_width="fill_parent"  
  128.         android:layout_height="wrap_content"  
  129.         android:orientation="horizontal">  
  130.         <TextView  
  131.             android:id="@+id/tv16"  
  132.             android:layout_width="80dip"  
  133.             android:layout_height="94dip"  
  134.             android:drawableTop="@drawable/ic_launcher"  
  135.             android:text="@string/hello" />  
  136.         <TextView  
  137.             android:id="@+id/tv17"  
  138.             android:layout_width="80dip"  
  139.             android:layout_height="94dip"  
  140.             android:drawableTop="@drawable/ic_launcher"  
  141.             android:text="@string/hello" />  
  142.         <TextView  
  143.             android:id="@+id/tv18"  
  144.             android:layout_width="80dip"  
  145.             android:layout_height="94dip"  
  146.             android:drawableTop="@drawable/ic_launcher"  
  147.             android:text="@string/hello" />  
  148.         <TextView  
  149.             android:id="@+id/tv19"  
  150.             android:layout_width="80dip"  
  151.             android:layout_height="94dip"  
  152.             android:drawableTop="@drawable/ic_launcher"  
  153.             android:text="@string/hello" />  
  154.     </LinearLayout>  
  155.   
  156. </LinearLayout>  

string.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World</string>  
  4.     <string name="app_name">ShakeTest</string>  
  5. </resources>  


测试机的分辨率是320x480,640x960,其他分辨率显示效果不佳自行调整吧

附截图一张:



资源下载:http://download.csdn.net/detail/long33long/4395746


原创粉丝点击