一部のコードに問題があります。ColorPicker を作成したいDialogFragment
。現時点では、 aGridView
と a customを使用して、BaseAdapter
いくつかの色の形を示しています。各色にラウンドを使用しShapeDrawable
ます。デフォルト状態の色を設定するには、次のコードを使用します。
私の「ColorAdapter」のgetView()
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(ctx);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Drawable drawable = ctx.getResources().getDrawable(R.drawable.color_item);
imageView.setBackground(drawable);
StateListDrawable states = (StateListDrawable) imageView.getBackground();
GradientDrawable shape = (GradientDrawable) states.getCurrent();
shape.setColor(colors[i]);
return imageView;
それは私にとってはうまくいきます。
しかし、アイコンが押されたときにアイコンの色を変更したい。だから私はを使用しStateListDrawable
ます。
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true" >
<item
android:state_pressed="true"
android:drawable="@drawable/circle" />
<item
android:drawable="@drawable/circle" />
</selector>
ただし、 のエントリごとに異なる色を使用しているためGridView
、実行時に状態の色を変更する必要があります。
ご覧のとおり、両方の状態に同じ描画可能リソースを使用しています。で行うように、1 つの状態の色を設定できるかもしれませんstates.getCurrent()
。
ColorFilter
また、輝度レベルを下げるために a を使用しようとしました。しかし、それをしようとすると、ImageView
押されたときに常に黒くなります。
誰もそれを行う方法を知っていますか?