问题描述
我尝试过这么多方法,但我的代码拒绝简单地在画布上画一条线。每一次我注释掉的尝试都崩溃了程序或将不显示行。我真的很努力,让我的头如何做到这一点,我不断得到不同的建议,使用线程或onDraw或任何,但没有似乎工作。
I have tried so many methods but still my code refuses to simply draw a line on the canvas. Each of the attempts I have commented out either crash the program or won't display the line. I am really struggling to get my head around how to do this, I keep getting different advice on using threads or onDraw or whatever, but none seem to work.
我的整体目的是在屏幕上显示位图图像,并在其顶部绘制线条。然后,我希望使用canvas.tranlate使整个图像(位图以及它的顶部的线)可以。 (我已经实现了成功地平移图像的功能。)
My overall objective is to display a bitmap image on the screen, and draw lines on top of it. I then wish to use canvas.tranlate to make the entire image (the bitmap along with the lines on top of it) pan-able. (I have already implemented the functionality to pan the image successfully.)
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);
}
});
}
}
这里是我的代码,如果你能提供任何解决方案或这个已经在线工作的一些例子我将感激,因为我的时间在这个项目上用完了!感谢
Here is my code, if you can offer me any solution or some example of this already working online I would be grateful because I am running out of time on this project! Thanks
UPDATE:
我在onCreate方法中添加了以下代码:
UPDATE:I added the following lines of code to the onCreate method:
Drawable drawBmp = new BitmapDrawable(bmp);
drawBmp.draw(canvas);
推荐答案
我需要添加 = new Paint()
in onCreate。 Paint对象是 null
所以这就是为什么它不会绘制。吃了几个星期,可笑的数量只是为了发现!我要去度假...
I needed to add paint = new Paint()
in onCreate. Paint object was null
so that's why it wouldn't draw. Took me weeks and a ridiculous amount of pestering just to spot that! I'm going on vacation...
如果有人计划制作这样的项目,我打算在不久的将来做一些YouTube视频教程,以帮助任何人从下降成为我做的同样愚蠢的陷阱。
If anybody plans on making a project like this I intend to make some youtube video tutorials in the near future to help anyone from falling into the same stupid pitfalls that I did!
非常感谢user_CC他所有的时间和帮助这个问题!
Many thanks to user_CC too for all his time and help on this question!
这篇关于canvas.drawLine()不出现在位图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!