标签:
public class MainActivity extends Activity {
private ViewPager viewpager;
private LinearLayout l;
private int[] img = { R.drawable.q, R.drawable.w, R.drawable.e,
R.drawable.r };
private ArrayList<ImageView> dos;
private ArrayList<ImageView> imagelist;
private SharedPreferences preferences;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找对象
viewpager = (ViewPager) findViewById(R.id.viewpager);
l = (LinearLayout) findViewById(R.id.l);
// handler = new Handler() {
// @Override
// public void handleMessage(Message msg) {
// int currentItem = viewpager.getCurrentItem();
// viewpager.setCurrentItem(++currentItem);
// handler.sendEmptyMessageDelayed(1, 2000);
// }
// };
// 判断
preferences = getSharedPreferences("name", MODE_PRIVATE);
boolean boolean1 = preferences.getBoolean("flag", false);
if (!boolean1) {
// 初始化数据
// intImage();
// viewpager适配器
viewpager.setAdapter(new MyPagerAdapter());
} else {
Intent intent = new Intent(MainActivity.this, QweActivity.class);
startActivity(intent);
}
// 初始化小圆点
intDos();
// // 初始化位置
// viewpager.setCurrentItem(10000);
// 加监听
viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
for (int i = 0; i < dos.size(); i++) {
if (position % dos.size() == i) {
dos.get(i).setImageResource(R.drawable.checked);
} else {
dos.get(i).setImageResource(R.drawable.normal);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
// handler.sendEmptyMessageDelayed(1, 2000);
}
private void intDos() {
dos = new ArrayList<ImageView>();
for (int i = 0; i < img.length; i++) {
ImageView iv = new ImageView(this);
if (i == 0) {
iv.setImageResource(R.drawable.checked);
} else {
iv.setImageResource(R.drawable.normal);
}
// 设置空间大小
LayoutParams layoutParams = new LayoutParams(10, 10);
// 设置宽高
layoutParams.setMargins(5, 0, 5, 0);
// 把小圆点放到页面的LinnearLayout容器中
l.addView(iv, layoutParams);
// 把小圆点存放到集合中
dos.add(iv);
}
}
// private void intImage() {
// imagelist = new ArrayList<ImageView>();
// for (int i = 0; i < img.length; i++) {
// ImageView view = new ImageView(this);
// view.setImageResource(img[i]);
// view.setScaleType(ImageView.ScaleType.FIT_XY);
// imagelist.add(view);
// if (i == img.length - 1) {
// view.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View arg0) {
// Editor edit = preferences.edit();
// edit.putBoolean("flag", true);
// edit.commit();
// Intent intent = new Intent(MainActivity.this,
// QweActivity.class);
// startActivity(intent);
// }
// });
// }
// }
// }
class MyPagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView view = new ImageView(MainActivity.this);
view.setImageResource(img[position]);
// 图片大小
view.setScaleType(ScaleType.FIT_XY);
container.addView(view);
if (position == img.length - 1) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//存值
Editor edit = preferences.edit();
edit.putBoolean("flag", true);
edit.commit();
// 跳转
Intent intent = new Intent(MainActivity.this,
QweActivity.class);
startActivity(intent);
finish();
}
});
}
return view;
}
@Override
public int getCount() {
return img.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
标签:
原文地址:http://www.cnblogs.com/3674-it/p/5550882.html