1

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

2 回答 2

1

您需要将位图创建为 Drawble 对象,然后在该位图上绘制,而不是在它自己的画布上绘制。

所以基本上你在 BitmapFactory 选项的帮助下创建的 Bitmap 对象的帮助下创建了一个 BitmapDrawable 对象。您必须使用此 Drawable 类的 draw(Canvas c) 方法在位图上绘图。

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

于 2013-04-17T11:28:34.453 回答
0

我需要添加paint = new Paint()onCreate。油漆对象是null如此,这就是它不会绘制的原因。我花了几个星期和大量的纠缠才发现这一点!我要去度假了...

如果有人计划制作这样的项目,我打算在不久的将来制作一些 youtube 视频教程,以帮助任何人避免陷入我所做的同样愚蠢的陷阱!

非常感谢 user_CC 的所有时间和对这个问题的帮助!

于 2013-04-23T12:04:00.890 回答