When the item is swiped the background is colored by another color and an icon is displayed. All works well, but I do not like the fact that the icon changes size depending on the height of the item. Please, tell me how to make the icon always remain the same size?
I use this code to draw a background and icons.
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
View itemView = viewHolder.itemView;
float height = (float) itemView.getBottom() - (float) itemView.getTop();
float width = height / 3;
Paint p = new Paint();
Bitmap icon;
if (dX > 0) {
p.setColor(ResourcesCompat.getColor(getResources(), R.color.red, null));
RectF background = new RectF((float) itemView.getLeft(), (float) itemView.getTop(), dX, (float) itemView.getBottom());
c.drawRect(background, p);
Drawable d = getResources().getDrawable(R.drawable.ic_delete_white_24dp);
icon = drawableToBitmap(d);
RectF iconDest = new RectF((float) itemView.getLeft() + width, (float) itemView.getTop() + width, (float) itemView.getLeft() + 2 * width, (float) itemView.getBottom() - width);
c.drawBitmap(icon, null, iconDest, p);
} else {
p.setColor(ResourcesCompat.getColor(getResources(), R.color.green, null));
RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
c.drawRect(background, p);
Drawable d = getResources().getDrawable(R.drawable.ic_done_white_24dp);
icon = drawableToBitmap(d);
RectF iconDest = new RectF((float) itemView.getRight() - 2 * width, (float) itemView.getTop() + width, (float) itemView.getRight() - width, (float) itemView.getBottom() - width);
c.drawBitmap(icon, null, iconDest, p);
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}