Android:將View的內容對映成Bitmap轉圖片匯出
- contentLayout.setDrawingCacheEnabled(true);
- contentLayout.measure(
- MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
- MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
- contentLayout.layout(0, 0, contentLayout.getMeasuredWidth(),
- contentLayout.getMeasuredHeight());
- contentLayout.buildDrawingCache();
- //會報Canvas: trying to use a recycled bitmap android.graphics.Bitmap
- //Bitmap bitmap= contentLayout.getDrawingCache();
- //解決辦法:
//Copy the bitmap before you pass it to the static variable.
Bitmap bitmap = contentLayout.getDrawingCache(true);
//to
Bitmap bitmap
= Bitmap.createBitmap(contentLayout.getDrawingCache(true));
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.set_view);
- contentLayout = (LinearLayout) findViewById(R.id.content);
- imgSource1 = (ImageView) findViewById(R.id.imgSource1);
- imgSource2 = (ImageView) findViewById(R.id.imgSource2);
- imgCache = (ImageView) findViewById(R.id.imgCache);
- imgSource1.setImageResource(R.drawable.source1);
- imgSource2.setImageResource(R.drawable.source2);
- contentLayout.setDrawingCacheEnabled(true);
- contentLayout.measure(
- MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
- MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
- contentLayout.layout(0, 0, contentLayout.getMeasuredWidth(),
- contentLayout.getMeasuredHeight());
- contentLayout.buildDrawingCache();
- Bitmap bitmap= contentLayout.getDrawingCache();
- if(bitmap!=null){
- imgCache.setImageBitmap(bitmap);
- }else{
- Log.i("CACHE_BITMAP", "DrawingCache=null");
- }
- }
- private void addRelativeLayout() {
- // TODO Auto-generated method stub
- RelativeLayout.LayoutParams layoutpare = new RelativeLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- RelativeLayout relativeLayout = new RelativeLayout(this);
- relativeLayout.setLayoutParams(layoutpare);
- ImageView imgView1 = new ImageView(this);
- ImageView imgView2 = new ImageView(this);
- imgView1.setImageResource(R.drawable.source1);
- imgView2.setImageResource(R.drawable.source2);
- RelativeLayout.LayoutParams img1 = new RelativeLayout.LayoutParams(38,
- 38);
- img1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
- RelativeLayout.LayoutParams img2 = new RelativeLayout.LayoutParams(38,
- 38);
- img2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
- relativeLayout.addView(imgView1, img1);
- relativeLayout.addView(imgView2, img2);
- addViewContent.addView(relativeLayout);
- }
- /**
- * 新增圖片源
- */
- private void addImgSource() {
- ImageView imgView1 = new ImageView(this);
- ImageView imgView2 = new ImageView(this);
- imgView1.setImageResource(R.drawable.source1);
- imgView2.setImageResource(R.drawable.source2);
- addViewContent.addView(imgView1, new LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT));
- addViewContent.addView(imgView2, new LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT));
- }
相關文章
- 將網路圖片 轉化成bitmap
- 利用網路請求將網路圖片轉換成Bitmap格式
- Android把一個View轉成Bitmap+處理帶圓角View轉化後黑邊AndroidView
- 將 SAP ABAP 內表內容本地匯出成 Excel 檔案試讀版Excel
- html2canvas將指定區域匯出為圖片(js將div匯出為圖片)HTMLCanvasJS
- 對映本地圖片地圖
- 如何將圖片轉換成影片?
- 使用ImageMagick將PDF轉成圖片
- PDF 轉換圖片然後識別圖片內容
- 利用bitmap將圖片部分顏色透明
- Android圖片編碼機制(Bitmap,Skia,libJpeg)Android
- Android 中 Base64 轉換成 圖片Android
- 生成對抗網路,AI將圖片轉成漫畫風格AI
- 如何將heic格式轉換成jpg圖片?
- 如何將一個PDF檔案裡的圖片批量匯出
- 圖片格式轉換,JPG圖片轉換成PDF
- 將dataGridView內容匯出到Excel檔案ViewExcel
- Bitmap的圖片壓縮彙總
- 圖片怎麼轉換成PDF,圖片轉PDF教程
- PhpSpreadsheet 匯出圖片到 ExcelPHPExcel
- php將內容轉為wordPHP
- SpringBoot圖文教程10—Excel模板匯出|百萬資料匯出|圖片匯出「easypoi」Spring BootExcel
- android圖片處理,讓圖片變成圓形Android
- 線上將網頁轉成PDF檔案\JPG圖片的工具網頁
- vue+element 將圖片壓縮並轉換成base64上傳圖片Vue
- 螢幕上內容究竟是怎樣畫出來的 —— Android View 工作原理詳解AndroidView
- window caps 轉換成 ctrl 按鍵對映
- ThinkPHP6.0 內容匯出 Word 案例PHP
- Laravel maatwebsite/excel 3.1 匯出圖片LaravelWebExcel
- fastadmin匯出圖片zip檔案AST
- laravel結合maatwebsite/excel包,匯出excel,將部分內容設定不可LaravelWebExcel
- 在matlab中如何將圖片(png和jpg)轉成mat格式Matlab
- 3個方法輕鬆將圖片匯出Mac電腦照片程式Mac
- [外掛擴充套件]Fancybox圖片播放外掛,讓你的文章內容圖片可以彈出播放套件
- python: 傳送內容帶圖片的郵件Python
- Android Bitmap(點陣圖)詳解Android
- canvas元件繪製的內容匯出生成圖片儲存到相簿後開啟異常Canvas元件
- 將bmp檔案轉換成JPEG(待修改,目前可轉換但圖片倒轉)
- 將SAP CRM WebClient UI的表格匯出成PDFWebclientUI