Android Bitmap最全基本操作:与Drawable 、File、btye[]互转

原创 2018年04月17日 03:01:21
/**
 * Created by kingadmin on 2018/4/17.
 */

public class BitmapUtil {

    private static BitmapUtil btimapUtil;
    private Context context;

    private BitmapUtil(Context context) {
        this.context = context;
    }

    public static BitmapUtil getBtimapUtil(Context context) {
        if (btimapUtil == null) {
            synchronized (BitmapUtil.class) {
                btimapUtil = new BitmapUtil(context);
            }
        }
        return btimapUtil;
    }

    /**
     * 文件转Bitmap
     */
    public Bitmap fileToBitmap(String filePath) {
        File file = new File(filePath);
        BitmapFactory.Options options = new BitmapFactory.Options();
        /**
         *压缩长宽各为一半避免图片过大装载不了
         */
        options.inPurgeable = true;
        options.inSampleSize = 2;
        return BitmapFactory.decodeFile(filePath, options);
    }

    /**
     * Bitmap转文件
     */
    public File bitmapToFile(Bitmap bitmap, String saveFilePath) {
        File file = new File(saveFilePath);//将要保存图片的路径
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
            return file;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 数组转Bitmap
     */
    public Bitmap btyesToBtimap(byte[] bytes) {
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }

    /**
     * Btimap转数组
     */
    public byte[] btimapToBtyes(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        return baos.toByteArray();
    }

    /**
     * Bitmap转Drawable
     */
    public Drawable btimapToDrawable(Bitmap bitmap) {
        return new BitmapDrawable(context.getResources(), bitmap);
    }

    /**
     * Drawable转Bitmap
     */
    public Bitmap drawableToBitmap(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.createBitmap(w, h, config);
        //建立对应 bitmap 的画布
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        // 把 drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }

    /**
     * 带圆角的绘制转Bitmap
     */
    public Bitmap creatRoundedBitmap(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));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    /**
     * 带倒影的绘制Bitmap
     */
    public Bitmap createReflectionBitmap(Bitmap bitmap) {
        final int reflectionGap = 4;
        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), 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(),
                0, bitmapWithReflection.getHeight() + reflectionGap,
                0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
        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, w, bitmapWithReflection.getHeight() + reflectionGap, paint);
        return bitmapWithReflection;
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29856589/article/details/79969030

Android Bitmap最全基本操作:与Drawable 、File、btye[]互转(Kotlin 版)

/** * Created by kingadmin on 2018/4/17. */ class BitmapUtil private constructor(private val cont...
  • qq_29856589
  • qq_29856589
  • 2018-04-17 03:12:26
  • 6756

Bitmap与byte、file、Drawable之间的相互转换

1、Bitmap转换成文件:     (1)、根据指定的文件,获取这个文件的输出流     (2)、通过使用Bitmap的compress方法,设置图片存储格式,图片的质量,并制定图片的存储的输出...
  • u011043551
  • u011043551
  • 2016-08-05 20:21:53
  • 2728

Android中 Bitmap和Drawable相互转换的方法

1.Drawable—>Bitmap Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R....
  • hezhipin610039
  • hezhipin610039
  • 2012-08-23 11:47:06
  • 151651

Android Drawable Resource学习(二)、BitmapDrawable和Bitmap

一、如何创建一个BitmapDrawable对象 Bitmap,代表一个位图图像,Android支持三种格式的位图图像:.png (preferred),.jpg (acceptable), .gi...
  • LonelyRoamer
  • LonelyRoamer
  • 2012-11-08 11:56:08
  • 34061

资源文件转换成Drawable或Bitmap

1、 Resources resources = mContext.getResources(); Drawable drawable = resources.getDrawable(R....
  • dxj151364136
  • dxj151364136
  • 2015-01-18 10:22:50
  • 1530

android如何获取res文件夹下drawable文件夹下的图片资源文件转成一个BitMap对象

网上有很多方法: bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.img);...
  • u013337840
  • u013337840
  • 2014-08-06 19:14:29
  • 5189

Android常用的Drawable和Bitmap之间的转化方法

bitmap和Drawable间的区别: Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB888。作为一种逐像素的显示对象执行效率高,但是缺点也很明...
  • zw904448290
  • zw904448290
  • 2016-11-07 18:45:02
  • 12156

android 获取资源文件 r.drawable中的图片转换为drawable、bitmap

android 获取资源文件 r.drawable中的图片转换为drawable、bitmap 转载自http://cfanz.cn/index.php?c=article&a=read&id=...
  • u013096866
  • u013096866
  • 2015-03-21 15:32:45
  • 14449

Android开发中Bitmap和Drawable的区别

- Drawable和Bitmap之间可以互相转换,Drawable占用内存远小于Bitmap。- setImageDrawable使用资源文件;setImageBitmap使用bitmap图片,该图...
  • liu537192
  • liu537192
  • 2015-08-14 17:37:46
  • 1607

Android中Drawable与Bitmap相互转换的方法

1..Bitmap转Drawable Drawable drawable =new BitmapDrawable(bmp); 2.Drawable转Bitmap ...
  • baidu_33512336
  • baidu_33512336
  • 2017-04-09 11:12:52
  • 999
收藏助手
不良信息举报
您举报文章:Android Bitmap最全基本操作:与Drawable 、File、btye[]互转
举报原因:
原因补充:

(最多只允许输入30个字)