Android banner图片轮播

图片轮播控件,使用Picasso加载图片

项目地址:http://download.csdn.net/download/weixin_40391500/10232185
轨迹球类PointBall.java

/**
 * 轨迹球
 * Created by pc20170521 on 2018-01-29.
 */

public class PointBall extends LinearLayout {
    private Context mContext;
    private ArrayList<ImageView> pointViewList = new ArrayList<>();//轨迹球list
    private int pointViewCount = 0;//轨迹球个数
    private int pointViewCunrent = 0;//轨迹球当前位置

    public PointBall(Context context) {
        super(context);
        this.mContext = context;
        initPoint();
    }

    public PointBall(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initPoint();
    }

    /****初始化轨迹球布局**/
    private void initPoint() {
        setGravity(Gravity.CENTER_HORIZONTAL);
        pointViewList.clear();
        for (int i = 0; i < pointViewCount; i++) {
            ImageView iv = new ImageView(mContext);
            pointViewList.add(iv);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);//将图片边界缩放,以适应视图边界时的可选项
            if (i == 0){
                Picasso.with(mContext).load(R.mipmap.banner_roller_selected).into(iv);
            }else {
                Picasso.with(mContext).load(R.mipmap.banner_roller_unselected).into(iv);
            }

            LayoutParams params = new LayoutParams(20, 20);
            addView(iv, params);
        }
    }

    /*****初始化轨迹球的个数****/
    public void initPointBallCount(int size){
        if (size >= 0){
            pointViewCount = size;
            initPoint();
        }
    }

    /****更新轨迹球的位置***/
    public void updatePointBallPosition(int position){
        if (position <= pointViewCount - 1){
            Picasso.with(mContext).load(R.mipmap.banner_roller_unselected).into(pointViewList.get(pointViewCunrent));
            pointViewCunrent = position;
            Picasso.with(mContext).load(R.mipmap.banner_roller_selected).into(pointViewList.get(pointViewCunrent));
        }
    }

}

图片轮播控件类ImageFlipper.java

/**
 * 图片轮播控件
 * Created by pc20170521 on 2018-01-29.
 */

public class ImageFlipper extends RelativeLayout implements GestureDetector.OnGestureListener {

    private Context mContext;
    private ViewFlipper viewFlipper;
    public static int intervalTime = 3000;//图片轮播时间间隔
    private PointBall pointBall;//轨迹球类
    private MyHandler handler;
    private int tatol;//图片总数
    private GestureDetector mGestureDetector;//手势

    /****设置图片的宽高***/
    public static int width = 0;
    public static int height = 0;

    private ImageThread imageThread;

    public ImageFlipper(Context context) {
        super(context);
        this.mContext = context;
    }

    public ImageFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public ImageFlipper(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
    }

    /*****
     * 初始化布局,启动图片轮播
     * ids 图片数组
     * drawable 默认图片
     * ****/
    public void init(int []ids, int drawable){
        mGestureDetector = new GestureDetector(this);
        handler = new MyHandler();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        final View view = inflater.inflate(R.layout.flipper, null);
        viewFlipper = (ViewFlipper) view.findViewById(R.id.view_flipper);
        pointBall = (PointBall) view.findViewById(R.id.banner_point);

        try {
            setBanner(ids, drawable);
            if (imageThread == null || imageThread.isInterrupted()){
                imageThread = new ImageThread();
                imageThread.start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        addView(view);
    }

    /****
     * 设置图片循环滚动
     * @param ids
     * @param drawable
     */
    private void setBanner(int []ids, int drawable){
        int size = ids.length;
        if (size == 0){
            tatol = 1;
            pointBall.initPointBallCount(tatol);
            final ImageView view = new ImageView(mContext);
            Picasso.with(mContext).load(drawable).into(view);
            addView(view);
        }else {
            tatol = size;
            pointBall.initPointBallCount(tatol);
            for (int i = 0; i < tatol; i++) {
                final ImageView view = new ImageView(mContext);
                view.setLayoutParams(new LayoutParams(width, height));
                Picasso.with(mContext).load(ids[i]).into(view);
                viewFlipper.addView(view);
            }
        }
    }

    /******
     * 设置哪个子视图将被显示出来
     * @param whacthChild 将要显示的子视图的索引
     */
    private void setDisplayedChild(int whacthChild){
        viewFlipper.setDisplayedChild(whacthChild);
        pointBall.updatePointBallPosition(whacthChild);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        int index = viewFlipper.getDisplayedChild();
        if (e1.getX() - e2.getX() > 60){//左滑
            viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_left_in));
            viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_left_out));
            if (index >= tatol -1){
                setDisplayedChild(0);
            }else {
                setDisplayedChild(index + 1);
            }
            return true;
        }else if (e2.getX() - e1.getX() > 60){//右滑
            viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_right_in));
            viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_right_out));
            if (index > 0){
                setDisplayedChild(index -1);
            }else {
                setDisplayedChild(tatol - 1);
            }
            return true;
        }
        return false;
    }

    /***关闭线程****/
    public void closeThread(){
        if (imageThread != null){
            imageThread.interrupt();
            imageThread = null;
        }
    }

    /****主线程更新UI***/
    private class MyHandler extends Handler{
        public MyHandler(){

        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_left_in));
            viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_left_out));
            int currentIndex = msg.getData().getInt("index");
            setDisplayedChild(currentIndex);
        }
    }

    /****t图片轮播线程***/
    class ImageThread extends Thread{
        @Override
        public void run() {
            super.run();
            while (true){
                try {
                    Thread.sleep(intervalTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int index = viewFlipper.getDisplayedChild() + 1;//获取将要显示的子视图索引
                if (index >= tatol){
                    index = 0;
                }
                Message message = Message.obtain();
                Bundle bundle = new Bundle();
                bundle.putInt("index", index);
                message.setData(bundle);
                handler.sendMessage(message);
            }
        }
    }
}

activity类中使用

private ImageFlipper image_flipper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        //设置图片的宽高
        ImageFlipper.width = dm.widthPixels;
        ImageFlipper.height = dm.heightPixels/3;
        //图片轮播时间间隔
        ImageFlipper.intervalTime = 3000;
        setContentView(R.layout.activity_main);

        image_flipper = (ImageFlipper) findViewById(R.id.image_flipper);

        int []ids = {R.mipmap.banner1, R.mipmap.banner2, R.mipmap.banner3};
        image_flipper.init(ids, R.mipmap.banner4);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (image_flipper != null){
            image_flipper.closeThread();
        }
    }

    //xml
    <com.example.imagecarousel.ImageFlipper
        android:id="@+id/image_flipper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"/>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值