【问题标题】:How to add two gradients(top-bottom and bottom-top) in a single image view in android?如何在android的单个图像视图中添加两个渐变(顶部和底部)?
【发布时间】:2015-12-11 01:23:53
【问题描述】:

我正在尝试为图像顶部和底部的图像视图添加渐变。我不想在图像视图之上添加文本视图。如何实现?

【问题讨论】:

  • 如果你想在另一个视图上显示一些东西,你必须使用RelativeLayout 并覆盖视图。

标签: android


【解决方案1】:

看来您简单而干净的解决方案是将 ImageView 与 FrameLayout 包装并使用前景属性如下:

布局

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foreground="@drawable/gradient">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/male"/>
</FrameLayout>

还有渐变以防万一

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient
        android:startColor="#33000000"
        android:centerColor="@android:color/transparent"
        android:endColor="#33000000"
        android:angle="90"/>
</shape>

产生这个:

FrameLayout 是相对简单的结构,不会增加视图层次结构的复杂性,而相对于 RelativeLayout,例如会导致额外的测量。

如果您绝对必须避免添加任何额外的包装视图,那么您的下一个选择是创建一个 CustomView,从 ImageView 扩展并覆盖 onDraw() 方法,并使用渐变位图中的像素手动覆盖现有画布。

【讨论】:

  • 感谢您的回答。我实际上是用相对布局做到的。我不想用框架布局替换它。 android:foreground 属性适用于相对布局。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多