【问题标题】:Create image from ImageView and TextView从 ImageView 和 TextView 创建图像
【发布时间】:2016-01-05 20:34:01
【问题描述】:

我有一个带有 GridView 的片段,GridView 的每个元素都有 ImageView 和 TextView。我想用 ImageView 和 TextView 创建和图像,可以吗?

这是GridView元素的代码:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="15dp"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >

    <ImageView
        android:id="@+id/imgGrid"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="false"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="false"
        android:layout_marginTop="5dp"
        android:scaleType="centerInside"
        android:src="@drawable/myImg" />

    <TextView
        android:id="@+id/nameGrid"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imgGrid"
        android:layout_centerHorizontal="true"
        android:background="@drawable/border_blue"
        android:gravity="center"
        android:text="My image"
        android:textColor="#ffffff"
        android:textStyle="bold" />

</RelativeLayout>

我希望 GridView 的每个元素都是这样的图像:

我用以下代码做圆角图像:

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, Context mContext) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int radius = Math.min(h / 2, w / 2);
    Bitmap output = Bitmap.createBitmap(w + 8, h + 8, Bitmap.Config.ARGB_8888);

    Paint p = new Paint();
    p.setAntiAlias(true);

    Canvas c = new Canvas(output);
    c.drawARGB(0, 0, 0, 0);
    p.setStyle(Paint.Style.FILL);

    c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);

    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    c.drawBitmap(bitmap, 4, 4, p);
    p.setXfermode(null);
    p.setStyle(Paint.Style.STROKE);
    p.setColor(mContext.getResources().getColor(R.color.myPrimaryColor));
    p.setStrokeWidth(4);
    c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);

    return output;
}

然后我需要将 ImageView 和 TextView 链接在一个图像中,以设置 GridView 中元素的圆角图像。我该怎么做才能达到这个效果?

提前致谢。

【问题讨论】:

  • 我真的不明白你想做什么......
  • @Pamela,你能推荐stackoverflow.com/questions/31313577/…一个吗?我已经给出了答案。请将父视图(相对布局)作为参数传递
  • 请在您的问题中解释更多。好像太难理解了
  • 请去掉:RelativeLayout 和 ImageView。您可以将图像直接放入TextView,作为compound drawable。这有助于扁平化您的布局并提高性能。

标签: android image gridview layout bitmap


【解决方案1】:

尝试在这样的相对布局中添加图像视图和文本视图

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/powered_by_google_light"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="your text here"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>
    </RelativeLayout>

这里整个相对布局就像一个图像视图,里面有一个文本视图。

【讨论】:

  • 对不起我的英语,我已经为上面的问题添加了解释。
猜你喜欢
  • 1970-01-01
  • 2011-08-04
  • 2015-11-28
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 1970-01-01
  • 2011-05-10
相关资源
最近更新 更多