通过不同形式获取Bitmap,以及创建Bitmap

来源:互联网 发布:java做自动化测试 编辑:程序博客网 时间:2024/06/13 11:27
package com.hqzn.view;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.util.TypedValue;


import java.io.InputStream;


/**位图获取
 * Created by Administrator on 2016/9/27.
 */
public class BitmapUtils {
    /**
     * 通过不同种形式获取资源文件
     * @param res
     * @param resId
     * @return
     */
    //通过资源ID
    public static Bitmap getBitmapFromResource(Resources res, int resId) {
        return BitmapFactory.decodeResource(res, resId);
    }
    public static Bitmap getBitmapFromResourceOptions(Resources res, int resId, BitmapFactory.Options options){
      return  BitmapFactory.decodeResource(res, resId,options);
    }
    public static Bitmap getBitmapFromResourceOptionsIO(Resources res, int resId, BitmapFactory.Options options, InputStream is, Rect rect, TypedValue typedValue){
        return  BitmapFactory.decodeResourceStream(res,typedValue,is,rect,options);
    }
    //文件
    public static Bitmap getBitmapFromFile(String pathName) {
        return BitmapFactory.decodeFile(pathName);
    }
    public static Bitmap getBitmapFromFileOptions(String pathName, BitmapFactory.Options options){
        return BitmapFactory.decodeFile(pathName,options);
    }
    //字节数组
    public static Bitmap getBytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }
    public static Bitmap getBytes2BimapOptions(byte[] b, BitmapFactory.Options options) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length,options);
        } else {
            return null;
        }
    }
    //输入流
    public static Bitmap getBitmapFromStream(InputStream inputStream) {
        return BitmapFactory.decodeStream(inputStream);
    }
    public static Bitmap getBitmapFromStreamOptions(InputStream inputStream, Rect rect, BitmapFactory.Options options){
        return BitmapFactory.decodeStream(inputStream,rect,options);
    }
    public static Bitmap getBitmapFromStreamOptionsInternal(InputStream inputStream, Rect rect, BitmapFactory.Options options){
        return BitmapFactory.decodeStream(inputStream,rect,options);
    }


    /**
     * 创建位图
     * @param src
     * @return
     */
     //以src为原图生成不可变得新图像
    public static  Bitmap createBitmap(Bitmap src){
        return Bitmap.createBitmap(src);
    }
    //以src为原图,创建新的图像,指定新图像的高宽以及是否变。
    public static  Bitmap createScaledBitmap(Bitmap src, int dstWidth,int dstHeight, boolean filter){
        return Bitmap.createScaledBitmap(src,dstWidth,dstHeight,filter);
    }
    //创建指定格式、大小的位图
    public static Bitmap createBitmap(int width, int height, Bitmap.Config config){
        return Bitmap.createBitmap(width,height,config);
    }
    //以source为原图,创建新的图片,指定起始坐标以及新图像的高宽。
    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height){
         return Bitmap.createBitmap(source,x,y,width,height);
    }
//    //
//    public static public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
}
0 0
原创粉丝点击