我成功更改了放置在“图像视图”列表中的位图图像的颜色,但是问题是它完全改变了,而不是我选择的图像,我试图在我选择的图像视图中获得位图。但仍然没有。我的想法是使用包含图像的哈希图,并且我在每个元素上捕获了On Click事件,所有功能都起作用,但它更改了所有图像的颜色,而不仅仅是我单击的图像。
我设置位图的功能
private void setUp() {
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icons8_bus_36_border);
tempBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(bitmap, 0, 0, mPaint);
if (bitmap == null) {
Log.d(TAG, "setUp: null rồi");
} else {
Log.d(TAG, "setUp: ko null");
}
}
将位图放入图像视图
private void sequentially() {
setUp();
imageList = new HashMap<>();
chooseColor = Color.parseColor("#1A86C8");
seat.setGravity(Gravity.CENTER);
LinearLayout row = new LinearLayout(getActivity());
row.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams firstImg = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
firstImg.setMargins(10, 0, 0, 0);
LinearLayout.LayoutParams forRow = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
forRow.setMargins(0, 0, 0, 15);
for (int j = 0; j < 2; j++) {
row = new LinearLayout(getActivity());
row.setLayoutParams(forRow);
seat.addView(row);
for (int i = 0; i < 3; i++) {
countForImage++;
imageView = new ImageView(getActivity());
imageList.put(countForImage, imageView);
Log.d(TAG, "sequentially: number" + countForImage);
imageView.setLayoutParams(firstImg);
tempCanvas.drawBitmap(bitmap, 0, 0, mPaint);
imageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
row.addView(imageView);
if (i == 1 && j == 0) {
imageView.setVisibility(View.INVISIBLE);
}
}
}
}
在上面的代码中,我还获得了图像列表,并开始为其捕获事件
public void loopSeat(Map<Integer, ImageView> map) {
Log.d(TAG, "loopSeat: ");
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
final Map.Entry pair = (Map.Entry) iterator.next();
final ImageView imageView = (ImageView) pair.getValue();
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick at number= :" + pair.getKey());
setColor(imageView);
}
});
Log.d(TAG, "loopSeat: key= " + pair.getKey());
}
}
最后一步是更改我选择的图像的颜色
public void setColor(ImageView imageView) {
// TODO: 2/18/2019 this function only change all image color,need to fix
Log.d(TAG, "setColor: da vao");
mPaint = new Paint(chooseColor);
ColorFilter filter = new LightingColorFilter(chooseColor, 1);
mPaint.setColorFilter(filter);
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
tempCanvas.drawBitmap(bm, 0, 0, mPaint);
imageView.setImageDrawable(new BitmapDrawable(getResources(), bm));
}
答案 0 :(得分:0)
因为ImageView的Bitmap实例相同。您需要为每个ImageView复制到新的位图: https://developer.android.com/reference/android/graphics/Bitmap.html#copy(android.graphics.Bitmap.Config,%20boolean)
尽管我建议使用ImageView的tint属性: How to set tint for an image view programmatically in android?
答案 1 :(得分:0)
public void setColor(ImageView imageView) {
// TODO: 2/18/2019 this function only change all image color,need to fix
Log.d(TAG, "setColor: da vao");
mPaint = new Paint(chooseColor);
ColorFilter filter = new LightingColorFilter(chooseColor, 1);
mPaint.setColorFilter(filter);
//end of set color
//start to creat a new bitmap
Bitmap bm = bitmap.copy(Bitmap.Config.ARGB_8888, false);
choosBitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
tempCanvas.drawBitmap(choosBitmap, 0, 0, mPaint);
imageView.setImageDrawable(new BitmapDrawable(getResources(), choosBitmap));
}
我根据建议更改了代码,但是现在当我选择图像时它不再更改蓝色而已。@ Benoit