正在将所有项目的颜色更改为列表中的最后一项,例如,如果最后一种颜色为黑色,则所有项目都为黑色(而不是正确的颜色)
ListView lv = (ListView) convertView.findViewById(R.id.lv);
LogAdapter la = new LogAdapter(m_this,R.layout.logspinner,data);
lv.setAdapter(la);
请参阅下面的课程 - ICON是每个项目应该是唯一的图像视图
我传递的是不同的十六进制值 - 当我逐步执行代码时,正确使用了十六进制的calue
public class LogAdapter extends ArrayAdapter<HashMap<String, ?>> {
public LogAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public LogAdapter(Context context, int resource, ArrayList<HashMap<String, ?>> items) {
super(context, resource, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.logspinner, null);
}
HashMap<String, ?> p = getItem(position);
if (p != null) {
ImageView imageV = (ImageView) v.findViewById(R.id.imageView1);
if(imageV != null) {
Object o = p.get("Icon");
if (o != null) {
String colorhex = (String.valueOf(o));
Drawable myIcon = getContext().getResources().getDrawable(R.drawable.mark_ffffff);
myIcon.setColorFilter(colorhex, PorterDuff.Mode.MULTIPLY);
imageV.setImageDrawable(myIcon);
}
}
}
return v;
}}
尝试修改:
if(imageV != null) {
Object o = p.get("Icon");
if (o != null) {
String colorhex = (String.valueOf(o));
Drawable myIcon = getContext().getResources().getDrawable(R.drawable.mark_ffffff);
myIcon.setColorFilter(PColor(colorhex), PorterDuff.Mode.MULTIPLY);
Bitmap b = drawableToBitmap(myIcon);
imageV.setImageBitmap(b);
}
}
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
答案 0 :(得分:0)
添加mutate修复了问题
Drawable myIcon = getContext().getResources().getDrawable(R.drawable.mark_ffffff);
myIcon = myIcon.mutate();
myIcon.setColorFilter(PColor(colorhex), PorterDuff.Mode.MULTIPLY);
源: http://android-developers.blogspot.com/2009/05/drawable-mutations.html
答案 1 :(得分:-1)
Context.getResources()中的资源仅预先实例化一次,以便更好地重用并减少内存压力。因此,基本上你使用的是Drawable的相同实例,因此在这种情况下只适用最后一个颜色过滤器。
此外,应该使用Drawables来实际绘制画布。您应该能够1)使用Bitmap创建new Canvas(Bitmap)和Canvas,2)将此Drawable绘制到该Canvas,以及3)将Bitmap设置为ImageView。