bitmap相关:
bitmap转换成byte[]:
public byte[] bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
byte[]转换成bitmap:
public Bitmap bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
// return BitmapFactory.decodeByteArray(b, 0, b.length/2); 直接用流的一半不行
} else {
return null;
}
}
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
// return BitmapFactory.decodeByteArray(b, 0, b.length/2); 直接用流的一半不行
} else {
return null;
}
}
bitmap缩放:
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleHeight = ((float) height / h);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return newbmp;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleHeight = ((float) height / h);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return newbmp;
}
drawable转bitmap:
public static Bitmap drawableToBitmap(Drawable drawable) {
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
bitmap转drawable:
public Drawable bitmap2Drawable(Bitmap bitmap){
return new BitmapDrawable(getResources(),bitmap);
}
return new BitmapDrawable(getResources(),bitmap);
}
bitmap圆角处理:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242; //画笔颜色
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, w, h);
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true); //画笔设置抗锯齿
canvas.drawARGB(0, 0, 0, 0); //画白板
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); //画圆角矩形
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //PorterDuff.Mode.SRC_IN 取两层绘制交集。显示上层。
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242; //画笔颜色
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, w, h);
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true); //画笔设置抗锯齿
canvas.drawARGB(0, 0, 0, 0); //画白板
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); //画圆角矩形
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //PorterDuff.Mode.SRC_IN 取两层绘制交集。显示上层。
//参考链接
http://blog.csdn.net/t12x3456/article/details/10432935
canvas.drawBitmap(bitmap, rect, rect, paint); //把bitmap画上去,由于paint上一步设置了Xfermode,paint就画bitmap与圆角矩阵的交集部分。
return output;
}
canvas.drawBitmap(bitmap, rect, rect, paint); //把bitmap画上去,由于paint上一步设置了Xfermode,paint就画bitmap与圆角矩阵的交集部分。
return output;
}
bitmap映射处理:
public Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGap = 20; //反射图与本身的间距
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1); //倒转
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,
h / 2, matrix, false); //取原图的下半部倒转
Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2 + reflectionGap),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight() + reflectionGap, 0,
bitmapWithReflection.getHeight(), 0xaaffffff,
0x33ffffff, Shader.TileMode.CLAMP); //线性渲染 参考链接 http://blog.csdn.net/t12x3456/article/details/10566219
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); //取两层绘制交集。显示下层。
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, h + reflectionGap, w, bitmapWithReflection.getHeight()
, paint);
return bitmapWithReflection;
}
final int reflectionGap = 20; //反射图与本身的间距
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1); //倒转
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,
h / 2, matrix, false); //取原图的下半部倒转
Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2 + reflectionGap),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight() + reflectionGap, 0,
bitmapWithReflection.getHeight(), 0xaaffffff,
0x33ffffff, Shader.TileMode.CLAMP); //线性渲染 参考链接 http://blog.csdn.net/t12x3456/article/details/10566219
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); //取两层绘制交集。显示下层。
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, h + reflectionGap, w, bitmapWithReflection.getHeight()
, paint);
return bitmapWithReflection;
}
缩放drawable:
public Drawable zoomDrawable(Drawable drawable, int w, int h) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
// drawable转换成bitmap
Bitmap oldbmp = drawableToBitmap(drawable);
// 创建操作图片用的Matrix对象
Matrix matrix = new Matrix();
// 计算缩放比例
float sx = ((float) w / width);
float sy = ((float) h / height);
// 设置缩放比例
matrix.postScale(sx, sy);
// 建立新的bitmap,其内容是对原bitmap的缩放后的图
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
matrix, true);
return new BitmapDrawable(getResources() ,newbmp);
}
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
// drawable转换成bitmap
Bitmap oldbmp = drawableToBitmap(drawable);
// 创建操作图片用的Matrix对象
Matrix matrix = new Matrix();
// 计算缩放比例
float sx = ((float) w / width);
float sy = ((float) h / height);
// 设置缩放比例
matrix.postScale(sx, sy);
// 建立新的bitmap,其内容是对原bitmap的缩放后的图
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
matrix, true);
return new BitmapDrawable(getResources() ,newbmp);
}
按机器的宽高比处理图片:
private Bitmap backgroundMachine(int id) {
Bitmap ret = null;
Bitmap orgBmp = BitmapFactory.decodeStream(getResources()
.openRawResource(id));
int orgBmpWidth = orgBmp.getWidth();
int orgBmpHeight = orgBmp.getHeight();
if (screenHeight * orgBmpWidth / orgBmpHeight >= screenWidth) {
int scaleWidth = screenHeight * orgBmpWidth / orgBmpHeight;
Bitmap temp = Bitmap.createScaledBitmap(orgBmp, scaleWidth,
screenHeight, true);
ret = Bitmap.createBitmap(temp, (scaleWidth - screenWidth) / 2, 0,
screenWidth + (scaleWidth - screenWidth) / 2, screenHeight);
} else {
int scaleHeight = screenWidth * orgBmpHeight / orgBmpWidth;
Bitmap temp = Bitmap.createScaledBitmap(orgBmp, screenWidth,
scaleHeight, true);
ret = Bitmap.createBitmap(temp, 0, 0, screenWidth, screenHeight);
}
if (!orgBmp.isRecycled()) {
orgBmp.recycle();
}
return ret;
}
Bitmap ret = null;
Bitmap orgBmp = BitmapFactory.decodeStream(getResources()
.openRawResource(id));
int orgBmpWidth = orgBmp.getWidth();
int orgBmpHeight = orgBmp.getHeight();
if (screenHeight * orgBmpWidth / orgBmpHeight >= screenWidth) {
int scaleWidth = screenHeight * orgBmpWidth / orgBmpHeight;
Bitmap temp = Bitmap.createScaledBitmap(orgBmp, scaleWidth,
screenHeight, true);
ret = Bitmap.createBitmap(temp, (scaleWidth - screenWidth) / 2, 0,
screenWidth + (scaleWidth - screenWidth) / 2, screenHeight);
} else {
int scaleHeight = screenWidth * orgBmpHeight / orgBmpWidth;
Bitmap temp = Bitmap.createScaledBitmap(orgBmp, screenWidth,
scaleHeight, true);
ret = Bitmap.createBitmap(temp, 0, 0, screenWidth, screenHeight);
}
if (!orgBmp.isRecycled()) {
orgBmp.recycle();
}
return ret;
}
通过反射获取图片Id:
public int getReflectId(String picName){
int r = R.drawable.ic_launcher;
try {
Field field = R.drawable.class.getField(picName);
// field.setAccessible(true);
r = field.getInt(null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
} catch (SecurityException e) {
// TODO Auto-generated catch block
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
} finally {
return r;
}
}
int r = R.drawable.ic_launcher;
try {
Field field = R.drawable.class.getField(picName);
// field.setAccessible(true);
r = field.getInt(null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
} catch (SecurityException e) {
// TODO Auto-generated catch block
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
} finally {
return r;
}
}