//这个是SAX解析的所有
public class XMLContentHandler extends DefaultHandler {
private boolean IsDisplayNameTag =false;
private boolean IsAvatarTag =false;
private boolean IsReputationTotalTag =false;
private boolean IsUserTag =false;
private String DisplayName="DisplayName";
private String Avatar="Avatar";
private String ReputationTotal="ReputationTotal";
private Users users ;
private List<Users> list;
public List<Users> getUsers() {
return list;
}
//打开xml文档的回调函数
@Override
public void startDocument() throws SAXException{
//这里做list的初始化工作
Log.e("+++++++++>1","1");
list =new ArrayList<Users>();
}
//一旦发现元素开始标记就回调此函数
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("User")) {
users =new Users();
IsUserTag =true;
}
if (localName.equals(DisplayName)) {
IsDisplayNameTag=true;
}else if (localName.equals(Avatar)) {
IsAvatarTag =true;
}else if (localName.equals(ReputationTotal)) {
IsReputationTotalTag =true;
}
}
//一发现元素值或属性值就回调此函数
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String data =new String(ch,start,length);
if (IsDisplayNameTag) {
users.setDisplayName(data);
}else if (IsAvatarTag) {
users.setAvatar(data);
}else if (IsReputationTotalTag) {
users.setReputationTotal(data);
// Log.i("uuuuuuuuuuuu3",data);
}else {
}
}
//一旦发现元素结束标记就回调此函数
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equals("User")) {
IsUserTag=false;
list.add(users);
}
if (localName.equals(DisplayName)) {
IsDisplayNameTag =false;
}else if (localName.equals(Avatar)) {
IsAvatarTag =false;
}else if (localName.equals(ReputationTotal)) {
IsReputationTotalTag =false;
}
}
//关闭xml文档的回调函数
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
Log.e("+++++++++>5","5");
super.endDocument();
}
}
// 解析的标签
* User DisplayName Avatar ReputationTotal需要解析的标签
* */
public class Users {
private String User;
private String DisplayName;
private String Avatar;
private String ReputationTotal;
public String getUser() {
return User;
}
public void setUser(String user) {
User = user;
}
public String getDisplayName() {
return DisplayName;
}
public void setDisplayName(String displayName) {
DisplayName = displayName;
}
public String getAvatar() {
return Avatar;
}
public void setAvatar(String avatar) {
Avatar = avatar;
}
public String getReputationTotal() {
return ReputationTotal;
}
public void setReputationTotal(String reputationTotal) {
ReputationTotal = reputationTotal;
}
}
/**自定义的MyAdapter 继承BaseAdapter
*最下面是一个ASYNCTACK的异步刷新
*
*/
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Users> items;
public MyAdapter(Context context,List<Users> it){
inflater =LayoutInflater.from(context);
items =it;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layout =(LinearLayout) inflater.inflate(R.layout.listone, null);
Users user =items.get(position);
TextView txtname =(TextView) layout.findViewById(R.id.txtup);
txtname.setTextColor(Color.BLACK);
txtname.setTextSize(20);
txtname.setText(user.getDisplayName());
TextView txtrep = (TextView) layout.findViewById(R.id.txtdown);
txtname.setTextColor(Color.BLACK);
txtrep.setTextSize(10);
txtrep.setText(user.getReputationTotal());
ImageView img =(ImageView) layout.findViewById(R.id.img01);
String image =user.getAvatar();
img.setTag(image);
new AsyncLoader().execute(img);
img.setDrawingCacheEnabled(true);
return layout;
}
//继承于AsyncTask 刷新ImageView,未实现图片下载到SD卡的功能;
class AsyncLoader extends AsyncTask <ImageView, Void, Bitmap>{
private ImageView gView;
protected Bitmap doInBackground(ImageView... views) {
Bitmap bmp = null;
ImageView view = views[0];
// 根据iconUrl获取图片并渲染,iconUrl的url放在了view的tag中。
if (view.getTag() != null) {
try {
URL url = new URL(view.getTag().toString());
GetImage image =new GetImage();
bmp =image.getImage(url);
} catch (Exception e) {
Log.v("img", e.getMessage());
return null;
}
}
this.gView = view;
return bmp;
}
protected void onPostExecute(Bitmap bm) {
if (bm != null) {
this.gView.setImageBitmap(bm);
this.gView = null;
}
}
}
}
http://www.eoeandroid.com/viewthread.php?tid=26588
关于Rokon
Rokon 是Android平台一个开源(New BSD)的2D OpenGL 游戏引擎,目前版本为 2.0.3 (09/07/10) 。其中集成了Box2D,帮助你在游戏中实现物理效果。
网站:http://rokonandroid.com/
google code: http://code.google.com/p/rokon/
我收集了一些资料,如果有需要的留下邮箱
刚开始学 谢了!
现在急用,谢谢
谢谢了,急用
wxw404@gmail.com
多谢楼主
希望楼主还能看见
要如何通过程序控制Activity的显示方向?在Android中,若要通过程序改变屏幕显示的方向,必须要覆盖setRequestedOrientation()方法,而若要取得目前的屏幕方向,则需要访问getRequestedOrientation()方法。
本范例为求简要示范更改做法,设计了一个按钮,当单击按钮的同时,判断当下的屏幕方向,例如竖排(PORTRAIT),则将其更改为横排(LANDSCAPE);若为横排(LANDSCAPE),则将其更改为竖排(PORTRAIT),范例非常简单。图5-25所示是运行的结果。
本程序重写setRequestedOrientation()方法,其目的是为了要捕捉设置屏幕方向的同时所触发的事件,并在更改的时候,以Toast显示要更改的方向。
程序一开始(onCreate)先判断getRequestedOrientation()的值是否为-1,若此值为-1,表示在Activity属性里没有设置Android:screenOrientation的值,这意味着即使单击按钮,也无法判断屏幕的方向,不会进行更改方向的事件了。
在被覆盖的setRequestedOrientation()事件里,会传入要转换的方向常数(requestedOrientation),其值为整数类型,有以SCREEN_ORIENTATION_PORTRAIT及SCREEN_ORIENTATION_LAN- DSCAPE两个指定常数。
/* import程序略 */
import android.content.pm.ActivityInfo;
import android.view.Display;
public class EX05_22 extends Activity
{
private TextView mTextView01;
private Button mButton01;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton01 = (Button)findViewById(R.id.myButton1);
mTextView01 = (TextView)findViewById(R.id.myTextView1);
if(getRequestedOrientation()==-1)
{
mTextView01.setText(getResources().getText
(R.string.str_err_1001));
}
/* 当单击按钮旋转屏幕画面 */
mButton01.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
/* 方法一:重写getRequestedOrientation */
/* 若无法取得screenOrientation属性 */
if(getRequestedOrientation()==-1)
{
/* 提示无法进行画面旋转功能,因无法判别Orientation */
mTextView01.setText(getResources().getText
(R.string.str_err_1001));
}
else
{
if(getRequestedOrientation()==
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
{
/* 若目前为横排,则更改为竖排呈现 */
setRequestedOrientation
(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if(getRequestedOrientation()==
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
{
/* 若目前为竖排,则更改为横排呈现 */
setRequestedOrientation
(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
}
});
}
@Override
public void setRequestedOrientation(int requestedOrientation)
{
// TODO Auto-generated method stub
/* 判断要更改的方向,以Toast提示 */
switch(requestedOrientation)
{
/* 更改为LANDSCAPE */
case (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE):
mMakeTextToast
(
getResources().getText(R.string.str_msg1).toString(),
false
);
break;
/* 更改为PORTRAIT */
case (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT):
mMakeTextToast
(
getResources().getText(R.string.str_msg2).toString(),
false
);
break;
}
super.setRequestedOrientation(requestedOrientation);
}
@Override
public int getRequestedOrientation()
{
// TODO Auto-generated method stub
/* 此重写getRequestedOrientation方法,可取得目前屏幕的方向 */
return super.getRequestedOrientation();
}
public void mMakeTextToast(String str, boolean isLong)
{
if(isLong==true)
{
Toast.makeText(EX05_22.this, str, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(EX05_22.this, str, Toast.LENGTH_SHORT).show();
}
}
}
请留意在AndroidManifest.xml当中需要设置Activity的Android:screenOrientation属性,否则,程序将无法通过getRequestedOrientation()方法,来判断现在Activity的方向。
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="irdc.ex05_22"
android:versionCode="1"
android:versionName="1.0.0">
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".EX05_22"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
在上面的程序里,是以调用getRequestedOrientation()方法来判断单击按钮时,屏幕的显示方向虽然程序也可以进行判断,但以下方法可以适用在长宽比不一样的手机上。
/* 方法二:判断屏幕宽高比 */
final Display defaultDisplay =
getWindow().getWindowManager().getDefaultDisplay();
int h= defaultDisplay.getHeight();
int w = defaultDisplay.getWidth();
/* 此分辨率为按钮单击当下的分辨率 */
mTextView01.setText
(Integer.toString(h)+"x"+Integer.toString(w));
/if(w > h)
{
/* Landscape */
/* 重写Activity里的setRequestedOrientation()方法 */
setRequestedOrientation
(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else
{
/* Portrait */
setRequestedOrientation
(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}