web-dev-qa-db-ja.com

実行時にAndroidでグラデーションの背景色を変更する

私はDrawableの背景を試していますが、今のところ問題はありません。

実行時にグラデーションの背景色を変更しようとしています。

残念ながら、実行時に変更するAPIはありません。ここで説明するように、ドローアブルをmutate()しようとしても、 ドローアブルの変異

サンプルXMLは次のようになります。期待どおりに動作します。

<shape xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:shape="rectangle">
    <gradient
        Android:startColor="#330000FF"
        Android:endColor="#110000FF"
        Android:angle="90"/>
</shape>

悲しいことに、私はさまざまな色のリストが必要であり、それらは実行時にプログラムで変更する必要があります。

実行時にこのグラデーションの背景を作成する別の方法はありますか?おそらく、XMLを完全に使用していませんか?

23
Phenome

はい!道を見つけた!

XMLを忘れなければなりませんでしたが、ここに私がそれをした方法があります:

私のgetView()でオーバーロードされた関数(ListAdapter)で、私はちょうどしなければなりませんでした:

    int h = v.getHeight();
    ShapeDrawable mDrawable = new ShapeDrawable(new RectShape());
    mDrawable.getPaint().setShader(new LinearGradient(0, 0, 0, h, Color.parseColor("#330000FF"), Color.parseColor("#110000FF"), Shader.TileMode.REPEAT));
    v.setBackgroundDrawable(mDrawable);

これにより、上記のXMLの背景と同じ結果が得られました。これで、プログラムで背景色を設定できます。

43
Phenome

ボタン表示にフェノメのソリューションを使ってみました。しかし、どういうわけかそれは機能しませんでした。

他に思いついたもの:(提供:Android APIデモの例)

package com.example.testApp;

import Android.app.Activity;
import Android.graphics.drawable.GradientDrawable;
import Android.os.Bundle;
import Android.view.View;

public class TetApp extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View v = findViewById(R.id.btn);
        v.setBackgroundDrawable( new DrawableGradient(new int[] { 0xff666666, 0xff111111, 0xffffffff }, 0).SetTransparency(10));

    }

    public class DrawableGradient extends GradientDrawable {
        DrawableGradient(int[] colors, int cornerRadius) {
            super(GradientDrawable.Orientation.TOP_BOTTOM, colors);

            try {
                this.setShape(GradientDrawable.RECTANGLE);
                this.setGradientType(GradientDrawable.LINEAR_GRADIENT);
                this.setCornerRadius(cornerRadius);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public DrawableGradient SetTransparency(int transparencyPercent) {
            this.setAlpha(255 - ((255 * transparencyPercent) / 100));

            return this;
        }
    }
}
12
Yogesh

以下の私のコードを試してください:

 int[] colors = new int[2];
            colors[0] = getRandomColor();
            colors[1] = getRandomColor();


            GradientDrawable Gd = new GradientDrawable(
                    GradientDrawable.Orientation.TOP_BOTTOM, colors);

            Gd.setGradientType(GradientDrawable.RADIAL_GRADIENT);
            Gd.setGradientRadius(300f);
            Gd.setCornerRadius(0f);
            YourView.setBackground(Gd);

ランダムな色を生成する方法:

public static int getRandomColor(){
    Random rnd = new Random();
    return Color.argb(255, rnd.nextInt(256), rnd.nextInt(56), rnd.nextInt(256));
}
5
Rajesh.k

要件によっては、startColorとendColorに固定色の代わりに 色状態リスト を使用すると、期待どおりの結果が得られる場合があります。

0
Ted Hopp