如何使用parcelable传递BitmapShader类对象?

时间:2015-11-27 07:02:03

标签: android bitmap

这是例外

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)

我的代码是

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

    }

}

1 个答案:

答案 0 :(得分:0)

BitmapShader类没有实现Parcelable,因此您不能使用这种方式。 根据您的使用情况,您可以例如保存您需要的参数(重新)创建BitmapShader。对于BitmapShader类,您可以尝试保存

  • 位图(实现Parcelable)
  • tileX的TileMode(使用例如TileMode.CLAMP.ordinal())
  • tileY的TileMode

在包裹中。然后,您可以使用此数据重新创建BitmapShader。不是最好的选择,但另一方面,您可能想重新考虑您的架构。