• fragment+原点 自动轮播


    import java.util.ArrayList;
    import java.util.Timer;
    import java.util.TimerTask;

    import com.lidroid.xutils.BitmapUtils;

    import android.os.Bundle;
    import android.os.Handler;
    import android.app.Activity;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.ImageView.ScaleType;
    import android.widget.LinearLayout;

    public class MainActivity extends Activity {
        private ViewPager vp;
        private LinearLayout dotcontaint;
        ArrayList<ImageView> imgs ;
        ArrayList<View> dots ;
        
        int currentIndex = 0;
        int olderIndex = 0;
        //图片数据
        private String[] image_url = new String[]{
                "http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg",
                "http://img3.3lian.com/2013/s1/20/d/57.jpg",
                "http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg",
                "http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg"};
        
        Handler h = new Handler(){
            public void handleMessage(android.os.Message msg) {

        if (currentIndex == gallery.size()) {
                        currentIndex = 0;
                    }
                vp.setCurrentItem(currentIndex);//设置此次要显示的pager
                //切换选中的圆点
                dots.get(olderIndex).setBackgroundResource(R.drawable.dot_nomal);//设置上次选中的圆点为不选中
                dots.get(currentIndex%image_url.length).setBackgroundResource(R.drawable.dot_focus);//设置当前选中的圆点为选中
                olderIndex = currentIndex%image_url.length;
                
            };
        };
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            init();
        }
        
        public void init(){
            vp = (ViewPager) findViewById(R.id.vp);
            dotcontaint = (LinearLayout) findViewById(R.id.dotcontaint);
            
            //获得网络图片,配置给Veiwpager
            getImageViewList();
            //获得圆点
            getDotList();
            //设置第一个圆点为选中状态
            dots.get(0).setBackgroundResource(R.drawable.dot_focus);
            vp.setAdapter(new MyVpAdapger());//配置pager页
            int currentItem = Integer.MAX_VALUE/2-Integer.MAX_VALUE/2%image_url.length;
            vp.setCurrentItem(currentItem);
            //通过定时器,制作自动划动效果
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    currentIndex = vp.getCurrentItem()+1;//下一个页
                
                    h.sendEmptyMessage(0x123);//在此线程中,不能操作ui主线程
                }
            }, 3000, 2000);
            
        }
        
        private void getDotList() {
            // TODO Auto-generated method stub
            dots = new ArrayList<View>();
            //循环图片数组,创建对应数量的dot
            for(int i=0;i<image_url.length;i++){
                View view = LayoutInflater.from(this).inflate(R.layout.dot_layout, null);//加载布局
                View dot = view.findViewById(R.id.dotView);//得到布局中的dot点组件
                //收集dot
                dots.add(dot);
                //把布局添加到线性布局
                dotcontaint.addView(view);
            }
            
        }

        class MyVpAdapger extends PagerAdapter{

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return Integer.MAX_VALUE;
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0==arg1;
            }
            
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                // TODO Auto-generated method stub
    //            super.destroyItem(container, position, object);
    //            ImageView img = imgs.get(position);
                container.removeView((View)object);
            }
            
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                // TODO Auto-generated method stub
                int item = position%image_url.length;
                container.addView(imgs.get(item));
                return imgs.get(item);
            }
            
        }

        /**
         * 到网络获得图片信息,并赋值到ImageView中
         */
        private void getImageViewList() {
            // TODO Auto-generated method stub
            imgs = new ArrayList<ImageView>();
            BitmapUtils btUtil = new BitmapUtils(this);
            //加载图片
            for(int i=0;i<image_url.length;i++){
                ImageView img = new ImageView(this);
                img.setScaleType(ScaleType.FIT_XY);
                
                btUtil.display(img, image_url[i]);
                imgs.add(img);
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }

    }

    //xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        tools:context=".MainActivity" >

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

        <LinearLayout
            android:id="@+id/dotcontaint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center" />

    </RelativeLayout>

    //dot的item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <View
            android:id="@+id/dotView"
            android:layout_width="8dp"
            android:layout_height="8dp"
            android:layout_margin="10dp"
            android:background="@drawable/dot_nomal"
            />

    </LinearLayout>

    //drwable的xml文件

    //正常显示

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

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

        <corners android:radius="8dip" />

    </shape>

    //选择后的显示

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <solid android:color="#ff0000"/>
        <corners android:radius="8dip"/>

    </shape>

  • 相关阅读:
    程序人生,编程思想
    CentOS Linux Jenkins安装、部署、更新
    Git常用命令
    U盘安装Mac OS X要点
    Shell执行*.sql
    WebStorm远程调试Node.js
    svn常用命令
    敏捷开发相关编辑思想(SOA、DDD、REST、CQRS)
    VisualVM远程监控Java
    centos搭建git服务
  • 原文地址:https://www.cnblogs.com/weiyangge/p/5382822.html
Copyright © 2020-2023  润新知