日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Android實現(xiàn)將View轉化為圖片并保存到本地_Android

作者:風云正 ? 更新時間: 2022-04-20 編程語言

本文實例為大家分享了Android將View轉化為圖片并保存到本地的具體代碼,供大家參考,具體內容如下

一、概述

app中有需求需要將View轉化為圖片并保存到本地,這里分兩種情況:

1.View本身已經(jīng)顯示在界面上
2.View還沒有添加到界面上或者沒有顯示(繪制)過

二、實現(xiàn)方法

對于上述的第一種情況我使用下面代碼即可:

private void viewSaveToImage(View view) {
? ? ? ? view.setDrawingCacheEnabled(true);
? ? ? ? view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
? ? ? ? view.setDrawingCacheBackgroundColor(Color.WHITE);

? ? ? ? // 把一個View轉換成圖片
? ? ? ? Bitmap cachebmp = loadBitmapFromView(view);

? ? ? ? FileOutputStream fos;
? ? ? ? String imagePath = "";
? ? ? ? try {
? ? ? ? ? ? // 判斷手機設備是否有SD卡
? ? ? ? ? ? boolean isHasSDCard = Environment.getExternalStorageState().equals(
? ? ? ? ? ? ? ? ? ? android.os.Environment.MEDIA_MOUNTED);
? ? ? ? ? ? if (isHasSDCard) {
? ? ? ? ? ? ? ? // SD卡根目錄
? ? ? ? ? ? ? ? File sdRoot = Environment.getExternalStorageDirectory();
? ? ? ? ? ? ? ? File file = new File(sdRoot, Calendar.getInstance().getTimeInMillis()+".png");
? ? ? ? ? ? ? ? fos = new FileOutputStream(file);
? ? ? ? ? ? ? ? imagePath = file.getAbsolutePath();
? ? ? ? ? ? } else
? ? ? ? ? ? ? ? throw new Exception("創(chuàng)建文件失敗!");

? ? ? ? ? ? cachebmp.compress(Bitmap.CompressFormat.PNG, 90, fos);

? ? ? ? ? ? fos.flush();
? ? ? ? ? ? fos.close();

? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? LogUtil.e("imagePath="+imagePath);

? ? ? ? view.destroyDrawingCache();
? ? }

? ? private Bitmap loadBitmapFromView(View v) {
? ? ? ? int w = v.getWidth();
? ? ? ? int h = v.getHeight();

? ? ? ? Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
? ? ? ? Canvas c = new Canvas(bmp);

? ? ? ? c.drawColor(Color.WHITE);
? ? ? ? /** 如果不設置canvas畫布為白色,則生成透明 */

? ? ? ? v.layout(0, 0, w, h);
? ? ? ? v.draw(c);

? ? ? ? return bmp;
?}

如果是第二種,還這樣使用的話,就會報錯了,因為View在添加到容器中之前并沒有得到實際的大小,所以首先需要指定View的大小:

DisplayMetrics metric = new DisplayMetrics();
? ? ? ? ? ? ? ? ? ? getWindowManager().getDefaultDisplay().getMetrics(metric);
? ? ? ? ? ? ? ? ? ? int width = metric.widthPixels; ? ? // 屏幕寬度(像素)
? ? ? ? ? ? ? ? ? ? int height = metric.heightPixels; ? // 屏幕高度(像素)
? ? ? ? ? ? ? ? ? ? View mingpianView = LayoutInflater.from(this).inflate(R.layout.view_team_mingpian, null, false);
? ? ? ? ? ? ? ? ? ? layoutView(mingpianView, width, height);
//然后View和其內部的子View都具有了實際大小,也就是完成了布局,相當與添加到了界面上。接著就可以創(chuàng)建位圖并在上面繪制了:
? ? private void layoutView(View v, int width, int height) {
? ? ? ? // 整個View的大小 參數(shù)是左上角 和右下角的坐標
? ? ? ? v.layout(0, 0, width, height);
? ? ? ? int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
? ? ? ? int measuredHeight = View.MeasureSpec.makeMeasureSpec(10000, View.MeasureSpec.AT_MOST);
? ? ? ? /** 當然,measure完后,并不會實際改變View的尺寸,需要調用View.layout方法去進行布局。
? ? ? ? ?* 按示例調用layout函數(shù)后,View的大小將會變成你想要設置成的大小。
? ? ? ? ?*/
? ? ? ? v.measure(measuredWidth, measuredHeight);
? ? ? ? v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
? ? }

然后再調用viewSaveToImage(mingpianView);方法即可。

原文鏈接:https://blog.csdn.net/chenzheng8975/article/details/76206290

欄目分類
最近更新