我试图在刷卡时在RecyclerView的一行下面绘制一个图标,类似于Gmail或收件箱应用。我实现了颜色,但我想绘制的Bitmap正在躲避我。我正在使用https://material.io/icons/网站上的删除图标。我将其作为SVG下载并将其作为XML文件导入我的drawables文件夹。我的代码如下:
private Paint p = new Paint();
private ItemTouchHelper.SimpleCallback ithSC = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
fileData.remove(viewHolder.getAdapterPosition());
adapter.notifyDataSetChanged();
}
@Override
public void onChildDraw(Canvas c, RecyclerView rv, RecyclerView.ViewHolder vh, float dx, float dy, int actionState, boolean isCurrentlyActive) {
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_delete);
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
// this gets the RecyclerView row from the ViewHolder
View itemView = vh.itemView;
// p is a Paint object, setting the color
p.setColor(Color.RED);
c.drawRect(dx, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom(), p);
super.onChildDraw(c, rv, vh, dx,dy, actionState, isCurrentlyActive);
}
}
};
当我运行应用程序时,logcat会显示消息:
D/skia: ---- fAsset->read(8192) returned 0
D/skia: --- SkAndroidCodec::NewFromStream returned null
为什么BitmapFactory.decodeResource()调用返回一个null对象,如何修复它?
提前致谢
更新:
我尝试过其他一些东西,这是我更新的代码。其中一个重大变化是图标是PNG而不是XML
public void onChildDraw(Canvas c, RecyclerView rv, RecyclerView.ViewHolder vh, float dx, float dy, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
// this gets the RecyclerView row from the ViewHolder
View itemView = vh.itemView;
// get the width and height
float height = itemView.getHeight();
float width = height/3;
// p is a Paint object, setting the color
p.setColor(Color.RED);
c.drawRect(dx, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom(), p);
Bitmap b = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.ic_delete);
RectF destination = new RectF((float) itemView.getRight() - 2*width , (float) itemView.getTop() + width, (float) itemView.getRight() - width, (float)itemView.getBottom() - width);
c.drawBitmap(b, null, destination, p);
super.onChildDraw(c, rv, vh, dx,dy, actionState, isCurrentlyActive);
}
}
};
这是我仍然得到的错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference