我希望在触摸时持续振动,并根据手指的运动“动态”扩展图案,而不会短时切断振动电机。 Continuous Vibration on Touch in android解决方案不起作用,因为MotionEvents应该重新定义振动模式(仅是幅度)。
import android.content.Context;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends AppCompatActivity implements {
Vibrator vibrator;
VibrationEffect vibe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
findViewById(R.id.text_view).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// do some calculations from MotionEvent's values
VibrationEffect vibe = VibrationEffect.createWaveform(
new long[]{20, 20},
new int[]{a, b}, 0);
vibrator.vibrate(vibe); // this causes the vibration motor to shortly stop and continue afterwards
}
return false;
}
});
}
}
设备是具有线性谐振执行器的Sony Xperia XZ3(必填,因为它比传统的偏心旋转质量快)。我对AndroidStudio软件包等开放的新SDK持开放态度,以解决此问题。