滑动项目时,背景会被另一种颜色着色,并显示一个图标。一切都很好,但我不喜欢图标根据项目的高度改变大小的事实。请告诉我如何使图标始终保持相同大小?
我使用此代码绘制背景和图标。
@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);
}
}