77

私はテスト例を行っています。一部の画像の背景でグラデーションを使用している場合、コードは次のようになります

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


  <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:angle="180"/>
    <corners android:radius="5dp" />
   </shape>

angle上記のxmlでは、属性を取得できませんでした。しかし、値をangle少し変更すると、パターンが傾きます。誰かがそれがどのように機能するかを正確に説明できますか?

4

4 に答える 4

176

勾配は基本的に、任意の量の空間 (方向) の変化を表します。色では、角度で表される方向の色強度の変化を表します。この概念を表すいくつかの図を次に示します。
ここに画像の説明を入力

ここで、図は水平方向の色の変化を示しています (角度は 0 に設定されています)。
XML コード:

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#000000"
        android:angle="0"/>
   </shape>

ここに画像の説明を入力

この図は、垂直方向 (角度は 90 度に設定) の色の変化を示しています。
XML コード:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:startColor="#000000"
    android:angle="90"/>
 </shape>

開始色、中心色、終了色として異なる色を使用することもできます。添付したコードには、これらすべての要素が含まれています。

于 2012-08-28T08:35:48.627 に答える
17

シェイプのグラデーション カラーを指定します。属性:

android:angle 整数。グラデーションの角度 (度単位)。0 は左から右、90 は下から上です。45 の倍数である必要があります。デフォルトは 0 です。

ドキュメントの説明は、カーンの答えと矛盾しているようです??

詳細については、ドキュメントを参照してください。

于 2015-06-11T03:19:59.570 に答える
11

コードから斜めのグラデーションを作成したい場合があります。はるかに簡単で、そこから多くのオプションを開くことができます。このスニペットは私を助けました

public void SetGradient(View view) {
        GradientDrawable gd = new GradientDrawable(
                GradientDrawable.Orientation.TL_BR,
                new int[]{0xFF141a24, 0xFF293f49, 0xFF72554c});
        view.setBackground(gd);
    }

GradientDrawable クラスから利用可能な指示

/*public enum Orientation {
        *//** draw the gradient from the top to the bottom *//*
        TOP_BOTTOM,
        *//** draw the gradient from the top-right to the bottom-left *//*
        TR_BL,
        *//** draw the gradient from the right to the left *//*
        RIGHT_LEFT,
        *//** draw the gradient from the bottom-right to the top-left *//*
        BR_TL,
        *//** draw the gradient from the bottom to the top *//*
        BOTTOM_TOP,
        *//** draw the gradient from the bottom-left to the top-right *//*
        BL_TR,
        *//** draw the gradient from the left to the right *//*
        LEFT_RIGHT,
        *//** draw the gradient from the top-left to the bottom-right *//*
        TL_BR,
    }*/

フラグメントで onCreate または onCreateView からメソッドを呼び出し、親ビューを渡します(私の場合)。

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_view_parent, container);           
        ...

        SetGradient(view);

        return view;
    }
于 2016-07-06T18:30:09.393 に答える