2

ボタンの背景として使用したい形状の描画可能なリソースがいくつかあり、グラデーションの開始色と終了色、およびストロークの色を除いて類似しています。問題は、このドローアブル用に類似した XML ファイルをいくつか作成したくないことです。

タグを使ってスタイルのような形状のドローアブルを継承できると思ったのですが、これは不可能です。

このDefining XML Parent Style of Gradient / Shape / Cornersを見つけましたが、それがどのように機能しているのかわかりません。彼は属性値をどのように変更しますか?

描画可能なリソースに親を使用すると、描画可能なレイヤーを重ねて描画するレイヤーリストによって背景の外観を変更できることがわかりました。しかし、レイヤーリストを使用してグラデーションとストロークの色を操作するにはどうすればよいでしょうか?

実行時にボタンの背景の外観を変更できることがわかりましたが、おそらくこれを行う簡単な方法はありますか?

4

2 に答える 2

1

私が行った最初の提案は実行可能ではありません。<shape>が a を返すと思っていましたShapeDrawableが、間違っていました。グラデーションを変更する方法は次のとおりです。

ボタンがクリックされる前 前

ボタンをクリックすると、背景が変わります。 後

これが主な活動です

public class MainActivity extends Activity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                try
                {
                    // you can use the GradientDrawable directly, check the docs for details,
                    // I extended GradientDrawable just to make myself look like a badass :)

                    MyReflection.invokeMethod(button, new Gradient(), "setBackground");
                } 

                catch (NoSuchMethodException e)
                {
                    e.printStackTrace();
                } 

                catch (InvocationTargetException e)
                {
                    e.printStackTrace();
                } 

                catch (IllegalAccessException e)
                {
                    e.printStackTrace();
                }
            }
        });

     }
}

Drawableメソッドを呼び出すオブジェクト、新しい背景として使用するオブジェクト、古いバージョンで実行するメソッド()などのないメソッドの名前を含む新しいメソッドを取得するユーティリティ メソッド。setBackground反射は遅いです。控えめに使用してください。

/*
* We want to use reflection because the View.setBackground() method
* is only available on API level 16 and up.
* If you don't understand this, you can follow the tutorial provided by
* Oracle, just search for it on Google with the keyword "reflection tutorial"
*/

public class MyReflection {

    public static void invokeMethod(Object receiverObject, Drawable background, String methodName)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
    {
        Class<?> clazz = View.class;

        Method method = clazz.getMethod(methodName, Drawable.class);

        // Allowing non-public access call to this method.
        method.setAccessible(true);
        method.invoke(receiverObject, background);
    }
}

私のカスタムグラデーション

/**
* I am extending the GradientDrawable just for fun, and also
* because I want to set the shader property for this drawable,
* you could, however, use the GradientDrawable directly.
*/
public class Gradient extends GradientDrawable {

    private Paint paint;
    private LinearGradient gradient;

    public Gradient()
    {
        super();
        // check the docs for LinearGradient for these parameters
        gradient = new LinearGradient(0,0, 100, 100, Color.BLUE, Color.GREEN,   Shader.TileMode.REPEAT);
        paint = new Paint();
        paint.setShader(gradient);
    }

    public Gradient(GradientDrawable.Orientation orientation, int[] colors)
    {
        super(orientation, colors);
        gradient = new LinearGradient(0,0, 100, 100, Color.BLUE, Color.GREEN, Shader.TileMode.REPEAT);
        paint = new Paint();
        paint.setShader(gradient);
    }

    @Override
    public void draw(Canvas canvas)
    {
        canvas.drawPaint(paint);
    }
} 

ボタンの初期背景

<?xml version="1.0" encoding="utf-8"?>
<!-- The initial shape with just a solid yellow color -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid
        android:color="#ff0"/>

</shape>
于 2015-01-15T23:19:00.653 に答える
0

リフレクションを使用する代わりに、現在のバージョンに基づいてこのコードを使用して、適切な背景を設定しました

int sdk = android.os.Built.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    setBackgroundDrawable();
} else {
    setBackground();
}
于 2015-01-18T16:37:20.980 に答える