1

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));
}
nguyen sam
  • 43
  • 8

2 Answers2

0

Because the Bitmap instance of the ImageView are the same. You need to copy to a new Bitmap for each ImageView: https://developer.android.com/reference/android/graphics/Bitmap.html#copy(android.graphics.Bitmap.Config,%20boolean)

Though I'd recommend to use the tint property of ImageView: How to set tint for an image view programmatically in android?

Benoit
  • 1,922
  • 16
  • 25
0

I changed the code in setColor function according to the suggestion of @Benoit. And it works perfect

public void setColor(ImageView imageView) {
    Log.d(TAG, "setColor: running");
    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));
}
nguyen sam
  • 43
  • 8