用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

倒计时/轮播

2017-02-28 作者: 老四和老三的老二都老大了举报

[android]代码库

//daojishi
private String[] imageView=new String[]{"http://p0.so.qhmsg.com/bdr/326__/t01620f66967a9f8c70.jpg",
    "http://p3.so.qhmsg.com/bdr/326__/t01ba898b85ee68c8dd.jpg","http://p4.so.qhmsg.com/bdr/326__/t01c8ccc5629cc31f2e.jpg",
    "http://p1.so.qhmsg.com/bdr/326__/t0168bc4191fc244d51.jpg"};
    private ImageView[] image=new ImageView[imageView.length];

    private int time=2*3600-1;
    private int hour=0;
    private int minute=0;
    private int second=0;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
           int a= vp.getCurrentItem();
            vp.setCurrentItem(++a);

            if(time>0) {
                hour = time / 3600;
                minute = (time - hour * 3600) / 60;
                second = time - hour * 3600 - minute * 60;

                date.setText(hour + "小时" + minute + "分钟" + second + "秒");
                time--;
            }

            handler.sendEmptyMessageDelayed(0,1000);
        }
    };

//main方法里
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = (ViewPager) findViewById(R.id.vp);
        ll = (LinearLayout) findViewById(R.id.ll);
        date = (TextView) findViewById(R.id.date);
        options = new DisplayImageOptions.Builder().build();
        setPorivter();
        setViewPager();
        handler.sendEmptyMessageDelayed(0,1000);
    }

//定义的方法
private void setViewPager() {
        vp.setAdapter(new MyView());
        vp.setCurrentItem(0);

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setPoint(position%imageView.length);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void setPoint(int index){
        for(int i=0;i<imageView.length;i++){
            if(i==index){
                image[i].setImageResource(R.drawable.selecter);
            }else{
                image[i].setImageResource(R.drawable.normal);
            }
        }
    }
    private void setPorivter() {
        for(int i=0;i<imageView.length;i++){
            ImageView img=new ImageView(this);
            img.setLayoutParams(new ViewGroup.LayoutParams(20,20));
            img.setScaleType(ImageView.ScaleType.FIT_XY);
            if(i==0){
                //上来就显示小圆点第一个就是红色
                img.setImageResource(R.drawable.selecter);
            }else{
                img.setImageResource(R.drawable.normal);
            }
            image[i]=img;
            //添加视图
            ll.addView(img);
        }
    }

    class MyView extends PagerAdapter{

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView img=new ImageView(MainActivity.this);
            img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            img.setScaleType(ImageView.ScaleType.FIT_XY);
            int index=position%imageView.length;
            ImageLoader.getInstance().displayImage(imageView[index],img,options);
            container.addView(img);

            return img;
        }
    }

//布局
<android.support.v4.view.ViewPager
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:id="@+id/vp"></android.support.v4.view.ViewPager>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:id="@+id/ll"
        android:layout_alignBottom="@+id/vp"></LinearLayout>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#f00"/>
    <stroke android:width="1dp" android:color="#0f0"/>

</shape>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#fff"/>
    <stroke android:width="1dp" android:color="#0f0"/>

</shape>


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...