android Bitmap用法总结

简介:

Bitmap用法总结
1、Drawable → Bitmap

Java代码 复制代码 
  1. <span style="font-size: medium;">Bitmap用法总结
  2. 1、Drawable → Bitmap
  3. public static Bitmap drawableToBitmap(Drawable drawable) {
  4. Bitmap bitmap = Bitmap
  5. .createBitmap(
  6. drawable.getIntrinsicWidth(),
  7. drawable.getIntrinsicHeight(),
  8. drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
  9. : Bitmap.Config.RGB_565);
  10. Canvas canvas = new Canvas(bitmap);
  11. // canvas.setBitmap(bitmap);
  12. drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
  13. drawable.getIntrinsicHeight());
  14. drawable.draw(canvas);
  15. return bitmap;
  16. }
  17. 2、从资源中获取Bitmap
  18. Resources res=getResources();
  19. Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
  20. 3、Bitmap → byte[]
  21. private byte[] Bitmap2Bytes(Bitmap bm){
  22. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  23. bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
  24. return baos.toByteArray();
  25. }
  26. 4byte[] → Bitmap
  27. private Bitmap Bytes2Bimap(byte[] b){
  28. if(b.length!=0){
  29. return BitmapFactory.decodeByteArray(b, 0, b.length);
  30. }
  31. else {
  32. return null;
  33. }
  34. }
  35. 5、保存bitmap
  36. static boolean saveBitmap2file(Bitmap bmp,String filename){
  37. CompressFormat format= Bitmap.CompressFormat.JPEG;
  38. int quality = 100;
  39. OutputStream stream = null;
  40. try {
  41. stream = new FileOutputStream("/sdcard/" + filename);
  42. } catch (FileNotFoundException e) {
  43. // TODO Auto-generated catch block
  44. Generated by Foxit PDF Creator © Foxit Software
  45. http://www.foxitsoftware.com For evaluation only.
  46. e.printStackTrace();
  47. }
  48. return bmp.compress(format, quality, stream);
  49. }
  50. 6、将图片按自己的要求缩放
  51. // 图片源
  52. Bitmap bm = BitmapFactory.decodeStream(getResources()
  53. .openRawResource(R.drawable.dog));
  54. // 获得图片的宽高
  55. int width = bm.getWidth();
  56. int height = bm.getHeight();
  57. // 设置想要的大小
  58. int newWidth = 320;
  59. int newHeight = 480;
  60. // 计算缩放比例
  61. float scaleWidth = ((float) newWidth) / width;
  62. float scaleHeight = ((float) newHeight) / height;
  63. // 取得想要缩放的matrix参数
  64. Matrix matrix = new Matrix();
  65. matrix.postScale(scaleWidth, scaleHeight);
  66. // 得到新的图片
  67. Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
  68. true);
  69. // 放在画布上
  70. canvas.drawBitmap(newbm, 0, 0, paint);
  71. 相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html
  72. 7、bitmap的用法小结
  73. BitmapFactory.Options option = new BitmapFactory.Options();
  74. option.inSampleSize = 2; //将图片设为原来宽高的1/2,防止内存溢出
  75. Bitmap bm = BitmapFactory.decodeFile("",option);//文件流
  76. URL url = new URL("");
  77. InputStream is = url.openStream();
  78. Bitmap bm = BitmapFactory.decodeStream(is);
  79. android:scaleType:
  80. android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /
  81. android:scaleType值的意义区别:
  82. CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分
  83. 显示
  84. CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长
  85. (宽)
  86. CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片
  87. 长/宽等于或小于View的长/宽
  88. Generated by Foxit PDF Creator © Foxit Software
  89. http://www.foxitsoftware.com For evaluation only.
  90. FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
  91. FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
  92. FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
  93. FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示
  94. MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。
  95. //放大缩小图片
  96. public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){
  97. int width = bitmap.getWidth();
  98. int height = bitmap.getHeight();
  99. Matrix matrix = new Matrix();
  100. float scaleWidht = ((float)w / width);
  101. float scaleHeight = ((float)h / height);
  102. matrix.postScale(scaleWidht, scaleHeight);
  103. Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
  104. true);
  105. return newbmp;
  106. }
  107. //将Drawable转化为Bitmap
  108. public static Bitmap drawableToBitmap(Drawable drawable){
  109. int width = drawable.getIntrinsicWidth();
  110. int height = drawable.getIntrinsicHeight();
  111. Bitmap bitmap = Bitmap.createBitmap(width, height,
  112. drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
  113. : Bitmap.Config.RGB_565);
  114. Canvas canvas = new Canvas(bitmap);
  115. drawable.setBounds(0,0,width,height);
  116. drawable.draw(canvas);
  117. return bitmap;
  118. Generated by Foxit PDF Creator © Foxit Software
  119. http://www.foxitsoftware.com For evaluation only.
  120. }
  121. //获得圆角图片的方法
  122. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){
  123. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
  124. .getHeight(), Config.ARGB_8888);
  125. Canvas canvas = new Canvas(output);
  126. final int color = 0xff424242;
  127. final Paint paint = new Paint();
  128. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  129. final RectF rectF = new RectF(rect);
  130. paint.setAntiAlias(true);
  131. canvas.drawARGB(0, 0, 0, 0);
  132. paint.setColor(color);
  133. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  134. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  135. canvas.drawBitmap(bitmap, rect, rect, paint);
  136. return output;
  137. }
  138. //获得带倒影的图片方法
  139. public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){
  140. final int reflectionGap = 4;
  141. int width = bitmap.getWidth();
  142. int height = bitmap.getHeight();
  143. Matrix matrix = new Matrix();
  144. matrix.preScale(1, -1);
  145. Bitmap reflectionImage = Bitmap.createBitmap(bitmap,
  146. 0, height/2, width, height/2, matrix, false);
  147. Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2),
  148. Config.ARGB_8888);
  149. Canvas canvas = new Canvas(bitmapWithReflection);
  150. canvas.drawBitmap(bitmap, 0, 0, null);
  151. Paint deafalutPaint = new Paint();
  152. Generated by Foxit PDF Creator © Foxit Software
  153. http://www.foxitsoftware.com For evaluation only.
  154. canvas.drawRect(0, height,width,height + reflectionGap,
  155. deafalutPaint);
  156. canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
  157. Paint paint = new Paint();
  158. LinearGradient shader = new LinearGradient(0,
  159. bitmap.getHeight(), 0, bitmapWithReflection.getHeight()
  160. + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
  161. paint.setShader(shader);
  162. // Set the Transfer mode to be porter duff and destination in
  163. paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
  164. // Draw a rectangle using the paint with our linear gradient
  165. canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
  166. + reflectionGap, paint);
  167. return bitmapWithReflection;
  168. }
  169. }</span>
