xml
<RelativeLayout 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" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal" >
</LinearLayout>
</RelativeLayout>
建立drawable文件夹 添加三个xml
point_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:drawable="@drawable/point_focused" />
<item android:state_enabled="false" android:drawable="@drawable/point_nomal" />
</selector>
point_focused.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:width="5dp" android:height="5dp" />
<solid android:color="#ff0" />
</shape>
point_nomal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:width="5dp" android:height="5dp" />
<solid android:color="#f00" />
</shape>
java代码
package com.example.views;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import com.example.welcome.adapter.MyAdapter;
public class HomeActivity extends Activity {
int[] img = new int[] { R.drawable.logo_start, R.drawable.img1,
R.drawable.img2, R.drawable.img3
};
private List<View> list;
private int lastPointIdex = 0;
private LinearLayout linearLayout;
private TextView textView;
private Boolean down;
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
SharedPreferences sharedPreferences = getSharedPreferences("one", 0);
boolean boolean1 = sharedPreferences.getBoolean("pase", true);
if (boolean1) {
pager = (ViewPager) findViewById(R.id.view_pager);
linearLayout = (LinearLayout) findViewById(R.id.linear);
list = setdata();
pager.setAdapter(new MyAdapter(list));
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
// 将当前指示点的图片enable设为 true
linearLayout.getChildAt(arg0).setEnabled(true);
// 将上一个指示点的设置enable 设为false;
linearLayout.getChildAt(lastPointIdex).setEnabled(false);
// 更新lastPointIdex的值
lastPointIdex = arg0;
if (arg0 == 3) {
textView.setVisibility(View.VISIBLE);
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences sharedPreferences = getSharedPreferences(
"one", 0);
Editor edit = sharedPreferences.edit();
edit.putBoolean("pase", false);
edit.commit();
startActivity(new Intent(
getApplicationContext(),
MainActivity.class));
}
});
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
} else {
startActivity(new Intent(getApplicationContext(),
MainActivity.class));
}
}
public List<View> setdata() {
list = new ArrayList<View>();
for (int i = 0; i < img.length; i++) {
View view = View.inflate(getApplicationContext(), R.layout.pager,
null);
textView = (TextView) view.findViewById(R.id.textView);
view.setBackgroundResource(img[i]);
list.add(view);
}
for (int i = 0; i < list.size(); i++) {
// 添加指示点
ImageView point = new ImageView(this);
/*
* 子view的LayoutParams参数,要看,当前父view是谁,
* 由于现在point的父view是LinearLayout,所以为point 添加
* LinearLayout.LayoutParams
*/
LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, -2);
params.leftMargin = 15;
point.setLayoutParams(params);
point.setBackgroundResource(R.drawable.point_bg);
linearLayout.addView(point);
// 默认情况下,第一个小点enable为true
if (i == 0) {
point.setEnabled(true);
} else {
point.setEnabled(false);
}
}
return list;
}
}
适配器
package com.example.welcome.adapter;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class MyAdapter extends PagerAdapter {
private List<View> list;
public MyAdapter(List<View> list) {
// TODO Auto-generated constructor stub
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
View view = list.get(position);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View) object);
}
}