[英]Continuous Vibration on Touch in android
I want to vibrate continuously on touch until I raise the finger from the view.I used the following code for vibration on canvas 我希望触摸时不断振动,直到从视图中抬起手指为止。我使用以下代码在画布上进行振动
public class DrawFunny extends Activity implements OnTouchListener {
private float x;
private float y;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyCustomPanel view = new MyCustomPanel(this);
ViewGroup.LayoutParams params =
new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
addContentView(view, params);
view.setOnTouchListener(this);
}
private class MyCustomPanel extends View {
public MyCustomPanel(Context context) {
super(context);
}
@Override
public void draw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(6);
canvas.drawLine(10,10,50,50,paint);
paint.setColor(Color.RED);
canvas.drawLine(50, 50, 90, 10, paint);
canvas.drawCircle(50, 50, 3, paint);
canvas.drawCircle(x,y,3,paint);
}
}
public boolean onTouch(View v, MotionEvent event) {
Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vb.vibrate(100);
x = event.getX();
y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("ACTION_DOWN");
return true;
case MotionEvent.ACTION_MOVE:
System.out.println("ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
System.out.println("ACTION_UP");
break;
default:
return false;
}
v.invalidate();
return true;
}
}
But the vibrator not playing continuously how is it done 但是振动器不能连续播放,它是如何完成的
First of all, you always have to specify the vibration time in millis. 首先,您始终必须以毫秒为单位指定振动时间。 So you can set a long time for the vibration and stop the vibration in the ACTION_UP event. 因此,您可以为振动设置较长的时间,并在ACTION_UP事件中停止振动。 For example: 例如:
public boolean onTouch(View v, MotionEvent event) {
Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
vb.vibrate(10000); // 10 seconds
System.out.println("ACTION_DOWN");
return true;
case MotionEvent.ACTION_MOVE:
System.out.println("ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
System.out.println("ACTION_UP");
vb.cancel(); // Stop the vibration
break;
default:
return false;
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.