我尝试了很多方法,但我的代码仍然拒绝在画布上简单地画一条线。我注释掉的每一次尝试要么使程序崩溃,要么不显示该行。我真的很难弄清楚如何做到这一点,我不断收到关于使用线程或 onDraw 或其他什么的不同建议,但似乎没有一个有效。
我的总体目标是在屏幕上显示位图图像,并在其上画线。然后,我希望使用 canvas.tranlate 使整个图像(位图及其顶部的线条)能够平移。(我已经实现了成功平移图像的功能。)
public class Main extends Activity implements OnTouchListener {
CanvasView myCanvas;
Canvas canvas;
Bitmap bmp;
float fingerPosX = 0;
float fingerPosY = 0;
float preFingerPosX = 0;
float preFingerPosY = 0;
float bmpX = 0;
float bmpY = 0;
float dx = 0;
float dy = 0;
Paint paint;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.gmit2013mod);
canvas = new Canvas();
myCanvas = new CanvasView(this);
myCanvas.setOnTouchListener(this);
setContentView(myCanvas);
}
@Override
public void onPause() {
super.onPause();
myCanvas.pause();
}
@Override
public void onResume() {
super.onResume();
myCanvas.resume();
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(action == event.ACTION_DOWN) { //when touch is received get finger position
preFingerPosX = event.getX();
preFingerPosY = event.getY();
}
if(action == event.ACTION_UP) { //when touch is released get finger position
fingerPosX = event.getX();
fingerPosY = event.getY();
dx = fingerPosX - preFingerPosX; //subtract previous finger position from current
dy = fingerPosY - preFingerPosY;
bmpX += dx; //apply difference to image
bmpY += dy;
}
//CanvasView.updateMap();
//invalidate();
return true;
}
//CanvasView Class
public class CanvasView extends SurfaceView implements Runnable{ //implements Runnable
SurfaceHolder surfaceHolder;
Thread myThread = null;
boolean isRunning = false;
public CanvasView(Context context) {
super(context);
surfaceHolder = getHolder();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawBitmap(bmp, 0, 0, null);
//canvas = surfaceHolder.lockCanvas();
canvas.drawLine(10, 10, 30, 30, paint);
//surfaceHolder.unlockCanvasAndPost(canvas);
}
public void pause() {
isRunning = false;
while(true) {
try {
myThread.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
myThread = null;
}
public void resume() {
isRunning = true;
myThread = new Thread(this);
myThread.start();
}
public void run() {
while(isRunning) {
//init(); //crashes when code here
if(!surfaceHolder.getSurface().isValid()) {
continue;
}
invalidate();
//init();
//canvas.translate(bmpX, bmpY);
updateMap();
//invalidate();
}
}
// canvas = surfaceHolder.lockCanvas();
// canvas.drawRGB(255, 255, 255);
// canvas.drawBitmap(bmp, 0-(bmp.getWidth()/2), 0-(bmp.getHeight()/2), null);
// surfaceHolder.unlockCanvasAndPost(canvas);
}
public void updateMap() {
runOnUiThread(new Runnable () {
public void run() {
// TODO Auto-generated method stub
//canvas = surfaceHolder.lockCanvas();
//canvas.drawRGB(255, 255, 255);
//canvas.drawBitmap(bmp, bmpX-(bmp.getWidth()/2), bmpY-(bmp.getHeight()/2), null);
//paint.setColor(Color.RED);
//canvas.drawLine(10, 10, 30, 30, paint); //it seems this must b in ondraw. try canvas(bmp) again, mite have some affect with different bmp
//invalidate();
//surfaceHolder.unlockCanvasAndPost(canvas);
}
});
}
}
这是我的代码,如果您能给我提供任何解决方案或一些已经在线运行的示例,我将不胜感激,因为我在这个项目上的时间不多了!谢谢
更新: 我在 onCreate 方法中添加了以下代码行:
Drawable drawBmp = new BitmapDrawable(bmp);
drawBmp.draw(canvas);