在我的应用程序中,我绘制一条线,并希望它在我的手指在屏幕上移动。但是现在只有当我把手指从屏幕上移开时才会重新绘制线条。
怎么办?
以这种方式在SurfaceView中绘制线条:
canvas.drawLine(0, 0, x_touch, y_touch, paint);
onTouch的代码:
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction() ;
x_touch = (int) event.getX() ;
y_touch = (int) event.getY() ;
if(action == MotionEvent.ACTION_DOWN)
{
}
return super.onTouchEvent(event);
}
答案 0 :(得分:0)
由于绘图线程(UIThread)调用onTouchEvent
,因此在手指启动之前,您将阻止绘图线程。无论你在if(action == MotionEvent.ACTION_DOWN)
需要在一个单独的线程中完成时做什么。例如:
if(action == MotionEvent.ACTION_DOWN) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
// Draw your line
}
}.start();
}
答案 1 :(得分:0)
试试这个
Path drawPath = new Path();
Paint drawPaint = new Paint();
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
break;
default:
return false;
}
invalidate();//this will cause the onDraw() to execute
return true;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
答案 2 :(得分:0)
我的问题以下列方式解决了:
新听众:
public class LintListener implements OnTouchListener {
public LintListener() {
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction() ;
//end_game(true);
x_touch = (int) event.getX() ;
y_touch = (int) event.getY() ;
return true;
}
}
在创建:
super.onCreate(savedInstanceState);
MySurfaceView msf = new MySurfaceView(this);
setContentView(msf);
msf.setOnTouchListener(new LintListener());