0

I have a class which extends View. I want to set gradient as a background color.

    @Override
    public void onDraw(Canvas canvas)
    {

        GradientDrawable gradient1 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]  
                {Color.parseColor("#B1FCA9"),Color.parseColor("#29C413")});
        gradient1.setShape(GradientDrawable.RECTANGLE);
        gradient1.setCornerRadius(10.f);


        GradientDrawable gradient2 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]  
                {Color.parseColor("#29C413"),Color.parseColor("#B1FCA9")});
        gradient2.setShape(GradientDrawable.RECTANGLE);
        gradient2.setCornerRadius(10.f);


        if(!Const.currentLevel.isBonusLevel())
            canvas.drawBitmap(Const.backgroundBitmap, 1, 1, null);
        else if(this.bonusPicFrame == 0)
            gradient1.draw(canvas);
        else
            gradient2.draw(canvas);
}

gradient1 should appear on my screen but the screen is white.

What is the problem?

4

2 回答 2

1

You can always use a drawable resource file like this

background.xml is the name of the resource file

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#B1FCA9" android:endColor="#29C413" android:angle="270" android:type="linear"/>
    <corners radius="10dp"/>
</shape>

and set it to your view with setBackgroundResource(R.drawable.background);

于 2013-11-15T08:49:50.310 回答
0

you have to setBounds to both gradient1 and gradient2

于 2013-11-15T09:04:51.037 回答