简体   繁体   English

在RecyclerView中滑动(图标大小)

[英]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. RectF不使用坐标,而是使用图形的边界。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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