android小圆点

1.布局

<android.support.v4.view.ViewPager
    android:id="@+id/vp_gps"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  />
<LinearLayout
    android:id="@+id/ll_dot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:layout_marginBottom="12dp"
    android:orientation="horizontal">
LinearLayout>
2.在drawable中画小圆点

①.椭圆 

xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    >

    <solid android:color="#009900"/>
    <size android:height="5dp" android:width="10dp"/>
     <corners android:radius="20dp"/>
shape>
②.圆

xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >

    <solid android:color="#ff0000"/>

     <size android:height="10dp"
          android:width="10dp"/>
shape>
③.空心圆

xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
      <stroke android:color="#999999" android:width="1dp"/>
      <solid  android:color="@android:color/transparent"  />
      <size android:height="25dp"
          android:width="25dp"/>
shape>

3.代码
①添加小圆点
private void addDots() {
    for (int i = 0; i < img.length; i++)
    {
        //有多少个图片定义多少imageview
        ImageView i1=new ImageView(this);
        if(i==0)
        {
            i1.setImageResource(R.drawable.red_dot);
        }
        else
        {
            i1.setImageResource(R.drawable.whiter_dot);
        }
        //圆点大小
        LinearLayout.LayoutParams ll=new LinearLayout.LayoutParams(10,10);
        //圆形间距
        ll.setMargins(5,0,5,0);
        //把ImageView放在LinearLayout中  把圆点放在LinearLayout
        ll_dot.addView(i1,ll);
    }
}



②小圆点跟着滑动
private void slideDots(int position) {
    for (int i = 0; i < img.length; i++) {
        //根据下标拿圆点(imageview对象 强转)
        ImageView childAt = (ImageView) ll_dot.getChildAt(i);
       if(position==i)
        {
           //设置第一个为选中颜色
          childAt.setImageResource(R.drawable.red_dot);
        }
        else
       {
           childAt.setImageResource(R.drawable.whiter_dot);
       }
    }

}

③.倒计时
//每秒运行一次onTick方法啊,间隔多少秒后运行onFinish方法
CountDownTimer timer=new CountDownTimer(3100,1000){
    @Override
    public void onTick(final long l){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dot.setText(l/1000+"");
            }
        });
    }



你可能感兴趣的:(android小圆点)