0

滑动项目时,背景会被另一种颜色着色,并显示一个图标。一切都很好,但我不喜欢图标根据项目的高度改变大小的事实。请告诉我如何使图标始终保持相同大小

我使用此代码绘制背景和图标。

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

1 回答 1

0

RectF使用的不是坐标,而是绘图的边界。要保存图标的大小,您需要指定边界,同时考虑到图标的大小。

对于左侧图标。

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

对于正确的图标。

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);
于 2017-12-13T10:12:33.123 回答