我已經嘗試了很多方法,但我的代碼仍拒絕在畫布上簡單地畫一條線。我已經註釋過的每個嘗試都會使程序崩潰或不顯示該行。我真的很努力地想知道如何做到這一點,我不斷得到有關使用線程或onDraw或其他方面的不同建議,但似乎沒有任何工作。canvas.drawLine()沒有出現在位圖上
我的總體目標是在屏幕上顯示位圖圖像,並在其上畫線。然後我希望使用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);
}
});
}
}
這裏是我的代碼,如果你能爲我提供任何解決方案或已聯機工作的一些這方面的例子,因爲我跑出來我將不勝感激在這個項目上的時間!由於
UPDATE: 我添加下面的代碼行到onCreate方法:
Drawable drawBmp = new BitmapDrawable(bmp);
drawBmp.draw(canvas);
看看這個鏈接... [這裏] [1] [1]:http://stackoverflow.com/questions/14427665/android-drawing-app-line-cannot在畫廊上繪製的位圖加載 – AndiM