欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動(dòng)效果

 更新時(shí)間:2020年03月23日 09:28:07   作者:GreatCoder726  
這篇文章主要介紹了Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動(dòng)效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

Android 點(diǎn)擊Button 實(shí)現(xiàn)震動(dòng)效果教程

Overview

在Android 的點(diǎn)擊效果中,遇到震動(dòng)效果的還是很多的。

接下來(lái)就讓我們看一下如何實(shí)現(xiàn)震動(dòng)效果。

所需要的權(quán)限

如果我們?cè)陂_發(fā)中需要使用到我們的震動(dòng),那么我們就需要申請(qǐng)一下權(quán)限:

<uses-permission android:name="android.permission.VIBRATE"/>

這樣我們的權(quán)限就申請(qǐng)好了。

我們震動(dòng)效果的幫助類

創(chuàng)建一個(gè)名為VibrateHelp的點(diǎn)擊震動(dòng)的幫助類。

然后看一下如何使用他的把:

public class VibrateHelp {
 private static Vibrator vibrator;
 /**
  * @ClassName:VibrateHelp - 簡(jiǎn)單的震動(dòng)
  * @author:CaoJiaHao
  * @Param:context 調(diào)用震動(dòng)類的 context
  * @param:millisecond 震動(dòng)的時(shí)間
  */
 @SuppressWarnings("static-access")
 public static void vSimple(Context context, int millisecode) {
  vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
  vibrator.vibrate(millisecode);
 }
 /**
  * @param : pattern 震動(dòng)的形式
  * @param : repeate 震動(dòng)循環(huán)的次數(shù)
  * @ClassName:VibrateHelp - 復(fù)雜的震動(dòng)
  * @author:CaoJiaHao
  * @Param: context 調(diào)用復(fù)雜震動(dòng)的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 - 停止震動(dòng)
  *@author:CaoJiaHao
  **/
 public static void stop() {
  if (vibrator != null)
   vibrator.cancel();
 }
}

這樣的話我們的 震動(dòng)幫助類就完成呢。

然后我們根據(jù)我們的源碼來(lái)分析一下:

我們需要將Vibrator 實(shí)例化一下。

然后我們創(chuàng)建我么你的簡(jiǎn)單的震動(dòng)模式。

接著創(chuàng)建我們比較復(fù)雜的震動(dòng)模式。

這樣我們的點(diǎn)擊震動(dòng)幫助類就完成了。

但是我們光有了幫助類是遠(yuǎn)遠(yuǎn)不夠的。我們還需要調(diào)用他才可以,不然我們的Helper Class 沒(méi)有任何作用。

封裝我們的震動(dòng)點(diǎn)擊事件

首先,我們創(chuàng)建一個(gè)類,讓他控制我們的點(diǎn)擊震動(dòng)效果。

我們創(chuàng)建一個(gè)名為ViewClickVibrate。然后先看一下源代碼:

public class ViewClickVibrate implements View.OnClickListener {
 private final int VIBRATE_TIME = 60;
 @Override
 public void onClick(View v) {
  VibrateHelp.vSimple(v.getContext(), VIBRATE_TIME);
 }
}

這個(gè)就是我們的源代碼,但是需要注意的是,我們封裝的這個(gè)類,需要去調(diào)用我們的View.OnClickListener的接口.

這樣我們的點(diǎn)擊效果算是全部完成了。

最后我們看一下如何實(shí)現(xiàn)他吧。

ImageCategory.setOnClickListener(new ViewClickVibrate() {
 public void onClick(View v) {
  super.onClick(v);
  Global.Go(FinanceActivity.this, CategoryActivity.class);
 }
});

這樣的一個(gè)點(diǎn)擊效果就完成了。

補(bǔ)充知識(shí):android控件實(shí)現(xiàn)抖動(dòng)的效果

這個(gè)程序的功能有可能在實(shí)際的開發(fā)中會(huì)用到,比如說(shuō)Button左右晃動(dòng),或者上下的晃動(dòng)效果,下面就給出代碼。

首先要定義一個(gè)xml文件,命名為shake

<?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" />


接下來(lái)再定義一個(gè)xml文件,命名為cycle_7

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" 
   android:cycles="2"  
   />

這兩個(gè)xml文件都要建在,res文件夾下面的anim文件中,如果沒(méi)有anim文件,可以自己建一個(gè)。

然后就是新建一個(gè)activity代碼如下

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);//加載動(dòng)畫資源文件
   findViewById(R.id.tv).startAnimation(shake); //給組件播放動(dòng)畫效果
  }
 
}

下面給出main.xml

<?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>

這樣就實(shí)現(xiàn)了一個(gè)edittext控件的抖動(dòng)效果,這里說(shuō)明一下cycle_7.xml文件中android:cycles="2" 這一項(xiàng)是設(shè)置抖動(dòng)的次數(shù)的,2為抖動(dòng)兩次。而shake.xml中

android:fromXDelta="0"
android:toXDelta="100"

是控制抖動(dòng)的范圍的,上面的代碼是在x軸進(jìn)行抖動(dòng),如果把x替換為y就是在y軸進(jìn)行抖動(dòng),當(dāng)然也可以在x,y軸同時(shí)抖動(dòng)。

以上這篇Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動(dòng)效果就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論