2016-09-08 5 views
0

그래야 제대로 작동하는지 알 수 없습니다. 보기의 배경에 사용 된 드로어 블이 다른보기와 공유되는 인스턴스 인 경우 개인의 색을 변경하는 방법은 무엇입니까? 로 R.drawable.circle_shape을 가진드로어 블 싱글 톤이

:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <corners android:radius="10dip"/> 
    <solid android:color="#cccccc"/> 
</shape> 

한 조각

<ImageView 
     android:id="@+id/circle_1” 
     android:layout_width="22dp" 
     android:layout_height="22dp" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:background="@drawable/circle_shape" 
     android:shadowRadius="10.0" 
         /> 

    <ImageView 
     android:id="@+id/circle_2” 
     android:layout_width="22dp" 
     android:layout_height="22dp" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:background="@drawable/circle_shape" 
     android:shadowRadius="10.0" 
         /> 

다른 사용은 다른 조각의 목록 항목의 템플릿을위한 두 개의 인스턴스로 사용

<ImageView 
     android:id="@+id/listItem_image” 
     android:layout_width="22dp" 
     android:layout_height="22dp" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:background="@drawable/circle_shape" 
     android:shadowRadius="10.0" 
         /> 

인스턴스 c1의 원색을 변경할 때 c2 및 listItem_image chan을 발견했습니다. ges color 너무.

View c1 = (View) findViewById(R.id. circle_1);         
c1.setBackgroundResource(R.drawable.circle_shape); // with or without this it will still affect the other ImageView which also uses R.drawable.circle_shape as background 

((GradientDrawable) c1.getBackground()).setColor(intColor); 
((GradientDrawable) c1.getBackground()).setStroke(0, Color.TRANSPARENT); 

답변

1

실제로 싱글 톤이 아니지만 올바른 방향으로 가고 있습니다. 드로어 블을 가져 오면 다른 드로어 블과 상태를 공유합니다. 그래서 그 중 하나를 수정할 때 상태를 공유하는 모든 드로어 블을 수정해야합니다.

해야 할 일은 mutate drawable이므로 새 상태가 만들어집니다. 귀하의 경우는 다음과 같이 보일 것입니다 :

GradientDrawable drawable = ((GradientDrawable) c1.getBackground()).mutate(); 
drawable.setColor(intColor); 
drawable.setStroke(0, Color.TRANSPARENT); 

첫 번째 줄은 다음 두 줄이 특정 당김의 상태를 변경할 수있는 새로운 상태를 만듭니다.

+0

cool! Malcolm에게 감사드립니다. – lannyf