2016-10-25 11 views
1

私はカスタマイズされた境界線を持つ円形のビューを持ち、CircularImageViewライブラリを使用して実装され、RecyclerViewで拡張されました。これまではイメージを入れていましたが、今はそのイメージを単色の丸とテキストに置き換えたいと考えています。既存の図形でソリッドカラーをプログラム的に変更する

私は、以下の形状(shape_colored_circle.xml)を作成しました:

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

    <size 
     android:height="70dp" 
     android:width="70dp"/> 
    <solid 
     android:color="#00f"/> 
</shape> 

そしてFrameLayoutCircularImageViewと一緒TextViewを追加しました:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="vertical"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <com.mikhaellopez.circularimageview.CircularImageView 
      android:id="@+id/card_thumbnail" 
      android:layout_width="70dp" 
      android:layout_height="70dp" 
      android:src="@drawable/icon" 
      app:civ_border="true" 
      app:civ_border_color="@color/border_color" 
      app:civ_border_width="2dp" 
      app:civ_shadow="true" 
      app:civ_shadow_color="@color/grey" 
      app:civ_shadow_radius="10" 
      android:contentDescription="@string/card_thumbnail"/> 

     <TextView 
      android:id="@+id/card_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="@string/app_name" 
      android:textSize="18sp" 
      android:textStyle="bold"/> 

    </FrameLayout> 

</LinearLayout> 

私はレイアウトが少し奇妙に思えることを承知しています(なぜ「LinearLayoutのトップは必要なのですか?」)、それがなければRecyclerViewに表示されず、このレイアウト(図書館の例から取られています)が私たちのニーズに役立ちます。 onBindViewHolder

私はCircularImageViewリソースとして形状を設定します。

holder.thumbnail.setImageDrawable(activity.getResources().getDrawable(R.drawable.shape_colored_circle)); 

そして最後に、私は私が期待していたものを参照してください。問題は、私のdrawableがあらかじめ特定の色を設定していることです。したがって、私はプログラム的な形状を作成しようとしました:

GradientDrawable gd = new GradientDrawable(); 
gd.setShape(GradientDrawable.RECTANGLE); 
gd.setColor(colors[position]);    //Getting the color from a predefined array of colors 
holder.thumbnail.setImageDrawable(gd); 

をしかし、ビューは単に黒い長方形に変わります。私は、形状をプログラムで作成するためのソリューションをいくつか試してみましたが(それらのすべてがかなり似ています)、無駄です。黒い矩形が表示されるか、まったく表示されません。

何か間違っていますか?実行時にシェイプを作成する別の方法、または既存のものを変更する方法はありますか?または、必要な動作を達成するための全く異なる方法でしょうか?

ありがとうございます!

+0

を私はプログラムで色を設定しようとしたときに、私のdrawableの背景が黒くなってしまう問題が発生しました。多分似たような問題? http://stackoverflow.com/questions/24581900/android-button-background-xml-sometimes-loses-alpha-when-setting-color-filter – 0x5453

+0

@ 0x5453 'gd.setAlpha()'を追加しました。値が255の場合は黒い四角形、値が0の場合は(明らかに)見えません。あなたのソリューションは、プログラムで作成されたドロウアブルと関連がありますか? –

答えて

1

ありがとうございましたVijay Pal Vishwakarma私はこの問題を解決することができました。

まず、私はCircularImageViewのソースとして所定の形状を設定します。

<com.mikhaellopez.circularimageview.CircularImageView 
      android:id="@+id/card_thumbnail" 
      android:layout_width="70dp" 
      android:layout_height="70dp" 
      android:src="@drawable/shape_colored_circle"  //Note this line 
      app:civ_border="true" 
      app:civ_border_color="@color/border_color" 
      app:civ_border_width="2dp" 
      app:civ_shadow="true" 
      app:civ_shadow_color="@color/grey" 
      app:civ_shadow_radius="10" 
      android:contentDescription="@string/card_followee_large_thumbnail"/> 

その後、onBindViewHolderに私は既存の描画可能を取得し、その色の変更:数年前

GradientDrawable gd = (GradientDrawable) holder.thumbnail.getDrawable(); 
if (gd != null) { 
    gd.setColor(colors[position]); 
} 
+0

これは私のアプローチの正確なコピーですね。 –

+0

いいえ、ここでは 'shape_colored_circle'がソースとして使用され、バックグラウンドとしては使用されません。 –

3
<com.mikhaellopez.circularimageview.CircularImageView 
    android:id="@+id/card_thumbnail" 
    android:layout_width="70dp" 
    android:layout_height="70dp" 
    android:background="@drawable/shape_colored_circle" //Note 
    android:contentDescription="@string/card_thumbnail" 
    android:src="@drawable/icon" 
    app:civ_border="true" 
    app:civ_border_color="@color/border_color" 
    app:civ_border_width="2dp" 
    app:civ_shadow="true" 
    app:civ_shadow_color="@color/grey" 
    app:civ_shadow_radius="10"/>` 

GradientDrawable gd = (GradientDrawable) holder.thumbnail.getBackground() 
if (gd != null) { 
    gd.mutate(); 
    gd.setColor(colors[position]); 
} 
+0

問題は、 'CircularImageView'バックグラウンドが円の背後にある矩形全体であることです。したがって、私はバックグラウンドではなくソースを設定する必要があります。 –

関連する問題