80,351
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
/**
* @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;
}