Shake the text

Res defines an animation in an anim file

shake.xml

<? xml version="1.0" encoding="utf-8"? > <translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="10"
    android:fromYDelta="0"
    android:interpolator="@anim/shake_inter">
</translate>
Copy the code
  • Android :duration= “1000” : animation time
  • Android :fromXDelta: X axis moved left and right 10
  • Android :fromYDelta= “0” : move the Y axis up or down by 0
  • Android :interpolator=”@anim/shake_inter” :interpolator

Shake_inter, XML interpolator contents:

<? xml version="1.0" encoding="utf-8"? > <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:cycles="Seven"
    ></cycleInterpolator>
Copy the code
  • Android: Cycles = “7” : indicates the number of cycles

Code use:

 Animation animation = AnimationUtils.loadAnimation(this, R.anim.shake);
tv.startAnimation(animation);
Copy the code

The phone shook

The phone zhenyizhen comes with Android;

Add shock permission:

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

Use:

// Vibration effect system service
Vibrator mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

// Time mode of vibration 1:
mVibrator.vibrate(2000);// Vibrate for two seconds

// Vibration time mode 2:
        /** * For pattern, if defined as new int[200,400,600,800] ** 200: indicates the number of milliseconds to wait before opening the vibrator. 0.2 seconds. * 400: Represents the number of milliseconds to keep the vibrator on before turning it off, 0.4 seconds. * 600,800: Alternate between a few milliseconds to turn off the vibrator or turn on the vibrator, 0.6-0.8 seconds. * /
        // Continuous vibration 3 times
       / / long [] the pattern = {600800600800600800};
        // Fast vibration 3 times
       long[] pattern = {200.400.200.400.200.400};
       mVibrator.vibrate(pattern, -1);
Copy the code

Other shaking methods:

  • mVibrator.cancel(); Turn off/stop the vibration
  • Mvibrator.hasvibrator () Whether vibrator is currently in the vibrator state