Bitmap用法总结
1、Drawable → Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
// canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
2、从资源中获取Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
3、Bitmap → byte[]
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
4、byte[] → Bitmap
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}
5、保存bitmap
static boolean saveBitmap2file(Bitmap bmp,String filename){
CompressFormat format= Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;
try {
stream = new FileOutputStream("/sdcard/" + filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
e.printStackTrace();
}
return bmp.compress(format, quality, stream);
}
6、将图片按自己的要求缩放
// 图片源
Bitmap bm = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.dog));
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 设置想要的大小
int newWidth = 320;
int newHeight = 480;
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
// 放在画布上
canvas.drawBitmap(newbm, 0, 0, paint);
相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html
7、bitmap的用法小结
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 2; //将图片设为原来宽高的1/2,防止内存溢出
Bitmap bm = BitmapFactory.decodeFile("",option);//文件流
URL url = new URL("");
InputStream is = url.openStream();
Bitmap bm = BitmapFactory.decodeStream(is);
android:scaleType:
android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /
android:scaleType值的意义区别:
CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分
显示
CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长
(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片
长/宽等于或小于View的长/宽
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示
MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。


//放大缩小图片
public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float)w / width);
float scaleHeight = ((float)h / height);
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
true);
return newbmp;
}
//将Drawable转化为Bitmap
public static Bitmap drawableToBitmap(Drawable drawable){
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height,
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0,0,width,height);
drawable.draw(canvas);
return bitmap;
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
}
//获得圆角图片的方法
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
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(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
//获得带倒影的图片方法
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap,
0, height/2, width, height/2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2),
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
canvas.drawRect(0, height,width,height + reflectionGap,
deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
bitmap.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
}


2、从资源中获取Bitmap

Java代码 复制代码 收藏代码
  1. <span style="font-size: medium;">Resources res=getResources();
  2. Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);</span>
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);


3、Bitmap → byte[]

Java代码 复制代码 收藏代码
  1. <span style="font-size: medium;">private byte[] Bitmap2Bytes(Bitmap bm){
  2. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  3. bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
  4. return baos.toByteArray();</span>
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();


}
4、byte[] → Bitmap

