【问题标题】:Converting a TextView->Bitmap->ImageView, and nothing's showing up转换一个TextView->Bitmap->ImageView,什么都没有出现
【发布时间】:2010-11-12 17:54:09
【问题描述】:

我开始了一个测试项目只是为了解决这个问题。 main.xml 没有变化。我想创建一个小部件大小的 ImageView (80x100),其中包含从 TextView 转换的位图。是的,这听起来很迂回,但这只是为了测试;最后我希望 ImageView 有一个背景图像和多个 TextViews。我不确定自己到底做错了什么,但没有任何内容被推送到屏幕上。

在构造函数中声明 TextView/ImageView 并传递“this”是否有问题?我的 layoutParams 有问题吗?代码如下:

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

感谢您提供的任何帮助。我对 Android 比较陌生,对这个很迷茫。

【问题讨论】:

  • 这听起来真是个坏主意。
  • @Rockmaninoff:你在使用自定义布局吗?
  • @Tilsan The Fighter:我计划使用自定义布局。最终目的是生成一个由可选背景图像和一些 TextView 组成的 LinearLayout,然后将该 LinearLayout 导出到 Bitmap,然后导出到小部件的背景(ImageView?)。
  • @Falmarri:你为什么这么说?

标签: android android-widget android-layout


【解决方案1】:

我相信它占据了整个屏幕,因为您没有像 Linear Layout 这样的容器,该容器然后包含具有布局约束的 ImageView,因此 ImageView 会扩展以填充可用屏幕。试试这个:

@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 文件中:

<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>

我不确定你想要达到什么目标,但我确信有更有效的方法来实现它。

【讨论】:

    【解决方案2】:

    快速修改代码,试试这个,看看是不是你要归档的:

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

    【讨论】:

    • 嗨,迈克,这绝对是朝着正确方向迈出的一步。背景和文本正在渲染,但它们填满了整个屏幕。你能解释一下为什么这个特定的代码有效而我的没有(试图在这里学习),也许为什么它可能会填满屏幕?我的预感是它与 layoutParams 有关......或者可能需要将 iv 放置在 Layout 内以防止它填满屏幕。谢谢!
    【解决方案3】:

    您的 TextView 需要进行测量(调用 measure())和布局(调用 layout()),然后才能绘制它。你什么都不做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      相关资源
      最近更新 更多