2011-08-22 6 views
27

私が達成しようとしているのは、内部にいくつかのレイヤーを持つDrawableを使用することですが、実行時にグラデーションのstartColorなどのいくつかの値を制御します。Android - ShapeDrawableをプログラムで定義する方法

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item> 
    <shape android:shape="rectangle"> 
     <stroke android:width="1dp" android:color="#FF000000" /> 
     <solid android:color="#FFFFFFFF" /> 
    </shape> 
    </item> 
    <item android:top="1dp" android:bottom="1dp"> 
    <shape android:shape="rectangle"> 
    <stroke android:width="1dp" android:color="#FF000000" /> 
    <gradient 
     android:startColor="#FFFFFFFF" 
     android:centerColor="#FFFFFF88" 
     android:endColor="#FFFFFFFF" 
     android:gradientRadius="250" 
     android:centerX="1" 
     android:centerY="0" 
     android:angle="315" 
    />    
    </shape> 
    </item> 
</layer-list> 

をそして、私はmMyImageView.setBackgroundResource(R.drawable.my_layered_shape)を使用する場合、それは動作します:ここで私はmy_layered_shape.xmlに持っているものです。 色々な色の値を取得する方法がある限り、私は必要があればxmlを分割しても構いません。私がプログラム的に考えているコンセプト(つまり、このxmlと同じコードを実行する際のベストショット)は次のとおりです。

Drawable[] layers = new Drawable[2]; 

ShapeDrawable sd1 = new ShapeDrawable(new RectShape()); 
sd1.getPaint().setColor(0xFFFFFFFF); 
sd1.getPaint().setStyle(Style.STROKE); 
sd1.getPaint().setStrokeWidth(1); 
// sd1.getPaint().somehow_set_stroke_color? 

ShapeDrawable sd2 = new ShapeDrawable(new RectShape()); 
sd2.getPaint().setColor(0xFF000000); 
sd2.getPaint().setStyle(Style.STROKE); 
// sd2.getPaint().somehow_set_stroke_color? 
// sd2.getPaint().somehow_set_gradient_params? 

layers[0] = sd1; 
layers[1] = sd2; 
LayerDrawable composite = new LayerDrawable(layers); 
mMyImageView.setBackgroundDrawable(composite); 

ありがとうございます。

答えて

25

それはShapeDrawableで動作しますが、私のGradientDrawable例を見てみていないようです:

GradientDrawable gd = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.RED, Color.GREEN}); 
gd.setStroke(10, Color.BLUE); 

あなたはまた、次のような方法が必要な場合があります。

gd.setGradientCenter(float x, float y); 
gd.setGradientRadius(float gradientRadius); 
+7

不思議なことに、GradientDrawableクラスには「setPadding」メソッドがありません。 – Palani

+0

優秀、それは私の問題を解決した – Goofy

5

ジャストつもりがこれをここに残す...まだテストされていない

/** 
* Created by Nedo on 09.04.2015. 
*/ 
public class ShapeBuilder { 

    public static Drawable generateSelectorFromDrawables(Drawable pressed, Drawable normal) { 
     StateListDrawable states = new StateListDrawable(); 
     states.addState(new int[]{ -android.R.attr.state_focused, -android.R.attr.state_pressed, -android.R.attr.state_selected}, normal); 
     states.addState(new int[]{ android.R.attr.state_pressed}, pressed); 
     states.addState(new int[]{ android.R.attr.state_focused}, pressed); 
     states.addState(new int[]{ android.R.attr.state_selected}, pressed); 

     return states; 
    } 

    public static Drawable generateShape(String colorTop, String colorBot, String colorStroke, int stokeSize, float strokeRadius) { 
     int top, bot, stroke; 
     top = Color.parseColor(colorTop); 
     bot = Color.parseColor(colorBot); 
     stroke = Color.parseColor(colorStroke); 

     GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{top, bot}); 
     drawable.setStroke(stokeSize, stroke); 
     drawable.setCornerRadius(strokeRadius); 

     return drawable; 
    } 

    public static Drawable buildSelectorShapeFromColors(String colorNormalStroke, String colorNormalBackTop, String colorNormalBackBot, 
                 String colorPressedStroke, String colorPressedBackTop, String colorPressedBackBot, 
                 int strokeSize, float strokeRadius) { 

     Drawable pressed = generateShape(colorPressedBackTop, colorPressedBackBot, colorPressedStroke, strokeSize, strokeRadius); 
     Drawable normal = generateShape(colorNormalBackTop, colorNormalBackBot, colorNormalStroke, strokeSize, strokeRadius); 
     return generateSelectorFromDrawables(pressed, normal); 
    } 
} 

編集:テストされましたが、間違いが1つありました。 実際はすべての単一の状態を記述する必要があります。状態をグループ化すると、すべてが一度にアジャストされた場合にのみトリガーされます。

関連する問題