Jak zrobić gradientowe tło w Androidzie

Chcę stworzyć gradientowe tło jak na zdjęciu. ale nie może z powodu centerColore został rozłożony na pokrycie dołu i góry.

Jak mogę zrobić tło jak zdjęcie poniżej? albo jak zrobić małe ceterColor, które nie rozprzestrzeniają się? masz jakiś pomysł?

To jest tło, które chcę.

Tutaj wpisz opis obrazka

To jest tło, które zrobiłem.

Tutaj wpisz opis obrazka

Jest to kod w XML przycisku tła powyżej

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <gradient 
        android:startColor="#6586F0"
        android:centerColor="#D6D6D6"
        android:endColor="#4B6CD6"
        android:angle="90"/>
    <corners 
        android:radius="0dp"/>


</shape>
Author: kongkea, 2012-12-18

9 answers

Możesz stworzyć ten 'półgradientowy' wygląd, używając XML Layer-List , aby połączyć górne i dolne 'pasma' w jeden plik. Każdy zespół ma kształt xml.

Zobacz poprzednią odpowiedź na SO dla szczegółowego samouczka: kształty Multi-gradient .

 49
Author: Gunnar Karlsson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:47:24

Spróbuj z tym:

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

    <gradient
        android:angle="90"
        android:centerColor="#555994"
        android:endColor="#b5b6d2"
        android:startColor="#555994"
        android:type="linear" />

    <corners 
        android:radius="0dp"/>

</shape>
 237
Author: Pratik Sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-08 19:35:01

Wizualne przykłady pomagają w tego rodzaju pytaniach.

Boilerplate

Aby utworzyć gradient, należy utworzyć plik xml w res / drawable. Nazywam moją my_gradient_drawable.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:type="linear"
        android:angle="0"
        android:startColor="#f6ee19"
        android:endColor="#115ede" />
</shape>

Ustawiasz go na tle jakiegoś widoku. Na przykład:

<View
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="@drawable/my_gradient_drawable"/>

Type = "linear"

Ustaw angle na typ linear. To musi być wielokrotność 45 stopni.

<gradient
    android:type="linear"
    android:angle="0"
    android:startColor="#f6ee19"
    android:endColor="#115ede" />

Tutaj wpisz opis obrazka

Type = "radial"

Zestaw gradientRadius dla Typu radial. Użycie %p oznacza, że jest to procent najmniejszego wymiaru rodzica.

<gradient
    android:type="radial"
    android:gradientRadius="10%p"
    android:startColor="#f6ee19"
    android:endColor="#115ede" />

Tutaj wpisz opis obrazka

Type = "sweep"

Nie wiem, po co komu zamiatanie, ale włączam to dla kompletności. Nie mogłem wymyślić, jak zmienić kąt, więc włączam tylko jeden obraz.

<gradient
    android:type="sweep"
    android:startColor="#f6ee19"
    android:endColor="#115ede" />

Tutaj wpisz opis obrazka

Center

Możesz również zmienić środek typu sweep lub radial. Wartości są ułamki szerokości i wysokości. Można również użyć notacji %p.

android:centerX="0.2"
android:centerY="0.7"

Tutaj wpisz opis obrazka

 133
Author: Suragch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-17 03:51:45

Poniższy link może Ci pomóc http://angrytools.com/gradient /.Spowoduje to utworzenie niestandardowego gradientowego tła w Androidzie, jak w Photoshopie.

 45
Author: Hits,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-22 11:30:21

Najpierw musisz utworzyć gradient.xml jak następuje

<shape>
    <gradient android:angle="270" android:endColor="#181818" android:startColor="#616161" />

    <stroke android:width="1dp" android:color="#343434" />
</shape>

Następnie należy wspomnieć powyżej gradient w tle layout.As follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/gradient"
    >   
</LinearLayout>
 25
Author: Harish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-18 10:18:54

Lub możesz użyć w kodzie cokolwiek byś pomyślał w PSD:

    private void FillCustomGradient(View v) {
        final View view = v;
        Drawable[] layers = new Drawable[1];

        ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
            @Override
            public Shader resize(int width, int height) {
                LinearGradient lg = new LinearGradient(
                        0,
                        0,
                        0,
                        view.getHeight(),
                        new int[] {
                                 getResources().getColor(R.color.color1), // please input your color from resource for color-4
                                 getResources().getColor(R.color.color2),
                                 getResources().getColor(R.color.color3),
                                 getResources().getColor(R.color.color4)},
                        new float[] { 0, 0.49f, 0.50f, 1 },
                        Shader.TileMode.CLAMP);
                return lg;
            }
        };
        PaintDrawable p = new PaintDrawable();
        p.setShape(new RectShape());
        p.setShaderFactory(sf);
        p.setCornerRadii(new float[] { 5, 5, 5, 5, 0, 0, 0, 0 });
        layers[0] = (Drawable) p;

        LayerDrawable composite = new LayerDrawable(layers);
        view.setBackgroundDrawable(composite);
    }
 20
Author: miroslavign,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-20 09:06:08
//Color.parseColor() method allow us to convert
// a hexadecimal color string to an integer value (int color)
int[] colors = {Color.parseColor("#008000"),Color.parseColor("#ADFF2F")};

//create a new gradient color
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);

gd.setCornerRadius(0f);
//apply the button background to newly created drawable gradient
btn.setBackground(gd);

Zobacz tutaj https://android--code.blogspot.in/2015/01/android-button-gradient-color.html

 4
Author: UpendranathReddy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-27 18:54:37

Dlaczego nie stworzyć obrazu lub 9 Patch image i użyć tego?

Poniższy link ma fajny poradnik Jak to zrobić:

Http://android.amberfog.com/?p=247

Jeśli nalegasz na użycie kształtu, wypróbuj witrynę poniżej (wybierz Android w lewym dolnym rogu): http://angrytools.com/gradient/

Stworzyłem podobny gradient (nie dokładny) do tego, który masz w tym link: http://angrytools.com/gradient/?0_6586f0, 54_4B6CD6, 2_D6D6D6&0_100,100_100 & l_269

 2
Author: Feras Arabiat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-18 10:00:22

**Użyj tego kodu w folderze drawable * *

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#3f5063"
    />

<corners android:bottomRightRadius="0dp" android:bottomLeftRadius="30dp" android:topLeftRadius="30dp" android:topRightRadius="0dp" />
<padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" />
<gradient
    android:startColor="#2ea4e7"
    android:centerColor="#015664"

    android:endColor="#636969"
    android:angle="45"
    >

</gradient>
<stroke android:color="#000000"

    android:width="1dp">

</stroke>

 0
Author: Muthu Krishnan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-04 11:50:54