Pregunta

I'm trying to load a picture to paint on top of in an Android app but can't seem to load the to paint on top of.

The ErrorLog for activity_main.xml tells me I have a problem in my onDraw() method in my custom views class at the line canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);`


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <edu.berkeley.cs160.opalkale.prog2.DrawingView

        android:id="@+id/drawing"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="3dp"
        android:layout_weight="1"
        android:src="@drawable/crayon" />

</LinearLayout>

My MainActivity.java file:

package edu.berkeley.cs160.opalkale.prog2;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.Menu;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.crayon);
    Canvas canvas = new Canvas (mBitmap);


    //DrawingView drawingView = (DrawingView)findViewById(R.id.drawing);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

My Custom View class:

import android.view.MotionEvent;
import android.view.View;

public class DrawingView extends View {
private Path mPath;
private Paint mPaint;

private Canvas mCanvas;
private Canvas shadowCanvas;

private Bitmap colorBitmap;
private Bitmap grayscaleBitmap;
private Bitmap mBitmap;
private Bitmap shadowBitmap;

private Paint mBitmapPaint;

private ArrayList<Path> paths = new ArrayList<Path>();

public DrawingView(Context context, AttributeSet attrs){
    super(context, attrs);

    mBitmapPaint = new Paint(Paint.DITHER_FLAG);

    mPath = new Path();

    mPaint = new Paint();
    mPaint.setColor(Color.GRAY);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(3);

    mCanvas = new Canvas ();

    paths.add(mPath);
}


@Override
protected void onDraw(Canvas canvas) {

canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);

    for (Path p : paths) {
        canvas.drawPath(p, mPaint);
    }
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

private void touch_up() {
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    mPath = new Path();
    paths.add(mPath);
}


public boolean onTouch(View arg0, MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
    }
    return true;
}
}
¿Fue útil?

Solución

You have one variable mBitmap which is not initialized, That's why it is throwing NPE.

@Override
protected void onDraw(Canvas canvas) {

canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);             // NPE here 

    for (Path p : paths) {
        canvas.drawPath(p, mPaint);                     
    }
}

You need to initialized this variable.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top