简体   繁体   中英

Swipe in RecyclerView (icon size)

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);
    }
}

RectF uses not the coordinates, but the boundaries of the drawing. To save the size of the icon, you need to specify the boundaries, taking into account the size of the icon.

For the left icon.

Drawable d = getResources().getDrawable(R.drawable.ic_delete_white_24dp);
icon = drawableToBitmap(d);

int iconWidth = icon.getWidth();
int iconHeight = icon.getHeight();

float leftPosition = iconWidth;
float rightPosition = leftPosition + iconWidth;
float topPosition = itemView.getTop() + ((height - iconHeight) / 2);
float bottomPosition = topPosition + iconHeight;

RectF iconDest = new RectF(leftPosition, topPosition, rightPosition, bottomPosition);
c.drawBitmap(icon, null, iconDest, p);

For the right icon.

Drawable d = getResources().getDrawable(R.drawable.ic_done_white_24dp);
icon = drawableToBitmap(d);

int iconWidth = icon.getWidth();
int iconHeight = icon.getHeight();

float rightPosition = itemView.getRight() - iconWidth;
float leftPosition = rightPosition - iconWidth;
float topPosition = itemView.getTop() + ((height - iconHeight) / 2);
float bottomPosition = topPosition + iconHeight;

RectF iconDest = new RectF(leftPosition, topPosition, rightPosition, bottomPosition);
c.drawBitmap(icon, null, iconDest, p);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM