2013-07-16 16 views
13

Aşağıdaki xml dosyasında tanımlanmış bir şeklim var, şimdi düz renklere programlı olarak değiştirmek istiyorum.Java Kodunda katı, köşe, konturlu şekil nasıl oluşturulur?

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

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

    <corners 
     android:bottomLeftRadius="8dp" 
     android:bottomRightRadius="8dp" 
     android:topLeftRadius="8dp" 
     android:topRightRadius="8dp" /> 

    <stroke 
     android:width="3dp" 
     android:color="#2E3135" /> 

</shape> 

ShapeDrawable'ı genişleten ve onDraw yöntemini uygulayan bir sınıfa sahip olmalıyım. Herkes bilir nasıl?

+1

Resources.getDrawable (int resId) kaynak kodunu görüntüleyebilir ve Shape etiket noktasını GradientDrawable olarak bulabilirsiniz. – yugy

+0

@yugy Size iyi bir öneriniz için teşekkürler, bu tür sorunları çözmek için iyi bir yol. – srain

cevap

20

Sonunda çalıştım!

Bunun gibi
// prepare 
int strokeWidth = 5; // 5px not dp 
int roundRadius = 15; // 15px not dp 
int strokeColor = Color.parseColor("#2E3135"); 
int fillColor = Color.parseColor("#DFDFE0"); 

GradientDrawable gd = new GradientDrawable(); 
gd.setColor(fillColor); 
gd.setCornerRadius(roundRadius); 
gd.setStroke(strokeWidth, strokeColor); 
Formda ve iç şekle dolgu eklemek için bir şekil yapmayı tercih ediyorum
+1

Lütfen bana nasıl bir daire oluşturulduğunu söyler misiniz? –

2

...

:

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="#2E3135" /> 
      <corners 
       android:bottomLeftRadius="8dp" 
       android:bottomRightRadius="8dp" 
       android:topLeftRadius="8dp" 
       android:topRightRadius="8dp" /> 
     </shape> 
    </item> 
    <item 
     android:bottom="3dp" 
     android:left="3dp" 
     android:right="3dp" 
     android:top="3dp"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#DFDFE0" /> 
      <corners 
       android:bottomLeftRadius="5dp" 
       android:bottomRightRadius="5dp" 
       android:topLeftRadius="5dp" 
       android:topRightRadius="5dp" /> 
     </shape> 
    </item> 
</layer-list> 

GÜNCELLEME: Ben köşeleri bulundu iç şekli daha iyi bir görünüm için daha küçük olmalıdır ... Farklı bir oran için, çözelti için hangi iç yarıçapı, dış yarıçapı ve strok boyutunu (dolguyu) en iyi şekilde kullanmanız gerektiğini test etmeniz gerekir

İlgili konular