如果你需要编写的是游戏程序,那么一般情况下可能你需要使用一个自定义的 view,你只要将setContentView的参数换成这个view的实例即可。在这个view的onDraw中完成绘制操作,应用需要通知这个view刷新界面的时候使用 invalidate或者postinvalidate完成。
如果你需要系统帮你实现缓冲机制,那么有一个实现了双缓冲的类SurfaceView可供你使用。
好吧,绘制的操作我们今后单独开章来将,我们今天讲一下怎么使用系统定义好的控件。
普通的TextView、EditText、Button等等这些控件我们不讲了,主要原因是太简单了。我们今天讲Adapter:
Adapter叫适配器,对于一个可以作为Container的Component来说,我们有时无法确定他内部所包含的Compoent个数和展现形式,我们仅能确定他得框架展现形式,所以需要一个Adapter。举例来说,对于一个ListView,我们仅能确定他的每一项肯定是一个一个的ListItem,但我们无法确定ListItem内部的内容如何展现,比如这个Item的宽高,有几个图片控件,几个文本控件。。。
我们使用Adapter来帮我们实现这一切。
- getCount帮我们确定了Item的总数;
- getItem确定取得每一项Item的方法;
- getItemId确定我们每一个Item的Id,不太用到,一般都是position;
- getView帮我们确定每一个Item的展现内容;
ok,有了这些,我们每一个item都知道怎么展现,又由于count这些也有了,我们知道如何将Item组合成一个大的Component,语言的设计之美就在这里了。。。
一个简单的例子如下:
PersonGridViewAdapter.java
package com.freesoft.uidemo.uicomponent;
import java.io.IOException;
import java.io.InputStream;
import com.freesoft.uidemo.R;
import com.freesoft.uidemo.entity.Person;
import com.freesoft.uidemo.entity.PersonList;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class PersonGridViewAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
public PersonGridViewAdapter(Context context) {
super();
this.context = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
return PersonList.getPersons().size();
}
public Object getItem(int position) {
return PersonList.getPersons().get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.gridview, null);
ImageView img = (ImageView) convertView
.findViewById(R.id.gv_gv_personpic);
TextView txt = (TextView) convertView
.findViewById(R.id.tv_gv_personname);
Person person = PersonList.getPersons().get(position);
AssetManager am = context.getAssets();
InputStream is;
try {
is = am.open(person.getPersonIconUri().toString());
img.setImageDrawable(Drawable.createFromStream(is, null));
txt.setText(person.getPersonName());
} catch (IOException e) {
e.printStackTrace();
}
return convertView;
}
}
Person.java
package com.freesoft.uidemo.entity;
import android.net.Uri;
public class Person {
private String personName;
private Uri personIconUri;
public Person(String personName, String iconFilename) {
super();
this.setPersonName(personName);
this.setPersonIconUri(Uri.parse(iconFilename));
}
private void setPersonName(String personName) {
this.personName = personName;
}
private void setPersonIconUri(Uri personIconUri) {
this.personIconUri = personIconUri;
}
public String getPersonName() {
return personName;
}
public Uri getPersonIconUri() {
return personIconUri;
}
}
PersonList.java
package com.freesoft.uidemo.entity;
import java.util.ArrayList;
public class PersonList {
private static ArrayList<Person> persons = null;
public static ArrayList<Person> getPersons() {
persons = new ArrayList<Person>();
initPersons();
return persons;
}
private static void initPersons() {
Person person = null;
person = new Person("张三", "QQtxtb/QQtxtb 00.png");
persons.add(person);
person = new Person("李四", "QQtxtb/QQtxtb 01.png");
persons.add(person);
person = new Person("王五", "QQtxtb/QQtxtb 02.png");
persons.add(person);
}
}
简单的总结一下,发现使用了Adapter的控件还真不少,例如:ListView、GridView、AutoCompleteTextView、Gallery、Spinner等。。。。