渐变背景

时间:2018-12-13 17:22:25

标签: java android

制作这样的屏幕的最佳方法是什么?

enter image description here 我尝试使用GradientDrawable,但没有成功。

2 个答案:

答案 0 :(得分:0)

-尝试使用此代码,可能会帮助您:)

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:height="20dp" android:width="300dp">
            <shape>
                <solid android:color="#010000"/>
            </shape>
        </item>
        <item android:height="20dp" android:width="300dp" android:top="20dp">
            <shape>
                <solid android:color="#0F0F0F"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="40dp">
            <shape>
                <solid android:color="#1E1E1E"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="60dp">
            <shape>
                <solid android:color="#2D2D2D"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="80dp">
            <shape>
                <solid android:color="#3C3C3C"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="100dp">
            <shape>
                <solid android:color="#4C4C4C"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="120dp">
            <shape>
                <solid android:color="#5A5A5A"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="140dp">
            <shape>
                <solid android:color="#696969"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="160dp">
            <shape>
                <solid android:color="#787878"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="180dp">
            <shape>
                <solid android:color="#878787"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="200dp">
            <shape>
                <solid android:color="#969696"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="220dp">
            <shape>
                <solid android:color="#A5A5A5"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="240dp">
            <shape>
                <solid android:color="#B4B4B4"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="260dp">
            <shape>
                <solid android:color="#C3C3C3"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="280dp">
            <shape>
                <solid android:color="#D2D2D2"/>
            </shape>
        </item>

        <item android:height="20dp" android:width="300dp" android:top="300dp">
            <shape>
                <solid android:color="#E1E1E1"/>
            </shape>
        </item>
    </layer-list>

答案 1 :(得分:0)

我的方法是:

//Java code:

private void setGreyLevelBackground() {
    int currentColor = 0;
    LinearLayout linearLayout;
    LinearLayout llGreyLevel = findViewById(R.id.ll_lcd_grey_level);
    LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, 1f);

    for (int i = 0; i < 16; i++) {
        linearLayout = new LinearLayout(getApplicationContext());
        linearLayout.setLayoutParams(linearParams);
        linearLayout.setBackgroundColor(
                Color.argb(255, currentColor, currentColor, currentColor));
        llGreyLevel.addView(linearLayout);
        currentColor += 17;
    }

    llGreyLevel.setVisibility(View.VISIBLE);
}

xml:

    <LinearLayout
    android:id="@+id/ll_lcd_grey_level"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:visibility="gone">

</LinearLayout>