• drawable与Bitmap


    从资源文件目录获取图片: 

    Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.ic_launcher);
    drawable.getBounds(); // 获取Drawable的大小范围


    将Drawable转换成Bitmap

            Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.ic_launcher);
            drawable.setBounds(0,0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.draw(canvas);
    iv.setImageBitmap(bitmap); //将Drawable转换成的Bitmap设置到ImageView.

    获取设置在ImageView上的图片

     Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

    将资源文件中的图片转成Bitmap有第二种方法:

          imageView.setImageResource(R.mipmap.ic_launcher);
          Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

    二  :  将Bitmap转为Drawable的方法:

            BitmapDrawable bitmapDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
    

    参考:ImageView.setImageBitmap() 

    获取ImageView上的图片的方法  (CircleImageView)

        private Bitmap getBitmapFromDrawable(Drawable drawable) {
            if (drawable == null) {
                return null;
            }
    
            if (drawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) drawable).getBitmap();
            }else if (drawable instanceof GlideBitmapDrawable) {
                GlideBitmapDrawable glideBitmapDrawable = (GlideBitmapDrawable) drawable;
                return glideBitmapDrawable.getBitmap();
            }else if (drawable instanceof SquaringDrawable){
    
            }
         try {
                Bitmap bitmap;
                if (drawable instanceof ColorDrawable) {
                    bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
                } else {
                    bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
                }
    
                Canvas canvas = new Canvas(bitmap);
                drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
                drawable.draw(canvas);
                return bitmap;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
  • 相关阅读:
    月经贴——.net前景何妨!
    [Asp.net 5] Options-配置文件之后的配置
    [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(5)(IEnumerable<>补充)
    [Asp.net 5] Logging-其他日志系统的实现
    [Asp.net 5] Logging-日志系统的基本架构(下)
    [Asp.net 5] Logging-日志系统的基本架构(上)
    [Asp.net 5] Logging-新日志系统目录
    [Asp.net 5] Localization-简单易用的本地化
    [Asp.net 5] Localization-Asp.net运行时多语言
    java-多线程安全问题
  • 原文地址:https://www.cnblogs.com/huyang011/p/7662405.html
Copyright © 2020-2023  润新知