简体   繁体   中英

how to convert TextView into ImageView in android

I need to convert TextView into ImageView so I will be able to save the ImageView as an image in the sd card. How do I convert TextView into ImageView?

Thanks. (I saw the other questions but them are not realy answer my question).

Yes there is a way to do this, by two ways.

You can create an Bitmap of any View using buildDrawingCache() and getDrawingCache()

TextView tv = (TextView)findViewById(R.id.textview);
tv.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.imageview);
img.setImageBitmap(tv.getDrawingCache());

You can also check this answer for further reference.

In the other way , you can take your whole rootview as a screen shot and you can save it into disc.

This will help to achieve the above How to programatically take a screenshot on Android?

public static Bitmap convertViewToBitmap(View view)  
{  
    view.measure(
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
    );  
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());  
    view.buildDrawingCache();  
    Bitmap bitmap = view.getDrawingCache();  

    return bitmap;  
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM