下面通过一段代码来说明如何获取View对应的Bitmap。在代码中使用了两个ImageView并给它们都设置了显示的图片资源,然后把第一个ImageView对应的bitmap显示到第二个ImageView中。由于在Activity的onCreate方法中调用这个方法,当执行Activity的onCreate方法时,控件还没有准备好,所以需要使用Handler进行延迟操作,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);
Demo运行效果图1
Demo运行效果图2
二、图片圆角处理下面使用PorterDuff.Mode.SRC_IN规则来给图片添加圆角效果,主要的思路是先绘制一个圆角矩形,然后在上面绘制图像,取图像与圆角矩形的交集部分,只保留图像。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;
}
图片圆角处理
//图片灰化处理
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;
}
图片灰化处理
//提取图像Alpha位图
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;
}
提取图像Alpha位图
其中最后一幅图片是把原图片四个边距缩小两个dp,然后与Alpha位图一起绘制的结果,读者可以参考本章Demo中的getStrokeBitmap()方法。
//getScaleBitmap 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; }
效果如下图所示:
图像缩放
2)图片旋转
//getRotatedBitmap
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;
}
图片旋转
//getScrewBitmap
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;
}
效果如下图所示:
图像倾斜
//getReflectedBitmap
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;
}
图像倒影
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(
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);
第一步效果图
int deltX = 76;
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();
第二步效果
Rect srcRect = new Rect(0, 0, 88, 106);
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);
第三部效果图
如果要为图片添加水印,或者把几张小图片拼接成大图片时,就需要利用图像合成的方法,在前面实例代码中已经使用了这种方法,就是创建新位图作为画板,然后在对应的位置上绘制其他图像。
Demo无积分下载:http://download.csdn.net/detail/wangfayinn/5152359
原文链接:http://www.devdiv.com/Android-Android%E4%B8%AD%E5%B8%B8%E7%94%A8%E7%9A%84%E4%BD%8D%E5%9B%BE%E6%93%8D%E4%BD%9C-thread-127485-1-1.html