Android Implementation Adds Vibration Effect to Click Events

  • 2021-11-24 03:00:34
  • OfStack

Android Click Button to achieve vibration effect tutorial

Overview

In the click effect of Android, there are still many vibration effects.

Next, let's look at how to achieve the vibration effect.

Required permissions

If we need to use our vibration in development, then we need to apply for 1 permissions:

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

In this way, our permission can be applied.

Our help class for vibration effect

Create a click vibration helper class named VibrateHelp.

Then see how to use his handle:


public class VibrateHelp {
 private static Vibrator vibrator;
 /**
  * @ClassName:VibrateHelp -  Simple vibration 
  * @author:CaoJiaHao
  * @Param:context  Object that calls the vibrating class  context
  * @param:millisecond  Time of vibration 
  */
 @SuppressWarnings("static-access")
 public static void vSimple(Context context, int millisecode) {
  vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
  vibrator.vibrate(millisecode);
 }
 /**
  * @param : pattern  The form of vibration 
  * @param : repeate  Number of vibration cycles 
  * @ClassName:VibrateHelp -  Complex vibration 
  * @author:CaoJiaHao
  * @Param: context  Object that calls complex vibrations 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 -  Stop vibrating 
  *@author:CaoJiaHao
  **/
 public static void stop() {
  if (vibrator != null)
   vibrator.cancel();
 }
}

In this case, our vibration helper class will be completed.

Then we analyze 1 according to our source code:

We need to instantiate Vibrator under 1.

Then we create our simple vibration mode.

Then create our more complex vibration mode.

In this way, our click vibration helper class is completed.

But it is not enough for us to have help classes. We still need to call him, otherwise our Helper Class has no effect.

Encapsulate our vibrating click event

First, we create a class and let it control our click vibration effect.

We created one named ViewClickVibrate. Then look at the source code first:


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

This is our source code, but it should be noted that the class we encapsulate needs to call our View. OnClickListener interface.

In this way, our click effect is completely completed.

Finally, let's see how to realize it.


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

This one-click effect is completed.

Additional knowledge: android controls implement the effect of jitter

The function of this program may be used in actual development, for example, Button shaking left and right, or shaking effect up and down, the following code is given.

First, define an xml file named 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" />


Next, define another xml file named cycle_7


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

These two xml files should be built in the anim file under the res folder. If there is no anim file, you can build one by yourself.

Then there is a new activity code as follows


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);// Load animation resource file 
   findViewById(R.id.tv).startAnimation(shake); // Play animation effects to components 
  }
 
}

main. xml is given below


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

In this way, the jitter effect of an edittext control is realized. It is explained here that android: cycles= "2" in cycle_7. xml file under 1 is to set the number of jitter, and 2 is to jitter twice. And in shake. xml

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

It controls the range of jitter. The above code is jitter on x axis. If x is replaced by y, it is jitter on y axis. Of course, it can also jitter on x and y axes at the same time.


Related articles: