I have a list of seat images drawn with canvas, and I want when I click on an image it will change color. Now it all changes color every time I click. Does anyone know how to solve this problem?
setUp is a function used to set up the things needed to draw an image with Canvas
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");
} else {
Log.d(TAG, "setUp: not null");
}
}
sequentially is the function I use to draw images with Canvas
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);
}
}
}
}
loopSet is the function I use to set the color change event
public void loopSeat(Map<Integer, ImageView> map) {
Log.d(TAG, "loopSeat: running");
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());
}
}
setColor will be the function responsible for changing the color of the image
public void setColor(ImageView imageView) {
color,need to fix
Log.d(TAG, "setColor running");
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));
}