SoFunction
Updated on 2025-03-01

How to achieve mobile phone vibration and jitter effect on Android

Implementation of vibration and jitter effect of Android phone

(1) The layout file is as follows

<RelativeLayout xmlns:andro
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >
  <EditText
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="16dp"
    android:ems="10" >
    <requestFocus />
  </EditText>
  <Button
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/et_text"
    android:layout_below="@+id/et_text"
    android:layout_marginTop="38dp"
    android:text="submit" />
</RelativeLayout>

(2)

package .test11;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends Activity {
  private EditText et_text;
  private Button btn_submit;
  /**
    * Mobile phone vibrator
    */
  private Vibrator vibrator;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    et_text = (EditText) (.et_text);
    btn_submit = (Button) (.btn_submit);
    // System service for vibration effects    vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
    btn_submit.setOnClickListener(new OnClickListener() {
      String text = et_text.getText().toString().trim();
      public void onClick(View v) {
        if ((text)) {
          (, "Content is empty", 0).show();
          Animation shake = (
              , );
          et_text.startAnimation(shake);
          /*
            * The way of vibration
            */
          // (2000);//Vibrate for two seconds          // Below is a vibration that can make the vibration regular -1: means no repetition 0: cyclic vibration          long[] pattern = { 200, 2000, 2000, 200, 200, 200 };
          (pattern, -1);
        }
      }
    });
  }
}

(3) The two animation files used are as follows:

cycle_7.xml

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:andro
  android:cycles="7" />

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:andro
  android:duration="1000"
  android:fromXDelta="0"
  android:interpolator="@anim/cycle_7"
  android:toXDelta="10" />

(4) Permissions required:

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

This effect can generally be achieved only on real machines and is not displayed in the picture above.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links