How to Pass Drawable using Parcelable

倖福魔咒の 提交于 2019-11-30 18:02:40
yorkw

As you already convert Drawable to Bitmap in your code, why not use Bitmap as an member of your Parcelable class.

Bitmap implements Parcelable by default in API, by using Bitmap, you don't need to do anything special in your code and it will automatically handled by Parcel.

Or if you insist to use Drawable, implement your Parcelable as something like this:

public void writeToParcel(Parcel out, int flags) {
  ... ...
  // Convert Drawable to Bitmap first:
  Bitmap bitmap = (Bitmap)((BitmapDrawable) mMyDrawable).getBitmap();
  // Serialize bitmap as Parcelable:
  out.writeParcelable(bitmap, flags);
  ... ...
}

private Guide(Parcel in) {
  ... ...
  // Deserialize Parcelable and cast to Bitmap first:
  Bitmap bitmap = (Bitmap)in.readParcelable(getClass().getClassLoader());
  // Convert Bitmap to Drawable:
  mMyDrawable = new BitmapDrawable(bitmap);
  ... ...
}

Hope this helps.

In my app, I saved Drawable/BitMap into Cache and passing it using file's path String instead.

Not a solution you were looking for, but at least some alternative for your problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!