gpt4 book ai didi

android - android中的连续触摸振动

转载 作者:行者123 更新时间:2023-11-29 21:04:34 26 4
gpt4 key购买 nike

我想在触摸时持续振动,直到我从 View 中抬起手指。我使用以下代码在 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;
}
}

但是震动器不连续播放怎么办

最佳答案

首先,您始终必须以毫秒为单位指定振动时间。所以可以在ACTION_UP事件中设置振动时间长,停止振动。例如:

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;
}

关于android - android中的连续触摸振动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com