效果图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSk1DNjAx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSk1DNjAx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSk1DNjAx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
功能描写叙述:
1、拖动能够切换view
2、点击dot能够跳转到对应view
3、每隔三秒自己主动切换view
实现:
1、先写布局main.xml文件,定义一个存放图片的ViewPager,和存放dot的LinearLayout
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="150dp">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/viewGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="405px"
android:orientation="horizontal" >
</LinearLayout>
2、主要实现代码:
a、初始化view视图,设置viewpager的适配器
private void initViewPager() {
// TODO Auto-generated method stub
items = new ArrayList<View>();
for(int i=0; i<4; i++){
image = new ImageView(this);
image.setBackgroundResource(ads[i]);
items.add(image);
}
viewpager = (ViewPager)findViewById(R.id.viewpager);
viewpager.setAdapter(adapter);
viewpager.setOnPageChangeListener(this);
}
b、初始化dot视图
private void initDot() {
// TODO Auto-generated method stub
viewGroup = (LinearLayout)findViewById(R.id.viewGroup);
dots = new ImageView[items.size()];
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(40, 40);
layoutParams.setMargins(4, 3, 4, 3);
for(int i=0 ; i<items.size(); i++){
dot = new ImageView(this);
dot.setLayoutParams(layoutParams);
dots[i] = dot;
dots[i].setTag(i);
dots[i].setOnClickListener(onClick);
if (i == 0) {
dots[i]
.setBackgroundResource(R.drawable.dotc);
} else {
dots[i]
.setBackgroundResource(R.drawable.dotn);
}
viewGroup.addView(dots[i]);
}
}
c、实现dot点击响应功能
OnClickListener onClick = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position = (Integer)v.getTag();
setCurView(position);
setCurDot(position);
}
};
d、自己主动切换视图
private void atomicOption() {
//获取当前的值,并自增
atomicInteger.incrementAndGet();
if (atomicInteger.get() > items.size() - 1) {
atomicInteger.getAndAdd(-4);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
}
源代码下载路径:http://download.csdn.net/detail/jmc601/7275991