Android开发之欢迎界面标准

时间:2022-02-08 18:11:57
 import java.util.ArrayList;

 import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams; //安卓应用欢迎界面标准
public class MainActivity extends Activity {
private ViewPager viewPager;
private ViewPagerAdapter pagerAdapter = new ViewPagerAdapter();
// 欢迎界面显示的视图ID
private int[] viewsId = { R.layout.wel1, R.layout.wel2, R.layout.wel3 };
// 缓冲界面显示的视图ID
private int cacheId = R.layout.wel2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences shared = this.getSharedPreferences("app_global",
MODE_WORLD_WRITEABLE);
viewPager = new ViewPager(this);
viewPager.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
if (!shared.contains("used")) {
pagerAdapter.setViewsId(viewsId);
shared.edit().putBoolean("used", true).commit();
} else {
pagerAdapter.setViewsId(cacheId);
}
viewPager.setAdapter(pagerAdapter); // 获取最后一个视图,以便绑定事件
View v = pagerAdapter.getLastView(); setContentView(viewPager);
} // 在其中写入当显示缓冲图片时执行的操作
protected void onShowCachePicture() { } private class ViewPagerAdapter extends PagerAdapter {
private ArrayList<View> viewList = new ArrayList<View>(); @Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
} @Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
} @Override
public int getCount() {
return viewList.size();
} @Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
} public void setViewsId(int... ids) {
LayoutInflater inflater = getLayoutInflater();
for (int id : ids) {
View v = inflater.inflate(id, null);
viewList.add(v);
}
} public View getLastView() {
return viewList.get(viewList.size() - 1);
}
} }