스마트기기개발관련/안드로이드 개발

안드로이드 버튼 그라데이션 넣어주기 관련

AlrepondTech 2020. 9. 19. 03:29
반응형

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 

//빨간색 계열 그라데이션 버튼

Button del = new Button(객체..);
del.getBackground().setColorFilter(new LightingColorFilter(0xFFFF5050, 0x0000000));

 

 

=================================

=================================

=================================

 

 


출처: http://stackoverflow.com/questions/1521640/standard-android-button-with-a-different-color

Standard Android Button with a different color

youDevise, Ltd. - Pragmatic Programmer London, England

EF Englishtown - Senior .Net Technical Lead / Architect Shanghai, China

Groupon GmbH - Java Frontend Developer Berlin, Germany

mag10 publishing - Software Engineers Web Frontend Berlin, Germany

 

<?xml version="1.0" encoding="utf-8"?>    

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

    <item android:state_pressed="true" android:drawable="@drawable/red_button_pressed" />

    <item android:state_focused="true" android:drawable="@drawable/red_button_focus" />

    <item android:drawable="@drawable/red_button_rest" />

</selector>
<?xml version="1.0" encoding="utf-8"?>

<selector

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



    <item android:state_pressed="true" >

        <shape>

            <gradient

                android:startColor="@color/yellow1"

                android:endColor="@color/yellow2"

                android:angle="270" />

            <stroke

                android:width="3dp"

                android:color="@color/grey05" />

            <corners

                android:radius="3dp" />

            <padding

                android:left="10dp"

                android:top="10dp"

                android:right="10dp"

                android:bottom="10dp" />

        </shape>

    </item>



    <item android:state_focused="true" >

        <shape>

            <gradient

                android:endColor="@color/orange4"

                android:startColor="@color/orange5"

                android:angle="270" />

            <stroke

                android:width="3dp"

                android:color="@color/grey05" />

            <corners

                android:radius="3dp" />

            <padding

                android:left="10dp"

                android:top="10dp"

                android:right="10dp"

                android:bottom="10dp" />

        </shape>

    </item>



    <item>        

        <shape>

            <gradient

                android:endColor="@color/blue2"

                android:startColor="@color/blue25"

                android:angle="270" />

            <stroke

                android:width="3dp"

                android:color="@color/grey05" />

            <corners

                android:radius="3dp" />

            <padding

                android:left="10dp"

                android:top="10dp"

                android:right="10dp"

                android:bottom="10dp" />

        </shape>

    </item>

</selector>
button.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);

will give you a red shaded button,.

 

button.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
I'd like to change the color of a standard Android button slightly in order to better match a client's branding. For example, see the "Find a Table" button for the OpenTable application:


The best way I've found to do this so far is to change the Button's drawable to the following drawable located in res/drawable/red_button.xml:
But doing that requires that I actually create three different drawables for each button I want to customize (one for the button at rest, one when focused, and one when pressed). That seems more complicated and non-DRY than I need.
All I really want to do is apply some sort of color transform to the button. Is there an easier way to go about changing a button's color than I'm doing?
android layout drawable
 

protected by Will Sep 22 '10 at 13:19

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have more than 10 reputation.

4 Answers

active oldest votes

I discovered that this can all be done in one file fairly easily. Put something like the following code in a file named custom_button.xml and then set background="@drawable/custom_button" in your button view:


show 5 more comments
Following on from Tomasz's answer, you can also programmatically set the shade of the entire button using the PorterDuff multiply mode. This will change the button colour rather than just the tint.
If you start with a standard grey shaded button:
will give you a red shaded button,
will give you a green shaded button etc., where the first value is the colour in hex format.
It works by multiplying the current button colour value by your colour value. I'm sure there's also a lot more you can do with these modes.


show 2 more comments
Mike, you might be interested in color filters. You can play with them to achieve the color you want.
An example:
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));

Nope, I think you have the right answer there. Be grateful you only need three states -- there are other possible ones (e.g., disabled, selected).
You can certainly just set the background to be a solid color, which is simpler, but it means you won't see focus, clicks, etc.

 

 

=================================

=================================

=================================

 

 

반응형