服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android编程实现仿iphone抖动效果的方法(附源码)

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

2021-04-14 15:20lee0oo0 Android

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

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

布局文件:

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

代码:

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

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

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

延伸 · 阅读

精彩推荐
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8342021-03-31
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4642021-03-09
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9602022-03-02
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810052021-05-03
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6192021-03-27
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4672021-01-03
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7452021-03-11