手機震動程式(Vibrator Program)主要是打開震動器(Vibrator),然後傳遞一個 int 數值或陣列,它們是以毫秒(ms)為單位,代表打開或關閉振動器的持續時間,如 1000 是代表 1000ms 等於 1s (vibrator.vibrate(1000) )。需要重複使用,可以在第二值加入 0 (vibrator.vibrate(mVibratePattern, 0) ),禁用重複可以在第二值加入 -1 (vibrator.vibrate(mVibratePattern, -1))。
Android 手機震動程式(Vibrator Program) |
- 操作系統:Windows 7 64-bit 版本
- 開發環境:Android Studio 4.0.1 版本
- 原程式:C:\Development\Development_Android\Android_Project\DIY-Android-152-02 Vibration Bugworkshop
- 程式:C:\Development\Development_Android\Android_Project\DIY-Android-152-02 Vibration Bugworkshop
- 測試環境:Vivo(維沃)X5Max Android 4.4.4(KitKat)版本
Android 手機震動程式(Vibrator Program)編程步驟:
- 設定 Vibrator 許可權限後 - AndroidManifest.xml
- 設定 Vibrator Data 參數
- 設定 Button
AndroidManifest.xml 獲取 Vibrator 許可權:
<uses-permission android:name="android.permission.VIBRATE" /> |
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
<Button android:id="@+id/btn_vibrator_300" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginLeft="30dp" android:layout_marginTop="40dp" android:text="300ms" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button android:id="@+id/btn_vibrator_custom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginLeft="20dp" android:layout_marginTop="40dp" android:text="Custom" app:layout_constraintStart_toEndOf="@+id/btn_vibrator_300" app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button android:id="@+id/btn_vibrator_repeat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginLeft="20dp" android:layout_marginTop="40dp" android:text="Repeat" app:layout_constraintStart_toEndOf="@+id/btn_vibrator_custom" app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout> |
MainActivity.java:
package bugworkshop.blogspot.com.vibration;
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.VibrationEffect; import android.os.Vibrator; import android.view.View; import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Vibrator vibrator;
private Button mBtn_virator_300; private Button mBtn_virator_custom; private Button mBtn_virator_repeat;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
mBtn_virator_300 = (Button)findViewById(R.id.btn_vibrator_300); mBtn_virator_custom = (Button)findViewById(R.id.btn_vibrator_custom); mBtn_virator_repeat= (Button)findViewById(R.id.btn_vibrator_repeat); mBtn_virator_amplitudes = (Button)findViewById(R.id.btn_vibrator_300);
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
mBtn_virator_custom.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { customVibratePattern(); } });
mBtn_virator_300.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { VibratePattern300(); } });
mBtn_virator_repeat.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { customVibratePatternReapt(); } }); }
private void VibratePattern300() {
// 300 : Vibrate for 300 milliseconds vibrator.vibrate(300); }
private void customVibratePattern() {
// 0 : Start without a delay // 400 : Vibrate for 400 milliseconds // 200 : Pause for 200 milliseconds // 400 : Vibrate for 400 milliseconds long[] mVibratePattern = new long[]{0, 400, 200, 400};
// -1 : Do not repeat this pattern // pass 0 if you want to repeat this pattern from 0th index vibrator.vibrate(mVibratePattern, -1); }
private void customVibratePatternReapt() { long[] mVibratePattern = new long[]{0, 400, 200, 400, 0}; vibrator.vibrate(mVibratePattern, 0); }
} |
沒有留言:
張貼留言