如何在android中从上到下显示渐变效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10084932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:26:41  来源:igfitidea点击:

How to display gradient effect from top to bottom in android

android

提问by mohan

can anybody tell How to dispaly gradient effect from top to bottom in android .can anybody provide example

任何人都可以告诉如何在android中从上到下显示渐变效果。任何人都可以提供示例

Thanks

谢谢

回答by Adil Soomro

make an xml file in your dawable folder name it like gradient_bg.xml

在你的 dawable 文件夹中创建一个 xml 文件,将其命名为 gradient_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="#000" 
        android:endColor="#fff"
        android:angle="90"
     />
</shape>

and set it as background to your View.

并将其设置为您的View.

android:background="@drawable/gradient_bg"

or

或者

setBackgroundResource(R.drawable.gradient_bg);

回答by confucius

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient

        android:angle="90"
        android:type="linear"
        android:startColor="#FFFFFF"
        android:endColor="#000000" />

</shape>

here if you set the angle to the 270the start color will appear at the bottom and the end color at the top if you set the angle to 90it will be reversed

在这里,如果您将角度设置为270起始颜色将出现在底部,而如果您将角度设置为顶部,则结束颜色90将被反转

回答by lopez.mikhael

You can used a custom view to do that. With this solution, it's finished the gradient shapes of all colors in your projects:

您可以使用自定义视图来做到这一点。使用此解决方案,它完成了项目中所有颜色的渐变形状:

class GradientView(context: Context, attrs: AttributeSet) : View(context, attrs) {

    // Properties
    private val paint: Paint = Paint()
    private val rect = Rect()

    //region Attributes
    var start: Int = Color.WHITE
    var end: Int = Color.WHITE
    //endregion

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        // Update Size
        val usableWidth = width - (paddingLeft + paddingRight)
        val usableHeight = height - (paddingTop + paddingBottom)
        rect.right = usableWidth
        rect.bottom = usableHeight
        // Update Color
        paint.shader = LinearGradient(0f, height.toFloat(), 0f, 0f,
                start, end, Shader.TileMode.CLAMP)
        // ReDraw
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawRect(rect, paint)
    }

}

I also create an open source project GradientViewwith this custom view:

我还使用这个自定义视图创建了一个开源项目GradientView

https://github.com/lopspower/GradientView

https://github.com/lopspower/GradientView

implementation 'com.mikhaellopez:gradientview:1.1.0'

回答by Pacific P. Regmi

To display gradient effect from top to bottom in android with transparent:

在android中以透明方式显示从上到下的渐变效果:

  1. Create a new XML file in res/drawable folder with the name gradient_transparent.xml and add following code.
  1. 在 res/drawable 文件夹中创建一个名为 gradient_transparent.xml 的新 XML 文件并添加以下代码。

.

.

  <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:startColor="#90000000"
            android:endColor="#00ffffff"
            android:angle="90"
            />
    </shape>

In XML Layout file

在 XML 布局文件中

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:background="@drawable/gradient_transparent"
                    android:orientation="vertical"
                    android:padding="@dimen/spacing_middle">
</LinearLayout>