2017-01-04 4 views
0

맞춤 설정 상태에 따라 캔버스에 페인팅 할 때 사용할 색상을 정의하고 싶습니다.Android : 선택기를 사용하여 맞춤 색상 속성 업데이트

고해상도/레이아웃 /은 content.xml에서

가 :

<resource> 
    <declare-styleable name="MyView"> 
     <attr name="primary_color" format="reference"/> 
    </declare-styleable> 
</resource> 

my_selector :

<com.example.package.MyView 
    app:primary_color="@drawable/my_selector" 
/> 

primary_color는 고해상도/값/attrs.xml이에 정의 된 사용자 지정 특성입니다 이것은 내가 가지고 얼마나 멀리입니다 res/drawable/my_selector.xml에 정의되어 있습니다.

<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res/com.example.package"> 
    <item 
     app:state_a="true" 
     android:drawable="@drawable/red" /> 
    <item 
     app:state_b="true" 
     android:drawable="@drawable/orange" /> 
    <item 
     app:state_c="true" 
     android:drawable="@drawable/red" /> 
</selector> 

빨강, 오렌지 및 빨강은 res/values ​​/ colordrawab에 정의되어 있습니다. le.xml는 :

<resources> 
    <drawable name="red">#f00</drawable> 
    <drawable name="orange">#fb0</drawable> 
    <drawable name="green">#0f0</drawable> 
</resources> 

MYVIEW에서 나는이 당김 얻을 수 있습니다 :

setBackground(primaryColor); 

그러나 I :

StateListDrawable primaryColor; 

public MyView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    try{ 

     primaryColor = (StateListDrawable) a.getDrawable(
      R.styleable.MyView_primary_color); 
    }finally { 
     a.recycle(); 
    } 
} 

primaryColor가 다른 상태에서 제대로 업데이트, 나는 호출하여 테스트 할 수 있습니다 페인트와 함께이 색상을 사용하려면 다음과 같이하십시오.

paint.setColor(primaryColor); 

하지만 분명히 허용되지 않습니다. primaryColor를 getColor() 메서드가있는 ColorDrawable으로 변환하려고했지만 가능한 경우이를 수행하는 방법을 알아낼 수 없습니다.

선택기에서보기에서 사용할 수있는 색상을 얻는 방법에 대한 제안은 훌륭합니다.

답변

0

ColorStateList 내가 정확히 필요한 것으로 밝혀졌습니다. 다음은 현재 구현 된 버전의 단순화 된 버전입니다. 다른 사람이 내가했던 것처럼 똑같은 경우가 있습니다. 입술/색상/my_selector.xml 입술/레이아웃 /은 content.xml에서

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

    <item app:state_weak="true" android:color="#F00" /> 
    <item app:state_average="true" android:color="#0F0" /> 
    <item app:state_strong="true" android:color="#00F" /> 
    <item android:color="#FA0" /> 
</selector> 

(이 감싸 다른 레이아웃을 갖지만, 그 관련이없는)

<com.example.package.MyView 
    android:id="@+id/strMeter" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:primary_color="@color/my_selector" 
    /> 

primary_color에서

이다는

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyView"> 
     <attr name="primary_color" format="reference"/> 
    </declare-styleable> 
</resources> 

내가 ColorStateList에 대한 참조를 얻을 고해상도/값/attrs.xml이에 참조로 정의

ColorStateList primaryColor; 

public PasswordStrengthBar(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    try{ 
     primaryColor = a.getColorStateList(
      R.styleable.MyView_primary_color); 
    }finally { 
     a.recycle(); 
    } 
} 

나는 현재 상태에 대한 색상을 얻으려면 : 사용자 정의 상태를 구현하는 경우

int color = secondaryColor.getColorForState(
       getDrawableState(), primaryColor.getDefaultColor()); 

내가 그랬던 것처럼, 당신도 재정의해야합니다 MYVIEW의 생성자 onCreateDrawableState 실제로 상태를 업데이트 할 상태이지만 해당 문서/게시물이 많이 있습니다.

관련 문제