本文共 10779 字,大约阅读时间需要 35 分钟。
原文地址:http://blog.csdn.net/ymangu666/article/details/37729109
1. BitMap类
public void recycle()——回收位图占用的内存空间,把位图标记为Dead public final boolean isRecycled() ——判断位图内存是否已释放 public final int getWidth()——获取位图的宽度 public final int getHeight()——获取位图的高度 public final boolean isMutable()——图片是否可修改 public int getScaledWidth(Canvas canvas)——获取指定密度转换后的图像的宽度 public int getScaledHeight(Canvas canvas)——获取指定密度转换后的图像的高度 public boolean compress(CompressFormat format, int quality, OutputStream stream)——按指定的图片格式以及画质,将图片转换为输出流。 format:Bitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG quality:画质,0-100.0表示最低画质压缩,100以最高画质压缩。对于PNG等无损格式的图片,会忽略此项设置。 常用的静态方法: public static Bitmap createBitmap(Bitmap src) ——以src为原图生成不可变得新图像 public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)——以src为原图,创建新的图像,指定新图像的高宽以及是否可变。 public static Bitmap createBitmap(int width, int height, Config config)——创建指定格式、大小的位图 public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)以source为原图,创建新的图片,指定起始坐标以及新图像的高宽。 2. BitmapFactory工厂类:
Option 参数类: public boolean inJustDecodeBounds——如果设置为true,不获取图片,不分配内存,但会返回图片的高度宽度信息。 public int inSampleSize——图片缩放的倍数。如果设为4,则宽和高都为原来的1/4,则图是原来的1/16。 public int outWidth——获取图片的宽度值 public int outHeight——获取图片的高度值 public int inDensity——用于位图的像素压缩比 public int inTargetDensity——用于目标位图的像素压缩比(要生成的位图) public boolean inScaled——设置为true时进行图片压缩,从inDensity到inTargetDensity。 使用BitmapFactory 可从资源files, streams, and byte-arrays中解码生成Bitmap对象。 读取一个文件路径得到一个位图。如果指定文件为空或者不能解码成文件,则返回NULL。 public static Bitmap decodeFile(String pathName, Options opts) public static Bitmap decodeFile(String pathName) 读取一个资源文件得到一个位图。如果位图数据不能被解码,或者opts参数只请求大小信息时,则返回NuLL。 (即当Options.inJustDecodeBounds=true,只请求图片的大小信息。) public static Bitmap decodeResource(Resources res, int id) public static Bitmap decodeResource(Resources res, int id, Options opts) 从输入流中解码位图 public static Bitmap decodeStream(InputStream is) 从字节数组中解码生成不可变的位图 public static Bitmap decodeByteArray(byte[] data, int offset, int length) BitmapDrawable类:继承于Drawable,你可以从文件路径、输入流、XML文件以及Bitmap中创建。 常用的构造函数: Resources res=getResources();//获取资源 public BitmapDrawable(Resources res)——创建一个空的drawable。(Response用来指定初始时所用的像素密度)替代public BitmapDrawable()方法(此方法不处理像素密度) public BitmapDrawable(Resources res, Bitmap bitmap)——Create drawable from a bitmap public BitmapDrawable(Resources res, String filepath)——Create a drawable by opening a given file path and decoding the bitmap. public BitmapDrawable(Resources res, java.io.InputStream is)——Create a drawable by decoding a bitmap from the given input stream.
2).显示Bitmap 2.1) 使用Canvas类显示位图
- public class Main extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new Panel(this));
- }
- class Panel extends View{
- public Panel(Context context) {
- super(context);
- }
- public void onDraw(Canvas canvas){
- Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);
- canvas.drawColor(Color.BLACK);
- canvas.drawBitmap(bmp, 10, 10, null);
- }
- }
- }
2.2) 通过BitmapDrawable显示位图 -
- Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);
-
- BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
-
- ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);
- iv2.setImageDrawable(bmpDraw);
摘自:http://www.myexception.cn/android/1353713.html
Android中Bitmap的常见操作整理一览,需要的朋友可以参考下
摘自: -
-
-
-
-
-
- public class MainActivity extends Activity {
- private ImageView imageView;
- private Bitmap copyRawBitmap1;
- private Bitmap copyRawBitmap2;
- private Bitmap copyRawBitmap3;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- imageView = (ImageView) findViewById(R.id.imageView);
-
- Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.haha);
- copyRawBitmap1=rawBitmap;
- copyRawBitmap2=rawBitmap;
- copyRawBitmap3=rawBitmap;
-
- String SDCarePath=Environment.getExternalStorageDirectory().toString();
- String filePath=SDCarePath+"/"+"haha.jpg";
- Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null);
-
- InputStream inputStream=getBitmapInputStreamFromSDCard("haha.jpg");
- Bitmap rawBitmap2 = BitmapFactory.decodeStream(inputStream);
-
-
- Bitmap roundCornerBitmap=this.toRoundCorner(rawBitmap, 40);
- imageView.setImageBitmap(roundCornerBitmap);
-
-
-
-
- int rawHeight = rawBitmap.getHeight();
- int rawWidth = rawBitmap.getWidth();
-
- int newHeight = 500;
- int newWidth = 500;
-
- float heightScale = ((float) newHeight) / rawHeight;
- float widthScale = ((float) newWidth) / rawWidth;
-
- Matrix matrix = new Matrix();
- matrix.postScale(heightScale, widthScale);
-
-
-
-
-
-
- Bitmap newBitmap = Bitmap.createBitmap(rawBitmap, 0, 0, rawWidth,rawWidth, matrix, true);
-
- Drawable newBitmapDrawable = new BitmapDrawable(newBitmap);
- imageView.setImageDrawable(newBitmapDrawable);
-
- this.compressAndSaveBitmapToSDCard(newBitmap, "xx100.jpg", 80);
-
-
-
-
-
-
-
-
-
-
- this.compressAndSaveBitmapToSDCard(copyRawBitmap1,"0011fa.jpg",80);
-
-
-
- String SDCarePath1=Environment.getExternalStorageDirectory().toString();
- String filePath1=SDCarePath1+"/"+"haha.jpg";
- Bitmap bitmapThumbnail1=this.getBitmapThumbnail(filePath1);
- imageView.setImageBitmap(bitmapThumbnail1);
-
-
-
- String SDCarePath2=Environment.getExternalStorageDirectory().toString();
- String filePath2=SDCarePath2+"/"+"haha.jpg";
- Bitmap tempBitmap=BitmapFactory.decodeFile(filePath2);
- Bitmap bitmapThumbnail2=ThumbnailUtils.extractThumbnail(tempBitmap, 100, 100);
- imageView.setImageBitmap(bitmapThumbnail2);
-
-
- }
-
- private InputStream getBitmapInputStreamFromSDCard(String fileName){
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- String SDCarePath=Environment.getExternalStorageDirectory().toString();
- String filePath=SDCarePath+File.separator+fileName;
- File file=new File(filePath);
- try {
- FileInputStream fileInputStream=new FileInputStream(file);
- return fileInputStream;
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- return null;
- }
-
-
-
- private String getSDCardPath() {
- String SDCardPath = null;
-
- boolean IsSDcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
- if (IsSDcardExist) {
- SDCardPath = Environment.getExternalStorageDirectory().toString();
- }
- return SDCardPath;
- }
-
- private void compressAndSaveBitmapToSDCard(Bitmap rawBitmap,String fileName,int quality){
- String saveFilePaht=this.getSDCardPath()+File.separator+fileName;
- File saveFile=new File(saveFilePaht);
- if (!saveFile.exists()) {
- try {
- saveFile.createNewFile();
- FileOutputStream fileOutputStream=new FileOutputStream(saveFile);
- if (fileOutputStream!=null) {
-
-
-
-
-
- rawBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
- }
- fileOutputStream.flush();
- fileOutputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
-
- }
- }
- }
-
-
- private Bitmap getBitmapThumbnail(String filePath){
- BitmapFactory.Options options=new BitmapFactory.Options();
-
- options.inJustDecodeBounds=true;
-
- Bitmap rawBitmap = BitmapFactory.decodeFile(filePath, options);
- if (rawBitmap==null) {
- System.out.println("此时rawBitmap为null");
- }
-
-
-
- int sampleSize=computeSampleSize(options, 150, 200*200);
-
- options.inJustDecodeBounds = false;
- options.inSampleSize = sampleSize;
-
-
-
-
-
- Bitmap thumbnailBitmap=BitmapFactory.decodeFile(filePath, options);
-
- this.compressAndSaveBitmapToSDCard(thumbnailBitmap, "15.jpg", 80);
- return thumbnailBitmap;
- }
-
-
-
-
-
-
- public static int computeSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
- int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);
- int roundedSize;
- if (initialSize <= 8) {
- roundedSize = 1;
- while (roundedSize < initialSize) {
- roundedSize <<= 1;
- }
- } else {
- roundedSize = (initialSize + 7) / 8 * 8;
- }
- return roundedSize;
- }
-
- private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
-
- double w = options.outWidth;
-
- double h = options.outHeight;
- System.out.println("========== w="+w+",h="+h);
- int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
- .sqrt(w * h / maxNumOfPixels));
- int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
- Math.floor(w / minSideLength), Math.floor(h / minSideLength));
- if (upperBound < lowerBound) {
-
- return lowerBound;
- }
- if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
- return 1;
- } else if (minSideLength == -1) {
- return lowerBound;
- } else {
- return upperBound;
- }
- }
-
-
-
-
-
-
-
-
- public Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
- Bitmap roundCornerBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
- Canvas canvas = new Canvas(roundCornerBitmap);
- int color = 0xff424242;
- Paint paint = new Paint();
- paint.setColor(color);
-
- paint.setAntiAlias(true);
- Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
- RectF rectF = new RectF(rect);
- float roundPx = pixels;
-
- canvas.drawARGB(0, 0, 0, 0);
-
- canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
- paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
-
- canvas.drawBitmap(bitmap, rect, rect, paint);
- return roundCornerBitmap;
- }
-
- }
转载地址:https://blog.csdn.net/happy_love1990/article/details/76391516 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!