Imageview

package wld.bawei.com.myonemoni.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

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

import wld.bawei.com.myonemoni.Bean;
import wld.bawei.com.myonemoni.R;

/**
 * Created by 大金鑫 on 2017/12/2.
 */

public class Mylunbo extends LinearLayout{

    private LinearLayout ll;
    private ImageView imageView;
    private List<ImageView> image=new ArrayList<>();
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int currentItem = pager.getCurrentItem();
            pager.setCurrentItem(currentItem+1);
            handler.sendEmptyMessageDelayed(0,5000);
        }
    };
    private ViewPager pager;
    private View view;

    public Mylunbo(Context context) {
        this(context,null);
    }

    public Mylunbo(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public Mylunbo(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        view = View.inflate(getContext(), R.layout.lunbo, this);
        pager = view.findViewById(R.id.pager);
        jiadian();
        handler.sendEmptyMessage(5000);
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i=0;i<image.size();i++)
                {
                    if(i==position%image.size())
                    {
                        image.get(i).setImageResource(R.drawable.dot_focuable);//修改图片颜色
                    }else
                    {
                        image.get(i).setImageResource(R.drawable.dot_normal);
                    }
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
        public void jiadian() {
            Log.e("aaa","aaa");
            //得到LinearLayout布局
            ll = (LinearLayout) view.findViewById(R.id.ll);
            image.clear();//清空图片集合
            ll.removeAllViews();//清空LinearLayout布局
            for (int i=0;i<4;i++)//添加
            {
                //创建一个图片布局
                imageView = new ImageView(getContext());
                if(i==0)
                {
                    imageView.setImageResource(R.drawable.dot_focuable);
                }else
                {
                    imageView.setImageResource(R.drawable.dot_normal);
                }
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);//圆点大小
                image.add(imageView);//圆点添加到图片集合中
                ll.addView(imageView,layoutParams);//圆点添加到LinearLayout中
            }
        }
    public void setAdapter(Myadapter myadapter){
        pager.setAdapter(myadapter);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值