Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display gradient effect from top to bottom in android

Tags:

android

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

Thanks

like image 508
mohan Avatar asked Apr 10 '12 07:04

mohan


People also ask

What is GradientDrawable?

A GradientDrawable is drawable with a color gradient for buttons, backgrounds, etc.

How do I create a gradient background in XML?

Inside the XAML Grid element, place a Grid. Background element. Inside that place a gradient brush element. This example uses a LinearGradientBrush that shades colors from the control's upper left corner (0, 0) to its lower right corner (1, 1).

How do you color a gradient in XML?

To create a gradient color we need to create a . xml file in the drawable folder. So go to app -> res -> drawable and right-click on drawable -> New -> Drawable Resource File and create gradient_drawable.


3 Answers

make an xml file in your dawable folder name it like 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.

android:background="@drawable/gradient_bg"

or

setBackgroundResource(R.drawable.gradient_bg);
like image 186
Adil Soomro Avatar answered Oct 23 '22 18:10

Adil Soomro


<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 270 the start color will appear at the bottom and the end color at the top if you set the angle to 90 it will be reversed

like image 30
confucius Avatar answered Oct 23 '22 17:10

confucius


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 GradientView with this custom view:

https://github.com/lopspower/GradientView

implementation 'com.mikhaellopez:gradientview:1.1.0'
like image 1
lopez.mikhael Avatar answered Oct 23 '22 18:10

lopez.mikhael