canvas.drawLine() not appearing on bitmap

瘦欲@ 提交于 2019-12-24 05:55:18

问题


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.

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: I added the following lines of code to the onCreate method:

Drawable drawBmp = new BitmapDrawable(bmp);
drawBmp.draw(canvas);

回答1:


You need to create the Bitmap as a Drawble object and then draw on that bitmap not on to the canvas it self.

So basically you create an object of BitmapDrawable with the help of Bitmap object that you have created with the help of BitmapFactory option. You will have to use the draw(Canvas c) method of this Drawable class to draw on the bitmap.

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




回答2:


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...

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!

Many thanks to user_CC too for all his time and help on this question!



来源:https://stackoverflow.com/questions/16058472/canvas-drawline-not-appearing-on-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!