Android中常用的位图操作
[代码]java代码:
//View转换为Bitmap
public void getDrawingCache(final ImageView sourceImageView, final ImageView destImageView) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//开启bitmap缓存
sourceImageView.setDrawingCacheEnabled(true);
//获取bitmap缓存
Bitmap mBitmap = sourceImageView.getDrawingCache();
//显示 bitmap
destImageView.setImageBitmap(mBitmap);
// Bitmap mBitmap = sourceImageView.getDrawingCache();
//Drawable drawable = (Drawable) new BitmapDrawable(mBitmap);
//destImageView.setImageDrawable(drawable);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//不再显示bitmap缓存
//destImageView.setImageBitmap(null);
destImageView.setImageResource(R.drawable.pet);
//使用这句话而不是用上一句话是错误的,空指针调用
//destImageView.setBackgroundDrawable(null);
//关闭bitmap缓存
sourceImageView.setDrawingCacheEnabled(false);
//释放bitmap缓存资源
sourceImageView.destroyDrawingCache();
}
}, DELAY_TIME);
}
}, DELAY_TIME);
}
mImageView1.setImageResource(R.drawable.android);
mImageView2.setImageResource(R.drawable.pet);
getDrawingCache(mImageView1, mImageView2);
[代码]java代码:
public Bitmap getRoundedBitmap() {
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.frame);
//创建新的位图
Bitmap bgBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_8888);
//把创建的位图作为画板
Canvas mCanvas = new Canvas(bgBitmap);
Paint mPaint = new Paint();
Rect mRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
RectF mRectF = new RectF(mRect);
//设置圆角半径为20
float roundPx = 15;
mPaint.setAntiAlias(true);
//先绘制圆角矩形
mCanvas.drawRoundRect(mRectF, roundPx, roundPx, mPaint);
//设置图像的叠加模式
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//绘制图像
mCanvas.drawBitmap(mBitmap, mRect, mRect, mPaint);
return bgBitmap;
}
[代码]java代码:
public Bitmap getGrayBitmap() {
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android);
Bitmap mGrayBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(mGrayBitmap);
Paint mPaint = new Paint();
//创建颜色变换矩阵
ColorMatrix mColorMatrix = new ColorMatrix();
//设置灰度影响范围
mColorMatrix.setSaturation(0);
//创建颜色过滤矩阵
ColorMatrixColorFilter mColorFilter = new ColorMatrixColorFilter(mColorMatrix);
//设置画笔的颜色过滤矩阵
mPaint.setColorFilter(mColorFilter);
//使用处理后的画笔绘制图像
mCanvas.drawBitmap(mBitmap, 0, 0, mPaint);
return mGrayBitmap;
}
[代码]java代码:
public Bitmap getAlphaBitmap() {
BitmapDrawable mBitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.enemy_infantry_ninja);
Bitmap mBitmap = mBitmapDrawable.getBitmap();
//BitmapDrawable的getIntrinsicWidth()方法,Bitmap的getWidth()方法
//注意这两个方法的区别
//Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmapDrawable.getIntrinsicWidth(), mBitmapDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(mAlphaBitmap);
Paint mPaint = new Paint();
mPaint.setColor(Color.BLUE);
//从原位图中提取只包含alpha的位图
Bitmap alphaBitmap = mBitmap.extractAlpha();
//在画布上(mAlphaBitmap)绘制alpha位图
mCanvas.drawBitmap(alphaBitmap, 0, 0, mPaint);
return mAlphaBitmap;
}
[代码]java代码:
public Bitmap getScaleBitmap() {
BitmapDrawable mBitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.pet);
Bitmap mBitmap = mBitmapDrawable.getBitmap();
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(0.75f, 0.75f);
Bitmap mScaleBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix, true);
return mScaleBitmap;
}
[代码]java代码:
public Bitmap getRotatedBitmap() {
BitmapDrawable mBitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.pet);
Bitmap mBitmap = mBitmapDrawable.getBitmap();
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preRotate(45);
Bitmap mRotateBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix, true);
return mRotateBitmap;
}
[代码]java代码:
public Bitmap getScrewBitmap() {
BitmapDrawable mBitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.pet);
Bitmap mBitmap = mBitmapDrawable.getBitmap();
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preSkew(1.0f, 0.15f);
Bitmap mScrewBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix, true);
return mScrewBitmap;
}
[代码]java代码:
private Bitmap getReflectedBitmap() {
BitmapDrawable mBitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.pet);
Bitmap mBitmap = mBitmapDrawable.getBitmap();
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
Matrix matrix = new Matrix();
// 图片缩放,x轴变为原来的1倍,y轴为-1倍,实现图片的反转
matrix.preScale(1, -1);
//创建反转后的图片Bitmap对象,图片高是原图的一半。
//Bitmap mInverseBitmap = Bitmap.createBitmap(mBitmap, 0, height/2, width, height/2, matrix, false);
//创建标准的Bitmap对象,宽和原图一致,高是原图的1.5倍。
//注意两种createBitmap的不同
//Bitmap mReflectedBitmap = Bitmap.createBitmap(width, height*3/2, Config.ARGB_8888);
Bitmap mInverseBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix, false);
Bitmap mReflectedBitmap = Bitmap.createBitmap(width, height*2, Config.ARGB_8888);
// 把新建的位图作为画板
Canvas mCanvas = new Canvas(mReflectedBitmap);
//绘制图片
mCanvas.drawBitmap(mBitmap, 0, 0, null);
mCanvas.drawBitmap(mInverseBitmap, 0, height, null);
//添加倒影的渐变效果
Paint mPaint = new Paint();
Shader mShader = new LinearGradient(0, height, 0, mReflectedBitmap.getHeight(), 0x70ffffff, 0x00ffffff, TileMode.MIRROR);
mPaint.setShader(mShader);
//设置叠加模式
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
//绘制遮罩效果
mCanvas.drawRect(0, height, width, mReflectedBitmap.getHeight(), mPaint);
return mReflectedBitmap;
}
[代码]java代码:
R.drawable.beauty);
Bitmap bitmap = bd.getBitmap();
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Bitmap bm = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
Paint mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Style.STROKE);
canvas.drawBitmap(bitmap, 0, 0, mPaint);
[代码]java代码:
int deltY = 98;
DashPathEffect dashStyle = new DashPathEffect(new float[] { 10, 5, 5, 5 }, 2);//创建虚线边框样式
RectF faceRect = new RectF(0, 0, 88, 106);
float [] faceCornerii = new float[] {30,30,30,30,75,75,75,75};
Paint mPaint = new Paint();//创建画笔
mPaint.setColor(0xFF6F8DD5);
mPaint.setStrokeWidth(6);
mPaint.setPathEffect(dashStyle);
Path clip = new Path();//创建路径
clip.reset();
clip.addRoundRect(faceRect, faceCornerii, Direction.CW);//添加圆角矩形路径
canvas.save();//保存画布
canvas.translate(deltX, deltY);
canvas.clipPath(clip, Region.Op.DIFFERENCE);
canvas.drawColor(0xDF222222);
canvas.drawPath(clip, mPaint);//绘制路径
canvas.restore();
[代码]java代码:
srcRect.offset(deltX, deltY);
PaintFlagsDrawFilter dfd = new PaintFlagsDrawFilter(Paint.ANTI_ALIAS_FLAG,
Paint.FILTER_BITMAP_FLAG);
canvas.setDrawFilter(dfd);
canvas.clipPath(clip);//使用路径剪切画布
canvas.drawBitmap(bitmap, srcRect, faceRect, mPaint);