SoFunction
Updated on 2025-04-09

Android realizes the vibration effect of mobile phone

This article introduces the effect of Android to realize the vibration and jitter of mobile phones. It is shared with you for your reference. The specific content is as follows

(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="" />

I hope this article will be helpful to everyone to learn Android software programming.