久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將多個 TextView 保存為大分辨率圖像

Save multiple TextViews as image of large resolution(將多個 TextView 保存為大分辨率圖像)
本文介紹了將多個 TextView 保存為大分辨率圖像的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

Situation: I have a picture and user could add texts on it, change there color, size, position, rotation, font size and etc., i need to save all this texts in one image. It's ok, i'm saving them by using drawing cache.

   //RelativeLayout layout - layout with textviews
    layout.setDrawingCacheEnabled(true);
    Bitmap bitmap = null;
    if (layout.getDrawingCache() != null)
        bitmap = Bitmap.createBitmap(layout.getDrawingCache());
    layout.setDrawingCacheEnabled(false);

Problem: Result image could be small due to screen size of the user's device. I need this image in resolution of 1500-2000 px. In case of just resizing this image - text looks fuzzy and not as good as it was on the screen.

Question: Is there're some other ways to save textviews as image without just resizing and loosing quality of text?

解決方案

Ok, finally i found working solution.

The idea: user add text view on the image with 800x800 px size, do something with it and then i need to get the same image but in 2000x2000 px. The problem was - after resizing text was fuzzy and noisy. But how can i take a screenshot of not rendered view with size bigger than screen?

Here code that i used, it works just fine, i get the same image, text in the same positions, same size and etc. but no resizing noise, text look clear and not fuzzy. Also, this code save bitmap much bigger than screen size and without showing it to user.

private Bitmap makeTextLayer(int maxWidth, int maxHeight, ImageObject imageObject) {
        Context c = mContext;
        View v = LayoutInflater.from(c).inflate(R.layout.text_view_generator, new LinearLayout(c), false);
        RelativeLayout editTexts = (RelativeLayout) v.findViewById(R.id.editTexts);

        initView(v, maxWidth, maxHeight);

        for (int i = 0; i < imageObject.getEditTexts().size(); ++i) {
            ImageObject.TextInImage textInImage = imageObject.getEditTexts().get(i);
            //text view in relative layout - init his size, in my case it's as big as image
            CustomEditText editText = new CustomEditText(c);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
            params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
            // don't forget to add your view to layout, this view will be saved as screenshot
            editTexts.addView(editText, params);
            editText.getLayoutParams().width = maxWidth;
            editText.getLayoutParams().height = maxHeight;
            editText.loadTextParams(textInImage);
            editText.loadSizeAndRotation(textInImage);
            // this is important, without new init - position of text will be wrong
            initView(v, maxWidth, maxHeight);
            // and here i configure position
            editText.loadPosition();
        }

        Bitmap result = getViewBitmap(v, maxWidth, maxHeight);
        return result;
    }

    Bitmap getViewBitmap(View v, int maxWidth, int maxHeight) {
        //Get the dimensions of the view so we can re-layout the view at its current size
        //and create a bitmap of the same size
        int width = v.getWidth();
        int height = v.getHeight();

        int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

        //Cause the view to re-layout
        v.measure(measuredWidth, measuredHeight);
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

        //Create a bitmap backed Canvas to draw the view into
        Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);

        //Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas
        v.draw(c);

        return b;
    }

    private void initView(View view,  int maxWidth, int maxHeight){
        ViewGroup.LayoutParams vParams = view.getLayoutParams();
        //If the View hasn't been attached to a layout, or had LayoutParams set
        //return null, or handle this case however you want
        if (vParams == null) {
            return;
        }
        int wSpec = measureSpecFromDimension(vParams.width, maxWidth);
        int hSpec = measureSpecFromDimension(vParams.height, maxHeight);
        view.measure(wSpec, hSpec);
        int width = view.getMeasuredWidth();
        int height = view.getMeasuredHeight();
        //Cannot make a zero-width or zero-height bitmap
        if (width == 0 || height == 0) {
            return;
        }
        view.layout(0, 0, width, height);
    }

    private int measureSpecFromDimension(int dimension, int maxDimension) {
        switch (dimension) {
            case ViewGroup.LayoutParams.MATCH_PARENT:
                return View.MeasureSpec.makeMeasureSpec(maxDimension, View.MeasureSpec.EXACTLY);
            case ViewGroup.LayoutParams.WRAP_CONTENT:
                return View.MeasureSpec.makeMeasureSpec(maxDimension, View.MeasureSpec.AT_MOST);
            default:
                return View.MeasureSpec.makeMeasureSpec(dimension, View.MeasureSpec.EXACTLY);
        }
    }

I would like to thank the authors of the comments in these posts:

Converting a view to Bitmap without displaying it in Android?

Taking a "screenshot" of a specific layout in Android

Take a screenshot of a whole View

Capture whole scrollview bigger than screen

How to screenshot or snapshot a view before it's rendered?

I found my solution when read them, if my solution will not work for you - check out this posts.

這篇關于將多個 TextView 保存為大分辨率圖像的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 亚洲欧美中文日韩在线v日本 | 亚洲精品一二三区 | a级大片| 亚洲精久| 羞羞视频在线观看免费观看 | 欧美亚洲国产一区二区三区 | 久久久久久久久久久蜜桃 | 精品日韩在线 | 欧美色综合一区二区三区 | 欧美日韩国产在线观看 | 欧美精品一区三区 | 欧美黑人狂野猛交老妇 | 精品欧美色视频网站在线观看 | 日韩精品成人av | 成人三级视频 | www.国产日本 | 成人精品一区二区三区中文字幕 | 日本黄色免费大片 | 国产精品成人一区二区三区 | 国产成人99av超碰超爽 | 亚洲欧美日韩精品久久亚洲区 | 三级免费 | 日韩中文字幕一区 | 亚洲欧洲日韩精品 中文字幕 | 人成在线| 99福利视频 | 久久天堂 | 日韩欧美在线播放 | 特级特黄特色的免费大片 | 91精品国产色综合久久 | 日韩视频 中文字幕 | 日日骚av | 黄片毛片免费观看 | 日韩在线成人 | 亚洲精品免费在线观看 | 国产乱码精品一品二品 | 高清欧美性猛交xxxx黑人猛交 | 全免费a级毛片免费看视频免 | 日韩精品一区二区在线观看 | 射欧美| 国产高清免费 |