转换一个TextView-> Bitmap-> ImageView的,而什么也没有显示出来什么也没、GT、TextView、ImageView

2023-09-13 00:18:48 作者:空城只有旧梦

我启动了一个测试项目只是为了获得下来。无需更改main.xml中。我想创建一个Widget大小ImageView的(80x100),其中包含从一个TextView转换成位图。是的,这听起来很迂回,但是这仅仅是用于测试;最后我想的ImageView有一个背景图像和多个TextViews。我不知道到底是什么我做错了,但没有被推到屏幕上。

I started a test project just to get this down. No changes to main.xml. I want to create a widget-sized ImageView (80x100) that contains a Bitmap converted from a TextView. Yes, that sounds very roundabout but this is just for testing; in the end I want the ImageView to have a background image and multiple TextViews. I'm not sure exactly what I'm doing wrong, but nothing is being pushed to the screen.

它是与声明的TextView / ImageView的与本在构造函数中传递给它的一个问题?是不是与我的LayoutParams有问题?这里是code:

Is it a problem with declaring the TextView/ImageView and passing it "this" in the constructor? Is it 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;
    }
}

感谢您的帮助,您可以提供。我是比较新的Andr​​oid和这一个pretty的丢失。

Thanks for any help you can provide. I'm relatively new to Android, and pretty lost with this one.

推荐答案

我相信它占用整个屏幕,因为你没有一个容器,如线性布局,然后包含与布局约束的ImageView的,所以ImageView的扩展以填充可用屏幕。试试这个:

I believe its taking up the whole screen because you don't have a container such as a Linear Layout which then contains an ImageView with layout constraints, so the ImageView expands to fill the available screen. Try this:

@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);
    }

而在你的main.xml中的文件:

And in your 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 there are more efficient ways of approaching it.