Android Parcelable 嵌套readParcelable

来源:互联网 发布:js写插件 编辑:程序博客网 时间:2024/06/05 06:41

转载地址:http://blog.csdn.net/wangbole/article/details/42494179


对于Android的自定义对象进行序列化,一般会选择Parcelable,或者Serializable。

在Android中,由于在使用内存时,Parcelable类要比Serializable类的性能要高;
并且Serializable在序列化的时候会产生大量的临时变量,从而会引起频繁的GC,因此在Android开发中,会推荐使用Parcelable进行序列化。


在序列化时,碰到Parcelabe类中,嵌套Parcelabe类,会稍微有点不一样。

如下:

[java] view plaincopyprint?
  1. public class FacialPart implements Parcelable {  
  2.   
  3.     private int id;  
  4.     private float scale = 1.0f;  
  5.     private float tx;  
  6.     private float ty;  
  7.   
  8.     public int getId() {  
  9.         return id;  
  10.     }  
  11.   
  12.     public void setId(int id) {  
  13.         this.id = id;  
  14.     }  
  15.   
  16.     public float getScale() {  
  17.         return scale;  
  18.     }  
  19.   
  20.     public void setScale(float scale) {  
  21.           
  22.         this.scale = scale;  
  23.     }  
  24.   
  25.     public float getTx() {  
  26.         return tx;  
  27.     }  
  28.   
  29.     public void setTx(float tx) {  
  30.         this.tx = tx;  
  31.     }  
  32.   
  33.     public float getTy() {  
  34.         return ty;  
  35.     }  
  36.   
  37.     public void setTy(float ty) {  
  38.         this.ty = ty;  
  39.     }  
  40.   
  41.     public FacialPart() {  
  42.         this(0);  
  43.     }  
  44.       
  45.     public FacialPart(int id) {  
  46.         this.id = id;  
  47.     }  
  48.       
  49.   
  50.     @Override  
  51.     public String toString() {  
  52.   
  53.         return "FacialPart[id=" + id + ", scale=" + scale + ", tx=" + tx + ", ty=" + ty + "]";  
  54.     }  
  55.   
  56.     @Override  
  57.     public int describeContents() {  
  58.         return 0;  
  59.     }  
  60.   
  61.     @Override  
  62.     public void writeToParcel(Parcel dest, int flag) {  
  63.           
  64.         dest.writeInt(id);  
  65.         dest.writeFloat(scale);  
  66.         dest.writeFloat(tx);  
  67.         dest.writeFloat(ty);  
  68.     }  
  69.       
  70.     public static final Parcelable.Creator<FacialPart> CREATOR = new Parcelable.Creator<FacialPart>() {  
  71.   
  72.         @Override  
  73.         public FacialPart createFromParcel(Parcel in) {  
  74.   
  75.             int id = in.readInt();  
  76.             float s = in.readFloat();  
  77.             float tx = in.readFloat();  
  78.             float ty = in.readFloat();  
  79.               
  80.             FacialPart part = new FacialPart(id);  
  81.             part.setScale(s);  
  82.             part.setTx(tx);  
  83.             part.setTy(ty);  
  84.               
  85.             return part;  
  86.         }  
  87.   
  88.         @Override  
  89.         public FacialPart[] newArray(int size) {  
  90.               
  91.             return new FacialPart[size];  
  92.         }  
  93.     };  
  94. }  

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public class DetectedFace  implements Parcelable {  
  2.   
  3.     private FacialPart facePart;  
  4.     private FacialPart nosePart;  
  5.     private FacialPart eyePart;  
  6.     private FacialPart eyebrowPart;  
  7.     private FacialPart mouthPart;  
  8.       
  9.     private List<FacialPart> parts;  
  10.   
  11.     @Override  
  12.     public int describeContents() {  
  13.         return 0;  
  14.     }  
  15.   
  16.     @Override  
  17.     public void writeToParcel(Parcel dest, int flag) {  
  18.   
  19.         dest.writeParcelable(facePart, flag);  
  20.         dest.writeParcelable(nosePart, flag);  
  21.         dest.writeParcelable(eyePart, flag);  
  22.         dest.writeParcelable(eyebrowPart, flag);  
  23.         dest.writeParcelable(mouthPart, flag);  
  24.   
  25.     dest.writeTypedList(parts);    
  26.     }  
  27.       
  28.     public static final Parcelable.Creator<DetectedFace> CREATOR = new Parcelable.Creator<DetectedFace>() {  
  29.   
  30.         @Override  
  31.         public DetectedFace createFromParcel(Parcel in) {  
  32.               
  33.               
  34.               
  35.             FacialPart facePart = in.readParcelable(FacialPart.class.getClassLoader());  
  36.             FacialPart nosePart = in.readParcelable(FacialPart.class.getClassLoader());  
  37.             FacialPart eyePart = in.readParcelable(FacialPart.class.getClassLoader());  
  38.             FacialPart eyebrowPart = in.readParcelable(FacialPart.class.getClassLoader());  
  39.             FacialPart mouthPart= in.readParcelable(FacialPart.class.getClassLoader());  
  40.               
  41.         List<FacialPart> parts = new ArrayList<FacialPart>();    
  42.         in.readTypedList(parts, FacialPart.CREATOR);    
  43.   
  44.             DetectedFace face = new DetectedFace();  
  45.   
  46.             face.facePart = facePart;  
  47.             face.nosePart = nosePart;  
  48.             face.eyePart = eyePart;  
  49.             face.eyebrowPart = eyebrowPart;  
  50.             face.mouthPart = mouthPart;  
  51.           
  52.         face.parts = parts;  
  53.               
  54.             return face;  
  55.         }  
  56.   
  57.         @Override  
  58.         public DetectedFace[] newArray(int size) {  
  59.             return new DetectedFace[size];  
  60.         }    
  61.           
  62.     };  
  63. }  

关键的代码就是读取时,使用**.class.getClassLoader()进行读取;
如:
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. FacialPart facePart = in.readParcelable(FacialPart.class.getClassLoader());  

而对于嵌套的列表,则使用如下代码:
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. List<FacialPart> parts = new ArrayList<FacialPart>();    
  2. in.readTypedList(parts, FacialPart.CREATOR);    



版权声明:本文为博主原创文章,未经博主允许不得转载。

0 0