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;
}
}大家看看。
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;
}
}大家看看。
解决方案 »
- 新人求帮助
- android 在Activity里怎样设置另一个xml布局文件TextView显示值
- 不同apk共用userid,卸载一个会对另一个有影响?
- Android中如何从服务器端加载界面
- Android 2.3的webkit中如何支持Javascript的defer属性
- 很诡异的问题Android Animation中AlphaAnimation问题, 望大侠关注下
- android 连接数据库
- github.com能ping通,但是https://github.com/wyouflf/xUtils网页打不开
- TRY CATCH的问题
- 求大神啊,帮我解决下。为什么我的思路不可以,我是android刚学没多久的
- 求教 用asmack向openfire注册时 如何增加vcard信息
- 关于线程的问题 急求解答
包括: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
/**
* @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;
}