public class ViewHierarchyEncoder extends Object
ViewHierarchyEncoder
is a serializer that is tailored towards writing out
view hierarchies (the view tree, along with the properties for each view) to a stream.
It is typically used as follows:
ViewHierarchyEncoder e = new ViewHierarchyEncoder(); for (View view : views) { e.beginObject(view); e.addProperty("prop1", value); ... e.endObject(); } // repeat above snippet for each view, finally end with: e.endStream();
On the stream, a snippet such as the above gets encoded as a series of Map's (one corresponding to each view) with the property name as the key and the property value as the value.
Since the property names are practically the same across all views, rather than using the property name directly as the key, we use a short integer id corresponding to each property name as the key. A final map is added at the end which contains the mapping from the integer to its property name.
A value is encoded as a single byte type identifier followed by the encoding of the value. Only primitive types are supported as values, in addition to the Map type.
Constructor and Description |
---|
ViewHierarchyEncoder(ByteArrayOutputStream stream) |
Modifier and Type | Method and Description |
---|---|
void |
addProperty(String name,
boolean v) |
void |
addProperty(String name,
float v) |
void |
addProperty(String name,
int v) |
void |
addProperty(String name,
short s) |
void |
addProperty(String name,
String s) |
void |
addPropertyKey(String name)
Writes the given name as the property name, and leaves it to the callee
to fill in value for this property.
|
void |
beginObject(Object o) |
void |
endObject() |
void |
endStream() |
public ViewHierarchyEncoder(ByteArrayOutputStream stream)
public void beginObject(Object o)
public void endObject()
public void endStream()
public void addProperty(String name, boolean v)
public void addProperty(String name, short s)
public void addProperty(String name, int v)
public void addProperty(String name, float v)
public void addPropertyKey(String name)