2013-07-16 1 views
13

아래 xml 파일에 정의 된 모양이 있습니다. 이제 프로그래밍 방식으로 단색으로 변경하고 싶습니다.Java 코드에서 솔리드, 코너, 스트로크로 모양을 만드는 방법은 무엇입니까?

<?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> 

은 내가 쉐이프를 확장하고 된 onDraw 메소드를 구현하는 클래스를가한다고 생각합니다. 누구?

+1

Resources.getDrawable (int resId)의 소스 코드를 볼 수 있으며 Shape 태그가 GradientDrawable를 가리키고 있음을 알 수 있습니다. – yugy

+0

@yugy 좋은 제안을 해 주셔서 감사합니다. 이런 종류의 문제를 해결하는 좋은 방법입니다. – srain

답변

20

마지막으로, 나는 그것을 해결했습니다!

이 같이
// 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); 
나는 모양과 내부 형태에 패딩을 추가하는 형태를 만들기 위해 선호
+1

서클을 만드는 방법을 알려주십시오. –

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> 

는 UPDATE : 나는 구석 발견 더 좋은 외관을 위해 더 작아야합니다 ... 다른 비율의 경우 내부 반경, 외부 반경 및 스트로크 크기 (패딩)가 솔루션에 가장 적합한 지 테스트해야합니다