首页 > 系统 > Android > 正文

Android编程实现仿iphone抖动效果的方法(附源码下载)

2019-10-24 20:34:03
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Android编程实现仿iphone抖动效果的方法,结合实例形式分析了仿iphone抖动效果的页面布局及功能实现技巧,并附带实例源码供读者下载,需要的朋友可以参考下

本文实例讲述了Android编程实现仿iphone抖动效果的方法。分享给大家供大家参考,具体如下:

布局文件:

 

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

代码:

 

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

完整实例代码代码点击此处本站下载。

希望本文所述对大家Android程序设计有所帮助。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表