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

时间:2013-04-17 11:11:23

标签: android canvas ondraw drawbitmap

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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

我需要在onCreate中添加paint = new Paint()。 Paint对象是null,这就是为什么它不会绘制的原因。花了几个星期和一个荒谬的纠缠只是为了发现!我要去度假......

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

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