Convert TextView-> Bitmap-> ImageView and nothing is displayed

I started a test project just to achieve this. There are no changes to main.xml. I want to create a widget the size of an ImageView (80x100) that contains a Bitmap converted from a TextView. Yes, that sounds very cool, but it's just for testing; in the end, I want the ImageView to have a background image and several TextViews. I'm not quite sure what I'm doing wrong, but nothing pushes to the screen.

Is this a problem with declaring a TextView / ImageView and passing it "this" in the constructor? Is this a problem with my layoutParams? Here is the code:

package com.doaf.testproject; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Canvas; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class TestProject extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100); tv.setLayoutParams(layoutParams); tv.setText("testing 1 2 3"); tv.setTextColor(0xFFFFFF); tv.setBackgroundColor(0x555555); Bitmap testB; testB = loadBitmapFromView(tv); ImageView iv = new ImageView(this); iv.setLayoutParams(layoutParams); iv.setBackgroundColor(0x555555); iv.setImageBitmap(testB); setContentView(iv); } public static Bitmap loadBitmapFromView(View v) { Bitmap b = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.layout(0, 0, 80, 100); v.draw(c); return b; } } 

Thanks for any help you can provide. I am relatively new to Android and am rather overlooked with this.

+4
source share
3 answers

I believe that it takes up the whole screen because you do not have a container like Linear Layout which then contains an ImageView with layout restrictions, so the ImageView expands to fill the available screen. Try the following:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = new TextView(this); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100); tv.setLayoutParams(layoutParams); tv.setText("testing 1 2 3"); tv.setTextColor(Color.BLACK); tv.setBackgroundColor(Color.TRANSPARENT); Bitmap testB; testB = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(testB); tv.layout(0, 0, 80, 100); tv.draw(c); ImageView iv = (ImageView)findViewById(R.id.menuIcon); iv.setLayoutParams(layoutParams); iv.setBackgroundColor(Color.GRAY); iv.setImageBitmap(testB); iv.setMaxHeight(80); iv.setMaxWidth(80); } 

And in the main.xml file:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/menuIcon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 

I’m not sure what you want to achieve, but I’m sure that there are more effective ways to get it closer.

+7
source

Quick code modification, try this and see if you want to archive it:

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100); tv.setLayoutParams(layoutParams); tv.setText("testing 1 2 3"); tv.setTextColor(Color.BLACK); tv.setBackgroundColor(Color.TRANSPARENT); Bitmap testB; testB = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(testB); tv.layout(0, 0, 80, 100); tv.draw(c); ImageView iv = new ImageView(this); iv.setLayoutParams(layoutParams); iv.setBackgroundColor(Color.GRAY); iv.setImageBitmap(testB); setContentView(iv); } 
+1
source

Your TextView needs to be measured (call up the measure ()) and laid out (layout () () before you can draw it. You are not doing anything.

+1
source

Source: https://habr.com/ru/post/913686/


All Articles