Java代码 复制代码 收藏代码
  1. <span style="font-size: medium;">private Bitmap Bytes2Bimap(byte[] b){
  2. if(b.length!=0){
  3. return BitmapFactory.decodeByteArray(b, 0, b.length);
  4. }
  5. else {
  6. return null;
  7. }
  8. }</span>
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}


5、保存bitmap

Java代码 复制代码 收藏代码
  1. <span style="font-size: medium;">static boolean saveBitmap2file(Bitmap bmp,String filename){
  2. CompressFormat format= Bitmap.CompressFormat.JPEG;
  3. int quality = 100;
  4. OutputStream stream = null;
  5. try {
  6. stream = new FileOutputStream("/sdcard/" + filename);
  7. } catch (FileNotFoundException e) {
  8. // TODO Auto-generated catch block
  9. Generated by Foxit PDF Creator © Foxit Software
  10. http://www.foxitsoftware.com For evaluation only.
  11. e.printStackTrace();
  12. }
  13. return bmp.compress(format, quality, stream);
  14. }</span>
static boolean saveBitmap2file(Bitmap bmp,String filename){
CompressFormat format= Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;
try {
stream = new FileOutputStream("/sdcard/" + filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
e.printStackTrace();
}
return bmp.compress(format, quality, stream);
}


6、将图片按自己的要求缩放

Java代码 复制代码 收藏代码
  1. <span style="font-size: medium;">// 图片源
  2. Bitmap bm = BitmapFactory.decodeStream(getResources()
  3. .openRawResource(R.drawable.dog));
  4. // 获得图片的宽高
  5. int width = bm.getWidth();
  6. int height = bm.getHeight();
  7. // 设置想要的大小
  8. int newWidth = 320;
  9. int newHeight = 480;
  10. // 计算缩放比例
  11. float scaleWidth = ((float) newWidth) / width;
  12. float scaleHeight = ((float) newHeight) / height;
  13. // 取得想要缩放的matrix参数
  14. Matrix matrix = new Matrix();
  15. matrix.postScale(scaleWidth, scaleHeight);
  16. // 得到新的图片
  17. Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
  18. true);
  19. // 放在画布上
  20. canvas.drawBitmap(newbm, 0, 0, paint);</span>
// 图片源
Bitmap bm = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.dog));
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 设置想要的大小
int newWidth = 320;
int newHeight = 480;
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
// 放在画布上
canvas.drawBitmap(newbm, 0, 0, paint);


相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html
7、bitmap的用法小结

Java代码 复制代码 收藏代码
  1. <span style="font-size: medium;">BitmapFactory.Options option = new BitmapFactory.Options();
  2. option.inSampleSize = 2; //将图片设为原来宽高的1/2,防止内存溢出
  3. Bitmap bm = BitmapFactory.decodeFile("",option);//文件流
  4. URL url = new URL("");
  5. InputStream is = url.openStream();
  6. Bitmap bm = BitmapFactory.decodeStream(is);
  7. android:scaleType:
  8. android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /
  9. android:scaleType值的意义区别:
  10. CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分
  11. 显示
  12. CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长
  13. (宽)
  14. CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片
  15. 长/宽等于或小于View的长/宽
  16. Generated by Foxit PDF Creator © Foxit Software
  17. http://www.foxitsoftware.com For evaluation only.
  18. FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
  19. FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
  20. FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
  21. FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示
  22. MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。</span>
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 2; //将图片设为原来宽高的1/2,防止内存溢出
Bitmap bm = BitmapFactory.decodeFile("",option);//文件流
URL url = new URL("");
InputStream is = url.openStream();
Bitmap bm = BitmapFactory.decodeStream(is);
android:scaleType:
android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /
android:scaleType值的意义区别:
CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分
显示
CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长
(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片
长/宽等于或小于View的长/宽
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示
MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。




//放大缩小图片

Java代码 复制代码 收藏代码
  1. <span style="font-size: medium;">public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){
  2. int width = bitmap.getWidth();
  3. int height = bitmap.getHeight();
  4. Matrix matrix = new Matrix();
  5. float scaleWidht = ((float)w / width);
  6. float scaleHeight = ((float)h / height);
  7. matrix.postScale(scaleWidht, scaleHeight);
  8. Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
  9. true);
  10. return newbmp;
  11. }</span>
public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float)w / width);
float scaleHeight = ((float)h / height);
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
true);
return newbmp;
}


//将Drawable转化为Bitmap

