如何通过使用Parcelable界面在Android的意图,通过与绘制参数包含到另一个活动的对象意图、界面、对象、参数

2023-09-12 05:34:38 作者:王晨正发光耶

我想通过使用含有Parcelable Inteface到另一个活动一个绘制场中的目标。但问题是,在 公共无效writeToParcel(包裹包裹,INT标志)覆盖类(其对象发送)的方法,我不能有parcel.writeDrawable一种方法。所以,其他领域我能顺利通过,而不是绘制对象。 我已经添加下面的方法在我的班级实施Parcelable界面后。

I want to pass an object containing a drawable field using Parcelable Inteface to another Activity. But problem is, in public void writeToParcel(Parcel parcel, int flags) overridden method in class(whose object has to send), I can't have "parcel.writeDrawable" kind of method. So other fields I'm able to pass, but not Drawable. I have added the following methods in my class after implementing Parcelable Interface.

public static final Parcelable.Creator<ImageInfo> CREATOR = new Parcelable.Creator<ImageInfo>() {

    public ImageInfo createFromParcel(Parcel source) {
        return new ImageInfo(source);
    }

    public ImageInfo[] newArray(int size) {
        return new ImageInfo[size];
    }
};

public int describeContents() {
    return 1;
}

public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeString(name);   
    parcel.writeString(price);
    parcel.writeString(urlOfImage);
}

和使用以下code我把我的对象的意图

and using following code I'm putting my object in intent

Bundle b = new Bundle();
b.putParcelable("parse", imageInfo);
intentTrackIntHome.putExtras(b);

所以我必须做的传递绘制现场。

So what I have to do to pass that drawable field.

我知道我可以使用共享preferences,SD卡,全球应用程序类,静态字段;但我不希望使用它。因为在未来我code可以改变的,所以我想稳健code。

I know I can use shared Preferences, sd card, global application class, static fields; but I don't want to use it. Because in future my code can change, so I want robust code.

推荐答案

将您绘制对象为位图,并将其发送给另一个活动。

Convert your Drawable to Bitmap and send it to Another Activity.

Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
intent.putExtra("Bitmap", bitmap);

要获取,这一意图

Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");

有关更多详细信息请点击此链接