0
点赞
收藏
分享

微信扫一扫

Bitmap,File,Drawable相互转化以及都如何转化为Uri,以及图片叠加

静悠 2022-04-26 阅读 70
android

1.图片id → BitmapDrawable → File → Uri

 BitmapDrawable d = (BitmapDrawable) res.getDrawable(R.mipmap.图片名字,mContext.getTheme());
        String imgName = 图片名字+".png";
        String path = mContext.getFilesDir() + File.separator + imgName;
        try{
            OutputStream os = new FileOutputStream(path);
            d.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, os);
            os.close();
        }catch(Exception e){
            Log.e("TAG", "", e);
        }
File file = new File(path);
Uri  userPickedUri = Uri.fromFile(file);

2.图片叠加

        //放大缩小Bitmap
        Matrix matrix = new Matrix();
        
        //file转化为BitmapDrawable转化为Bitmap
        BitmapDrawable d =new BitmapDrawable(mContext.getResources(),file.getPath());
        Bitmap b1 = d.getBitmap();

        int btWidth = b1.getWidth();
        //x或者y缩放的比例(width为我想要缩放的大小,btWidth为需要缩放图片的大小)
        float sx = (float)width/btWidth;
        matrix.postScale(sx,sx); //长和宽放大缩小的比例

        //缩放Bitmap
        b1 = Bitmap.createBitmap(b1, 0, 0,btWidth, btWidth, matrix, false);
        //需要叠加的其他图片 :图片id转化为Bitmap
        Bitmap b2 = BitmapFactory.decodeResource(res, R.mipmap.bg_app_insatll_1);
        Bitmap b3 = BitmapFactory.decodeResource(res, R.mipmap.bg_app_install_3);
        Bitmap b4 = null;
          
        //叠加后i的图片容器
        Bitmap newBitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
        Canvas cv = new Canvas(newBitmap);
        //叠加图片时设置某一或某些图片的透明度
        Paint paint = new Paint();
        paint.setAlpha(178); 
        //第二个第三个参数为x,y,用来设置此图片在叠加图中的坐标位置
        cv.drawBitmap(b1, 0, 0, null);
        cv.drawBitmap(b2, 0, 0, paint);
        cv.drawBitmap(b3, 0, 0, paint);
举报

相关推荐

0 条评论