Java代码 复制代码 收藏代码
  1. <span style="font-size: medium;">public static Bitmap drawableToBitmap(Drawable drawable){
  2. int width = drawable.getIntrinsicWidth();
  3. int height = drawable.getIntrinsicHeight();
  4. Bitmap bitmap = Bitmap.createBitmap(width, height,
  5. drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
  6. : Bitmap.Config.RGB_565);
  7. Canvas canvas = new Canvas(bitmap);
  8. drawable.setBounds(0,0,width,height);
  9. drawable.draw(canvas);
  10. return bitmap;
  11. Generated by Foxit PDF Creator © Foxit Software
  12. http://www.foxitsoftware.com For evaluation only.
  13. }</span>
public static Bitmap drawableToBitmap(Drawable drawable){
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height,
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0,0,width,height);
drawable.draw(canvas);
return bitmap;
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
}


//获得圆角图片的方法

Java代码 复制代码 收藏代码
  1. <span style="font-size: medium;">public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){
  2. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
  3. .getHeight(), Config.ARGB_8888);
  4. Canvas canvas = new Canvas(output);
  5. final int color = 0xff424242;
  6. final Paint paint = new Paint();
  7. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  8. final RectF rectF = new RectF(rect);
  9. paint.setAntiAlias(true);
  10. canvas.drawARGB(0, 0, 0, 0);
  11. paint.setColor(color);
  12. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  13. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  14. canvas.drawBitmap(bitmap, rect, rect, paint);
  15. return output;
  16. }</span>
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
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(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}


//获得带倒影的图片方法

Java代码 复制代码 收藏代码
  1. <span style="font-size: medium;">public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){
  2. final int reflectionGap = 4;
  3. int width = bitmap.getWidth();
  4. int height = bitmap.getHeight();
  5. Matrix matrix = new Matrix();
  6. matrix.preScale(1, -1);
  7. Bitmap reflectionImage = Bitmap.createBitmap(bitmap,
  8. 0, height/2, width, height/2, matrix, false);
  9. Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2),
  10. Config.ARGB_8888);
  11. Canvas canvas = new Canvas(bitmapWithReflection);
  12. canvas.drawBitmap(bitmap, 0, 0, null);
  13. Paint deafalutPaint = new Paint();
  14. Generated by Foxit PDF Creator © Foxit Software
  15. http://www.foxitsoftware.com For evaluation only.
  16. canvas.drawRect(0, height,width,height + reflectionGap,
  17. deafalutPaint);
  18. canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
  19. Paint paint = new Paint();
  20. LinearGradient shader = new LinearGradient(0,
  21. bitmap.getHeight(), 0, bitmapWithReflection.getHeight()
  22. + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
  23. paint.setShader(shader);
  24. // Set the Transfer mode to be porter duff and destination in
  25. paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
  26. // Draw a rectangle using the paint with our linear gradient
  27. canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
  28. + reflectionGap, paint);
  29. return bitmapWithReflection;
  30. }
  31. }<
相关文章
|
1月前
|
Android开发
[Android jni] Bitmap与Mat对象的相互转换
[Android jni] Bitmap与Mat对象的相互转换
51 0
|
2月前
|
Java Android开发 数据安全/隐私保护
安卓逆向 -- IDA基本用法
安卓逆向 -- IDA基本用法
41 0
|
3月前
|
存储 缓存 编解码
Android 性能优化: 解释Bitmap的优化策略。
Android 性能优化: 解释Bitmap的优化策略。
41 1
|
8月前
|
Android开发
Android 中选项菜单(Option menu)的用法
Android 中选项菜单(Option menu)的用法
82 0
|
4月前
|
XML Android开发 数据格式
[Android]Bitmap Drawable
[Android]Bitmap Drawable
29 0
|
4月前
|
XML 算法 Java
Android App开发之位图加工Bitmap中转换位图的像素色彩、裁剪内部区域、利用矩阵变换位图的讲解及实战(附源码和演示)
Android App开发之位图加工Bitmap中转换位图的像素色彩、裁剪内部区域、利用矩阵变换位图的讲解及实战(附源码和演示)
29 0
|
8月前
|
前端开发 Android开发
Android 中使用Canvas绘制文字和矩形,将结果呈现在Bitmap上
Android 中使用Canvas绘制文字和矩形,将结果呈现在Bitmap上
141 0
|
8月前
|
Android开发
Android studio中的调试(Debug)按钮具体用法
Android studio中的调试(Debug)按钮具体用法
120 0
|
8月前
|
XML Android开发 数据格式
Android 动画之帧动画(也叫图片动画)的用法
Android 动画之帧动画(也叫图片动画)的用法
148 0
|
8月前
|
Android开发
Android 中String.format()的用法
Android 中String.format()的用法
54 0