简体   繁体   中英

how to pass BitmapShader class object using parcelable?

here is the exception

java.lang.RuntimeException: Parcel: unable to marshal value android.graphics.BitmapShader@419d81a0
            at android.os.Parcel.writeValue(Parcel.java:1235)
            at com.imagepatterns.ShaderParcelalable.writeToParcel(ShaderParcelalable.java:47)
            at android.os.Parcel.writeParcelable(Parcel.java:1254)
            at android.os.Parcel.writeValue(Parcel.java:1173)
            at android.os.Parcel.writeMapInternal(Parcel.java:591)
            at android.os.Bundle.writeToParcel(Bundle.java:1619)
            at android.os.Parcel.writeBundle(Parcel.java:605)
            at android.content.Intent.writeToParcel(Intent.java:6470)
            at android.app.ActivityManagerProxy.finishActivity(ActivityManagerNative.java:1921)
            at android.app.Activity.finish(Activity.java:4135)
            at com.imagepatterns.ImagePatternActivity.sendResultBack(ImagePatternActivity.java:111)
            at com.imagepatterns.ImagePatternActivity.access$100(ImagePatternActivity.java:31)
            at com.imagepatterns.ImagePatternActivity$1.onClick(ImagePatternActivity.java:52)
            at android.view.View.performClick(View.java:4106)
            at android.view.View$PerformClick.run(View.java:17052)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5059)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)

And my code is

public class ShaderParcelalable implements Parcelable {

    public BitmapShader getShader() {
        return shader;
    }

    private BitmapShader shader;

    protected ShaderParcelalable(Parcel in) {
        this.shader = (BitmapShader) in.readValue(BitmapShader.class.getClassLoader());
    }

    public ShaderParcelalable(BitmapShader shader) {
        this.shader = shader;
    }

    public static final Creator<ShaderParcelalable> CREATOR = new Creator<ShaderParcelalable>() {
        @Override
        public ShaderParcelalable createFromParcel(Parcel in) {

            return new ShaderParcelalable(in);
        }

        @Override
        public ShaderParcelalable[] newArray(int size) {
            return new ShaderParcelalable[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(shader);

    }

}

The BitmapShader class does not implement Parcelable, so you can't use this way. Depending on your use case you could eg save the parameters you need to (re-)create the BitmapShader. In the case of the BitmapShader-class, you could try to save the

  • Bitmap (implements Parcelable)
  • TileMode for tileX (use eg TileMode.CLAMP.ordinal())
  • TileMode for tileY

in the Parcel. Then you could recreate your BitmapShader with this data. Not the best option, but on the other hand you might want to rethink your architecture.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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