Android 自定义Layout转Bitmap并保存成照片至本地

第一种:1.获取屏幕宽高像素值并添加自定义布局View(可以不再当前UI界面显示直接可生成)

/**
     * get Screen Px
     */
    private void getCurrentScreenPx() {
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int mScreenWidth = metric.widthPixels;     // 屏幕宽度(像素)
        int mScreenHeight = metric.heightPixels;   // 屏幕高度(像素)
        //get print view
        View mPrintView = LayoutInflater.from(this).inflate(R.layout.print, null, false);
        initPrintView(mPrintView);
        Log.d("wc_test","mPrintView-getCurrentScreenPx-->"+mScreenWidth+"--->"+mScreenHeight);
        Log.d("wc_test","mPrintView--->"+mPrintView.getWidth()+"--->"+mPrintView.getHeight());
        layoutView(mPrintView, mScreenWidth, mScreenHeight);//指定打印view大小
    }

     private void layoutView(View mPrintView, int mPrintWidth, int mPrintHeight) {
        //指定整个View的大小 参数是左上角 和右下角的坐标
        mPrintView.layout(0, 0, mPrintWidth, mPrintHeight);
        int measuredWidth = View.MeasureSpec.makeMeasureSpec(mPrintWidth, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(mPrintHeight, View.MeasureSpec.AT_MOST);
        //measure完后,并不会实际改变View的尺寸,需要调用View.layout方法去进行布局
        //调用layout函数后,View的大小将会变成你想要设置成的大小
        mPrintView.measure(measuredWidth, measuredHeight);
        mPrintView.layout(0, 0,mPrintView.getMeasuredWidth(), mPrintView.getMeasuredHeight());
    }

2.进行View绘制。如果UI界面需要显示,则直接设置此bitmap即可。

private Bitmap loadBitmapFromView(ScrollView mPrintScrollView) {
        int w = mPrintScrollView.getWidth();
        int h = mPrintScrollView.getHeight();
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        canvas.drawColor(Color.WHITE);
        /** 如果不设置canvas画布为白色,则生成透明 */
        mPrintScrollView.layout(0, 0, w, h);
        mPrintScrollView.draw(canvas);
        return bmp;
    }

2.保存照片至本地,设置完view之后,记得执行

mPrintScrollView.destroyDrawingCache();
 public static void savePhotoToSDCard(Bitmap photoBitmap, String path, String photoName) {
        if (checkSDCardAvailable()) {
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            File photoFile = new File(path, photoName + ".png");
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(photoFile);
                if (photoBitmap != null) {
                    if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
                        fileOutputStream.flush();
                    }
                }
            } catch (FileNotFoundException e) {
                photoFile.delete();
                e.printStackTrace();
            } catch (IOException e) {
                photoFile.delete();
                e.printStackTrace();
            } finally {
                try {
                    if(fileOutputStream != null){
                         fileOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

/**
*check sdCard
*/
public static boolean checkSDCardAvailable() {
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }

第二种:当前view必须在视图上显示。则可以直接使用以下方式:

1.如果当前View没有超出界面

 private Bitmap getViewBitmap(View v) {
        v.clearFocus();
        v.setPressed(false);
        boolean willNotCache = v.willNotCacheDrawing();
        v.setWillNotCacheDrawing(false);
        int color = v.getDrawingCacheBackgroundColor();
        v.setDrawingCacheBackgroundColor(0);
        if (color != 0) {
            v.destroyDrawingCache();
        }
        v.buildDrawingCache();
        Bitmap cacheBitmap = v.getDrawingCache();
        if (cacheBitmap == null) {
            return null;
        }
        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
        v.destroyDrawingCache();
        v.setWillNotCacheDrawing(willNotCache);
        v.setDrawingCacheBackgroundColor(color);
        return bitmap;
    }

2.当前view超出界面

  public static Bitmap getBitmapByView(ScrollView scrollView) {
        int h = 0;
        Bitmap bitmap = null;
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            h += scrollView.getChildAt(i).getHeight();
        }
        bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
                Bitmap.Config.RGB_565);
        final Canvas canvas = new Canvas(bitmap);
        scrollView.draw(canvas);
        return bitmap;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值