Parcelable序列化对象
简介:
- 鉴于Serializable在内存序列化上开销比较大,而内存资源属于android系统中的稀有资源(android系统分配给每个应用的内存开销都是有限的),为此android中提供了Parcelable接口来实现序列化操作,Parcelable的性能比Serializable好,在内存开销方面较小,所以在内存间数据传输时推荐使用Parcelable
使用:
1.实现Parcelable接口,重写其两个方法:
//对对当前对象的内容描述,一般不用管,返回0即可。
@Override
public int describeContents() {
return 0;
}
//将当前对象写入序列化结构中
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeLong(this.duration);
parcel.writeDouble(this.distance);
parcel.writeDouble(this.calorie);
parcel.writeDouble(this.distribution);
parcel.writeList(this.pathLinePoints);
parcel.writeValue(this.endPoint);
parcel.writeValue(this.startPoint);
}
- 创建内部对象CREATOR
//通过序列结构创建性的当前类对象
public PathRecord(Parcel in) {
this.duration = in.readLong();
this.distance = in.readDouble();
this.calorie = in.readDouble();
this.distribution = in.readDouble();
in.readList(pathLinePoints, LatLng.class.getClassLoader());
this.endPoint = (LatLng) in.readValue(LatLng.class.getClassLoader());
this.startPoint = (LatLng) in.readValue(LatLng.class.getClassLoader());
}
public static final Parcelable.Creator<PathRecord> CREATOR = new Creator<PathRecord>() {
@Override
//实现从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层
public PathRecord createFromParcel(Parcel parcel) {
return new PathRecord(parcel); //返回新的对象
}
@Override
//创建一个类型为T,长度为size的数组,供外部类反序列化本类数组使用
public PathRecord[] newArray(int i) {
return new PathRecord[0];
}
};
注意:public static final一个都不能少,内部对象CREATOR的名称也不能改变,必须全部大写。
至此序列化对象创建完成。
完整代码:
public class PathRecord implements Parcelable {
//运动开始点
private LatLng startPoint;
//运动结束点
private LatLng endPoint;
//运动轨迹
private List<LatLng> pathLinePoints = new ArrayList<>();
//运动距离
private double distance;
//运动时长
private long duration;
//消耗卡路里
private double calorie;
//平均配速(分钟/公里)
private double distribution;
public double getCalorie() {
return calorie;
}
public void setCalorie(double calorie) {
this.calorie = calorie;
}
public LatLng getStartPoint() {
return startPoint;
}
public void setStartPoint(LatLng startPoint) {
this.startPoint = startPoint;
}
public LatLng getEndPoint() {
return endPoint;
}
public void setEndPoint(LatLng endPoint) {
this.endPoint = endPoint;
}
public List<LatLng> getPathLinePoints() {
return pathLinePoints;
}
public void setPathLinePoints(List<LatLng> pathLinePoints) {
this.pathLinePoints = pathLinePoints;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public double getDistribution() {
return distribution;
}
public void setDistribution(double distribution) {
this.distribution = distribution;
}
public void addLaLng(LatLng latLng) {
pathLinePoints.add(latLng);
}
public PathRecord() {
}
@Override
public int describeContents() {
return 0;
}
public PathRecord(Parcel in) {
this.duration = in.readLong();
this.distance = in.readDouble();
this.calorie = in.readDouble();
this.distribution = in.readDouble();
in.readList(pathLinePoints, LatLng.class.getClassLoader());
this.endPoint = (LatLng) in.readValue(LatLng.class.getClassLoader());
this.startPoint = (LatLng) in.readValue(LatLng.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeLong(this.duration);
parcel.writeDouble(this.distance);
parcel.writeDouble(this.calorie);
parcel.writeDouble(this.distribution);
parcel.writeList(this.pathLinePoints);
parcel.writeValue(this.endPoint);
parcel.writeValue(this.startPoint);
}
public static final Parcelable.Creator<PathRecord> CREATOR = new Creator<PathRecord>() {
@Override
public PathRecord createFromParcel(Parcel parcel) {
return new PathRecord(parcel);
}
@Override
public PathRecord[] newArray(int i) {
return new PathRecord[0];
}
};
}
如上:我们创建自己需要序列化的类。然后它有自己的成员变量,使用注意点:
- startPoint,endPoint为自定义类,如若当前类拥有其他自定义类的成员变量的对象,那么此自定义对象需要实现Parcelable接口,否则会抛出异常
- 若此成员变量为引用类型则需要进行初始化,否则会发生空指针异常
- 在对当前成员变量写入与读出的顺序应一样
- 在读出时有一个classLoader对像,此为类加载器对象,一般通过类名.class.getClassLoader()获得。