<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/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:id="@+id/rl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp"> <LinearLayout android:id="@+id/linear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout> </RelativeLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="50dp" android:visibility="invisible" android:text="开始体验"/> </RelativeLayout>
private ViewPager viewPager;
private int[]images={R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3};
private List<ImageView>imageViews;//用来存放几个imageview的实例
viewPager.setAdapter(new MyAdapter());
imageViews=new ArrayList<ImageView>();
for(int i=0;i<images.length;i++){
ImageView imageView=new ImageView(this);
imageView.setImageResource(images[i]);
imageViews.add(imageView);
class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@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
container.addView(imageViews.get(position));
return imageViews.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View)object);
}
}
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="10dp" android:height="10dp"/> <!-- 填充颜色 --> <solid android:color="#ff0000"/> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="10dp" android:height="10dp"/> <!-- 填充颜色 --> <solid android:color="#ECECEC"/> </shape>
ImageView gray_Iv=new ImageView(this);
gray_Iv.setImageResource(R.drawable.gray_circle);
//使用LayoutParams改变控件的位置
LinearLayout.LayoutParams layoutParams=
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
if(i>0){
layoutParams.leftMargin=20;
}
gray_Iv.setLayoutParams(layoutParams);
li.addView(gray_Iv);
red_Iv=new ImageView(this); red_Iv.setImageResource(R.drawable.red_circle); rl.addView(red_Iv);
//示图树
red_Iv.getViewTreeObserver().
addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
//该方法就是在界面全面绘制结束之后回调
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
//求距离
left=li.getChildAt(1).getLeft()-li.getChildAt(0).getLeft();
System.out.println("left为"+left);
red_Iv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
viewPager的滑动监听器还要监听图片什么时候滑动,以至于让红点滑动的跟好看(就比如第一个点到第二个点的途中也会有红点)
//滑动的时候
@Override
public void onPageScrolled(int position, float posionOffset, int arg2) {
// TODO Auto-generated method stub
System.out.println(posionOffset);//滑动的百分比
RelativeLayout.LayoutParams layoutParams=
(RelativeLayout.LayoutParams)red_Iv.getLayoutParams();
layoutParams.leftMargin=(int)(left*posionOffset+position*left);
red_Iv.setLayoutParams(layoutParams);
}
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if(position==images.length-1){
btn.setVisibility(View.VISIBLE);
}else{
btn.setVisibility(View.GONE);
}
}
public class MainActivity extends Activity {
private ViewPager viewPager;
private int[]images={R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3};
private List<ImageView>imageViews;//用来存放几个imageview的实例
private LinearLayout li;
private RelativeLayout rl;
private ImageView red_Iv;
private int left;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) findViewById(R.id.viewPager);
li=(LinearLayout) findViewById(R.id.linear);
rl=(RelativeLayout) findViewById(R.id.rl);
btn=(Button) findViewById(R.id.btn);
viewPager.setAdapter(new MyAdapter());
imageViews=new ArrayList<ImageView>();
for(int i=0;i<images.length;i++){
ImageView imageView=new ImageView(this);
imageView.setImageResource(images[i]);
imageViews.add(imageView);
ImageView gray_Iv=new ImageView(this);
gray_Iv.setImageResource(R.drawable.gray_circle);
//使用LayoutParams改变控件的位置
LinearLayout.LayoutParams layoutParams=
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
if(i>0){
layoutParams.leftMargin=20;
}
gray_Iv.setLayoutParams(layoutParams);
li.addView(gray_Iv);
}
red_Iv=new ImageView(this);
red_Iv.setImageResource(R.drawable.red_circle);
rl.addView(red_Iv);
//示图树
red_Iv.getViewTreeObserver().
addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
//该方法就是在界面全面绘制结束之后回调
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
//求距离
left=li.getChildAt(1).getLeft()-li.getChildAt(0).getLeft();
System.out.println("left为"+left);
red_Iv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if(position==images.length-1){
btn.setVisibility(View.VISIBLE);
}else{
btn.setVisibility(View.GONE);
}
}
//滑动的时候
@Override
public void onPageScrolled(int position, float posionOffset, int arg2) {
// TODO Auto-generated method stub
System.out.println(posionOffset);//滑动的百分比
RelativeLayout.LayoutParams layoutParams=
(RelativeLayout.LayoutParams)red_Iv.getLayoutParams();
layoutParams.leftMargin=(int)(left*posionOffset+position*left);
red_Iv.setLayoutParams(layoutParams);
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@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
container.addView(imageViews.get(position));
return imageViews.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View)object);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有