怎么将资源图片设置背景图片?

占占 2013-05-26 03:13:19
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;
}
}


大家看看。
...全文
127 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
穿裤衩闯天下 2013-05-30
  • 打赏
  • 举报
回复
你试试我的这个方法,我这样是可以设置的,因为我这个是用一个小的图片来平铺的,所以写了有两个循环来横向和纵向平铺,如果你的图片够大的话,那两个方法可以不用!
    /**
     * @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;
    }
wowowowo 2013-05-29
  • 打赏
  • 举报
回复
Bitmap bitmaptemp = ((BitmapDrawable) context.getResources().getDrawable(resId)).getBitmap();
顾小林 2013-05-27
  • 打赏
  • 举报
回复
setBackgroundResource(int) 直接调用这个方法 或者 之类的方法
占占 2013-05-27
  • 打赏
  • 举报
回复
首先非常感谢你的回答,你说的那个方法我试了。是可以设置背景图片。但现在情况是这样,我最终还是需要获得一个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
褚六 2013-05-27
  • 打赏
  • 举报
回复
在XML文件中设置android:background属性 || 在Activity程序中使用setBackgroundResource()方法
占占 2013-05-26
  • 打赏
  • 举报
回复
没人么,自顶一个。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