gpt4 book ai didi

android - canvas.drawLine() 没有出现在位图上

转载 作者:行者123 更新时间:2023-11-30 03:39:17 29 4
gpt4 key购买 nike

我已经尝试了很多方法,但我的代码仍然拒绝在 Canvas 上简单地画一条线。我注释掉的每一次尝试要么使程序崩溃,要么不显示该行。我真的很难理解如何做到这一点,我不断收到关于使用线程或 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);

最佳答案

您需要将位图创建为 Drawble 对象,然后在该位图上绘制,而不是在其自身的 Canvas 上绘制。

所以基本上,您在 Bitmap 对象的帮助下创建了一个 BitmapDrawable 对象,该对象是您在 BitmapFactory 选项的帮助下创建的。您将不得不使用此 Drawable 类的 draw(Canvas c) 方法在位图上绘制。

http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html

关于android - canvas.drawLine() 没有出现在位图上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16058472/

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