如何设置不同的颜色以循环绘制

时间:2017-09-12 10:45:06

标签: android colors

首先我在循环中创建动态imagview,然后设置背景图像,之后我采用drawable并使用以下代码将颜色设置为我的imageview drawable,

int[] colorcodes = {Color.BLUE, Color.BLACK, Color.GREEN};
for (int i = 0; i < medicine.size(); i++) {
    ImageView image = new ImageView(getActivity());
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(60, 60);
    params.setMargins(10, 20, 10, 20);
    image.setLayoutParams(params);

    colorOfMedicine = medicineDetails.getMedicineColor();

    image.setBackgroundResource(0);
    image.setBackgroundResource(R.drawable.ic_tablet);
   //Drawable drawable = image.getResources().getDrawable(R.drawable.ic_tablet);
    Drawable drawable = image.getBackground();
    drawable.clearColorFilter();
    //image.invalidate();
    //image.setBackgroundColor(colorcodes[i]);

    drawable.setColorFilter(new PorterDuffColorFilter(colorcodes[i], PorterDuff.Mode.SRC_IN));
    layout.addView(image);
}

但我的问题是,它正在为所有图像设置最后一种颜色。

0 个答案:

没有答案