Android序列化
来源:互联网 发布:c语言编程实例 编辑:程序博客网 时间:2024/05/19 22:46
其实刚开始目的就是想知道bitmap数组如何传递,结果硬是没弄出来,哎!先把写好的发了, 后面再去研究研究。。。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.serializable.MainActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="serializable"
/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="parcelable"
/>
</LinearLayout>
MainActivity:
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
Button btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
Intent intent = new Intent(MainActivity.this,SerializableActivity.class);
List<String> info = new ArrayList<String>();
info.add("18");
info.add("男");
PersonBean pb = new PersonBean("张三", info);
Bundle mBundle = new Bundle();
mBundle.putSerializable("PersonBean", pb);
intent.putExtras(mBundle);
startActivity(intent);
break;
case R.id.btn2:
Intent intent2 = new Intent(MainActivity.this,ParcelableActivity.class);
List<String> info2 = new ArrayList<String>();
info2.add("18");
info2.add("男");
Bitmap d1 = BitmapFactory.decodeResource(getResources(), R.drawable.a001);
ParcelableBean pab = new ParcelableBean("李四", info2);
Bundle mBundle2 = new Bundle();
mBundle2.putParcelable("ParcelableBean", pab);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
d1.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte [] bitmapByte =baos.toByteArray();
intent2.putExtras(mBundle2);
intent2.putExtra("bitmap",bitmapByte);
startActivity(intent2);
break;
}
}
}
activity_serializable.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.serializable.SerializableActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
SerializableActivity:
public class SerializableActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_serializable);
PersonBean pb = (PersonBean) getIntent().getSerializableExtra("PersonBean");
TextView tv1 = (TextView) findViewById(R.id.tv1);
TextView tv2 = (TextView) findViewById(R.id.tv2);
TextView tv3 = (TextView) findViewById(R.id.tv3);
tv1.setText(pb.getName());
tv2.setText(pb.getInfo().get(0));
tv3.setText(pb.getInfo().get(1));
}
}
PersonBean:
public class PersonBean implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private List<String> info;
public PersonBean() {
}
public PersonBean(String name, List<String> info) {
this.name = name;
this.info = info;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getInfo() {
return info;
}
public void setInfo(List<String> info) {
this.info = info;
}
}
activity_parcelable.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.serializable.SerializableActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄" />
<TextView
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别" />
<TextView
android:id="@+id/tv6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/iv1"
android:layout_width="60dp"
android:layout_height="60dp"
/>
<ImageView
android:id="@+id/iv2"
android:layout_width="60dp"
android:layout_height="60dp"
/>
<ImageView
android:id="@+id/iv3"
android:layout_width="60dp"
android:layout_height="60dp"
/>
<ImageView
android:id="@+id/iv4"
android:layout_width="60dp"
android:layout_height="60dp"
/>
</LinearLayout>
ParcelableActivity:
public class ParcelableActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("PDA", "=======2========");
setContentView(R.layout.activity_parcelable);
Log.i("PDA", "=======3========");
//用Intent传bitmap不能大于40KB,否则就会出现这个情况。解决这个问题的简单方案就是全局单例模式:
//我将bitmap放在一个固定的生命周期中的值里,随时可取可消。
ParcelableBean pb = (ParcelableBean) getIntent().getParcelableExtra("ParcelableBean");
Intent intent=getIntent();
byte [] bis=intent.getByteArrayExtra("bitmap");
Bitmap bitmap=BitmapFactory.decodeByteArray(bis, 0, bis.length);
TextView tv1 = (TextView) findViewById(R.id.tv4);
TextView tv2 = (TextView) findViewById(R.id.tv5);
TextView tv3 = (TextView) findViewById(R.id.tv6);
ImageView iv1 = (ImageView) findViewById(R.id.iv1);
tv1.setText(pb.getName());
tv2.setText(pb.getInfo().get(0));
tv3.setText(pb.getInfo().get(1));
iv1.setImageBitmap(bitmap);
}
}
ParcelableBean:
public class ParcelableBean implements Parcelable{
/*实现Parcelable接口需要实现三个方法:
1)writeToParcel方法。该方法将类的数据写入外部提供的Parcel中。声明:writeToParcel(Parcel dest, int flags)。
2)describeContents方法。直接返回0就可以。
3)静态的Parcelable.Creator<T>接口,本接口有两个方法:createFromParcel(Parcel in) 实现从in中创建出类的实例的功能。
newArray(int size) 创建一个类型为T,长度为size的数组, returnnew T[size];即可。本方法是供外部类反序列化本类数组使用。*/
String name;
List<String> info;
public ParcelableBean() {
}
public ParcelableBean(String name, List<String> info) {
this.name = name;
this.info = info;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getInfo() {
return info;
}
public void setInfo(List<String> info) {
this.info = info;
}
@Override
public int describeContents() {
return 0;
}
//该方法将类的数据写入外部提供的Parcel中。
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeList(info);
}
public static final Parcelable.Creator<ParcelableBean> CREATOR = new Creator<ParcelableBean>(){
//实现从source中创建出类的实例的功能
@Override
public ParcelableBean createFromParcel(Parcel source) {
ParcelableBean pb = new ParcelableBean();
pb.name = source.readString();
pb.info = new ArrayList<String>();
source.readList(pb.info,getClass().getClassLoader());
return pb;
}
//创建一个类型为T,长度为size的数组
@Override
public ParcelableBean[] newArray(int size) {
return new ParcelableBean[size];
}
};
}
- Android bitmap序列化
- Android中序列化
- Android 序列化 Parcelable
- Android中的序列化
- android的序列化
- android对象序列化
- android中的序列化
- Android 序列化
- android 传值 序列化
- android 序列化
- android 的序列化
- android 对象序列化
- android序列化Parcelable
- android序列化
- 浅谈Android序列化
- 浅谈Android序列化
- Android序列化Parcelable
- Android学习 - 序列化
- 12天学好C语言——记录我的C语言学习之路(Day 3)
- 黑马程序员_笔记总结(给自己看的向)
- 如何知道一个python对象的类型?
- 算法导论——分治法——最大子数组问题
- android APP性能数据收集
- Android序列化
- 循环06~~~10
- 深入Android【八】 —— Activity间数据传输
- 股票学习31(公司财务分析02)
- POJ-1247
- 如何用Python来进行查询和替换一个文本字符串
- ACM暑期计划——第四天
- 使用xml.etree.ElementTree遍历xml所有节点
- CAS学习笔记(一)