图片viewpage动态添加小圆点

代码

package com.xjkj.gltest;




import java.util.ArrayList;
import java.util.List;


import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.xjkj.gl_adapter.ViewPageAdapter;
import com.xjkj.gl_tbase.BaseActivityf;
import com.xjkj.gl_util.UtilsSharedPreferences;
/**
 * 
 * @author:moshangpiaoxue
 * @file_name:WelcomeActivity.java
 * @todo:欢迎界面
 * @date:2016-3-16
 * @time: 上午10:59:46
 */
@ContentView(R.layout.welcome)
public class WelcomeActivity extends BaseActivityf{


@ViewInject(R.id.ll_point)private LinearLayout ll_point;
@ViewInject(R.id.vp_images)private ViewPager vp_imagers;
private int[] images={R.drawable.boot_1,R.drawable.boot_2,R.drawable.boot_3,R.drawable.boot_4};
private List<ImageView> list;//图片集合
private ImageView[] dots;//圆点集合
private String landingstate;//是否首次登陆
private int prePosition =0;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);


landingstate = (String) UtilsSharedPreferences.get(this, "landingstate", "false");


initImage();
initPoint();


// if (landingstate.equals("false")) {
vp_imagers.setAdapter(new ViewPageAdapter(list, this));
setChangeListener();
// }else if (landingstate.equals("true")) {
// startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
// finish();
// }
}




/**
* 初始化图片
*/
private void initImage() {
list= new ArrayList<ImageView>();
for(int i=0;i<images.length;i++){
//设置图片
ImageView now_pic=new ImageView(this);
now_pic.setBackgroundResource(images[i]);
//设置缩放模式
now_pic.setScaleType(ScaleType.FIT_XY);
list.add(now_pic);
}
}
/**
* 初始化圆点
*/
private void initPoint() {
for(int i=0;i<images.length;i++){
//设置圆点
ImageView now_dot=new ImageView(WelcomeActivity.this);
LayoutParams params=new LayoutParams(15, 15);
now_dot.setBackgroundResource(R.drawable.button_gray);
//设置圆点的宽高和左边距
params.setMargins(10, 0, 10, 0);
now_dot.setLayoutParams(params);
//设置缩放模式
now_dot.setScaleType(ScaleType.FIT_XY);
ll_point.addView(now_dot);
}
//设置第一个圆点为黄色
ll_point.getChildAt(0).setBackgroundResource(R.drawable.button_yellow);
}


/**
* 图片变化监听
*/
private void setChangeListener() {
vp_imagers.setOnPageChangeListener(new OnPageChangeListener() {


@Override
public void onPageSelected(int position) {
ll_point.getChildAt(position).setBackgroundResource(R.drawable.button_yellow);
ll_point.getChildAt(prePosition).setBackgroundResource(R.drawable.button_gray);
prePosition = position;
if(position==images.length){


}
}
@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
}
});
UtilsSharedPreferences.put(this, "landingstate", "true");
}


}

布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/welcome_ll"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <android.support.v4.view.ViewPager
        android:id="@+id/vp_images"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="5" >
        </LinearLayout>


        <LinearLayout
            android:id="@+id/ll_point"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal" >
        </LinearLayout>
    </LinearLayout>


</FrameLayout>




有一个缺陷是不能控制圆点的间距,我知道是用线性布局的原因 ,但是不知道怎么写,有会的大神请留言告诉下  谢谢了!

你可能感兴趣的:(图片viewpage动态添加小圆点)