public class MyView extends View { private static final float MINP = 0.25f;
private static final float MAXP = 0.75f; private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint; public MyView(Context c) {
super(c);
// mBitmap = Bitmap.createBitmap(600, 800, Bitmap.Config.ARGB_8888);
                //就是这里,但这种方式没有效果。
mBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bk18).copy(Bitmap.Config.ARGB_8888, true); mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
} public void setBitmap(Bitmap bitmap) {
mBitmap = bitmap;
} @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// super.onSizeChanged(w, h, oldw, oldh);
if (mBitmap != null) {
if (mBitmap.getHeight() == w && mBitmap.getWidth() == h) {
Log.d(TAG, "rotating bitmap by 90 degree"); Matrix mtx = new Matrix();
mtx.postRotate(90, h / 2, w / 2);
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, h, w, mtx,
false);
mCanvas = new Canvas();
mCanvas.setBitmap(mBitmap);
return;
} else {
mBitmap.recycle();
}
}
mCanvas = new Canvas();
// mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
                        mBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bk18).copy(Bitmap.Config.ARGB_8888, true);
mCanvas.setBitmap(mBitmap);
} @Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA); canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); canvas.drawPath(mPath, 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.reset();
} void save() {
myAsyncTask = new MyAsyncTask(mCanvas, mBitmap, fingerPaint.this);
myAsyncTask.execute(0);
} @Override
public boolean onTouchEvent(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;
}
}大家看看。

解决方案 »

  1.   

    setBackgroundResource(int) 直接调用这个方法 或者 之类的方法
      

  2.   

    在XML文件中设置android:background属性 || 在Activity程序中使用setBackgroundResource()方法
      

  3.   

    首先非常感谢你的回答,你说的那个方法我试了。是可以设置背景图片。但现在情况是这样,我最终还是需要获得一个bitmap对象,然后将其通过canvas的构造方法去构造一个画布,然后在再其上面涂鸦。可是,我用了好几种将资源文件转为bitmap的方法并传入都会报错。
    包括:BitmapFactory.decodeResource、this.getResources().getDrawable等。。
    报错如下:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.llm.android.fingerPaint/com.llm.android.fingerPaint.fingerPaint}: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
      

  4.   

    Bitmap bitmaptemp = ((BitmapDrawable) context.getResources().getDrawable(resId)).getBitmap();
      

  5.   

    你试试我的这个方法,我这样是可以设置的,因为我这个是用一个小的图片来平铺的,所以写了有两个循环来横向和纵向平铺,如果你的图片够大的话,那两个方法可以不用!
        /**
         * @param bitmap 设置涂鸦板的背景
         */
        public void setGraffitiBg(Bitmap bitmap) {        mGraffitiPadBg = Bitmap.createBitmap(screenInfo.getScreenWidth(),
                    screenInfo.getScreenHeight(), Bitmap.Config.ARGB_8888);        mCanvas = new Canvas(mGraffitiPadBg);        if (bitmap == null) {
                /*
     * 设置画布的颜色,当使用图片为背景时不能设置,否则会覆盖图片
     */
                // mCanvas.drawColor(getResources().getColor(android.R.color.darker_gray));
    /*
     * 设置默认背景图片
     */
                Bitmap defaultbg = BitmapFactory.decodeResource(getResources(),
                        R.drawable.huise1);
                mCanvas.drawBitmap(drawColTilebitmap(drawRowTileBitmap(defaultbg)),
                        0, 0, mPaint);
            } else {        }
        }
    /**
         * @param bitmap
         * @return 横向平铺背景图片
         */
        private Bitmap drawRowTileBitmap(Bitmap bitmap) {
            int count = (screenInfo.getScreenWidth() + bitmap.getWidth() - 1)
                    / bitmap.getWidth();        Bitmap rowTileBitmap = Bitmap.createBitmap(screenInfo.getScreenWidth(),
                    bitmap.getHeight(), Config.ARGB_8888);        Canvas rowCanvas = new Canvas(rowTileBitmap);
            for (int idx = 0; idx < count; idx++) {
                rowCanvas.drawBitmap(bitmap, bitmap.getWidth() * idx, 0, null);
            }        return rowTileBitmap;    }    /**
         * @param bitmap
         * @return 纵向平铺背景图片
         */
        private Bitmap drawColTilebitmap(Bitmap bitmap) {
            int count = (screenInfo.getScreenHeight() + bitmap.getHeight() - 1)
                    / bitmap.getHeight();
            Bitmap colTileBitmap = Bitmap.createBitmap(bitmap.getWidth(),
                    screenInfo.getScreenHeight(), Config.ARGB_8888);
            Canvas colCanvas = new Canvas(colTileBitmap);
            for (int idx = 0; idx < count; idx++) {
                colCanvas.drawBitmap(bitmap, 0, bitmap.getHeight() * idx, null);
            }        return colTileBitmap;
        }