Android 点击Button 实现震动效果教程
Overview
在Android 的点击效果中,遇到震动效果的还是很多的。
接下来就让我们看一下如何实现震动效果。
所需要的权限
如果我们在开发中需要使用到我们的震动,那么我们就需要申请一下权限:
<uses-permission android:name="android.permission.VIBRATE"/>
这样我们的权限就申请好了。
我们震动效果的帮助类
创建一个名为VibrateHelp的点击震动的帮助类。
然后看一下如何使用他的把:
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
|
public class VibrateHelp { private static Vibrator vibrator; /** * @ClassName:VibrateHelp - 简单的震动 * @author:CaoJiaHao * @Param:context 调用震动类的 context * @param:millisecond 震动的时间 */ @SuppressWarnings ( "static-access" ) public static void vSimple(Context context, int millisecode) { vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE); vibrator.vibrate(millisecode); } /** * @param : pattern 震动的形式 * @param : repeate 震动循环的次数 * @ClassName:VibrateHelp - 复杂的震动 * @author:CaoJiaHao * @Param: context 调用复杂震动的context **/ @SuppressWarnings ( "static-access" ) public static void vComplicated(Context context, long [] pattern, int repeate) { vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE); vibrator.vibrate(pattern, repeate); } /** *@ClassName:VibrateHelp - 停止震动 *@author:CaoJiaHao **/ public static void stop() { if (vibrator != null ) vibrator.cancel(); } } |
这样的话我们的 震动帮助类就完成呢。
然后我们根据我们的源码来分析一下:
我们需要将Vibrator 实例化一下。
然后我们创建我么你的简单的震动模式。
接着创建我们比较复杂的震动模式。
这样我们的点击震动帮助类就完成了。
但是我们光有了帮助类是远远不够的。我们还需要调用他才可以,不然我们的Helper Class 没有任何作用。
封装我们的震动点击事件
首先,我们创建一个类,让他控制我们的点击震动效果。
我们创建一个名为ViewClickVibrate。然后先看一下源代码:
1
2
3
4
5
6
7
|
public class ViewClickVibrate implements View.OnClickListener { private final int VIBRATE_TIME = 60 ; @Override public void onClick(View v) { VibrateHelp.vSimple(v.getContext(), VIBRATE_TIME); } } |
这个就是我们的源代码,但是需要注意的是,我们封装的这个类,需要去调用我们的View.OnClickListener的接口.
这样我们的点击效果算是全部完成了。
最后我们看一下如何实现他吧。
1
2
3
4
5
6
|
ImageCategory.setOnClickListener( new ViewClickVibrate() { public void onClick(View v) { super .onClick(v); Global.Go(FinanceActivity. this , CategoryActivity. class ); } }); |
这样的一个点击效果就完成了。
补充知识:android控件实现抖动的效果
这个程序的功能有可能在实际的开发中会用到,比如说Button左右晃动,或者上下的晃动效果,下面就给出代码。
首先要定义一个xml文件,命名为shake
1
2
3
4
5
6
|
<? xml version = "1.0" encoding = "utf-8" ?> < translate xmlns:android = "http://schemas.android.com/apk/res/android" android:fromXDelta = "0" android:toXDelta = "100" android:duration = "1000" android:interpolator = "@anim/cycle_7" /> |
接下来再定义一个xml文件,命名为cycle_7
1
2
3
4
|
<? xml version = "1.0" encoding = "utf-8" ?> < cycleInterpolator xmlns:android = "http://schemas.android.com/apk/res/android" android:cycles = "2" /> |
这两个xml文件都要建在,res文件夹下面的anim文件中,如果没有anim文件,可以自己建一个。
然后就是新建一个activity代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); } public void go(View v){ Animation shake = AnimationUtils.loadAnimation( this , R.anim.shake); //加载动画资源文件 findViewById(R.id.tv).startAnimation(shake); //给组件播放动画效果 } } |
下面给出main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<? 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" android:gravity = "center_horizontal|center_vertical" > < EditText android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:id = "@+id/tv" android:text = "wojiuahiswo" /> < Button android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "go" android:onClick = "go" /> </ LinearLayout > |
这样就实现了一个edittext控件的抖动效果,这里说明一下cycle_7.xml文件中android:cycles="2" 这一项是设置抖动的次数的,2为抖动两次。而shake.xml中
android:fromXDelta="0"
android:toXDelta="100"
是控制抖动的范围的,上面的代码是在x轴进行抖动,如果把x替换为y就是在y轴进行抖动,当然也可以在x,y轴同时抖动。
以上这篇Android 实现为点击事件添加震动效果就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/cao-1/p/12095234.html