您的位置:首页 > 移动开发 > Android开发

android 循环播放图片实现

2014-01-20 20:57 387 查看
代码很简单,2个控件。一个ViewFlipper装载显示图片,一个LinearLayout装载图片指示点。加载网络图片,每4秒换一次。

Timer timer =new Timer();
    Handler handler = new Handler();
    int pre_pos = 0;
    int count = 0;
    void showScreenShot() {
        screen_shots.clear();
        screen_shots = ai.screenshots;
        vf_show = (ViewFlipper) findViewById(R.id.vf_show);
        ll_dot = (LinearLayout) findViewById(R.id.ll_dot);
        for(int i=0;i<screen_shots.size();i++){
            ImageView iv1 = new ImageView(DetailActivity.this);
            LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
            iv1.setLayoutParams(lp);
            String icon_url = FilePath.SERVER_URL+screen_shots.get(i);
            ImageFetcher fetcher = SharedImageFetcher.getSharedFetcher(DetailActivity.this);
            BaseImageFetchTask task = fetcher.getBaseTask(icon_url);
            task.setTaskType(TaskType.BACKGROUND);
            fetcher.loadImage(task, iv1);
            vf_show.addView(iv1); ImageView iv_dot = new ImageView(DetailActivity.this);
            LinearLayout.LayoutParams lp_dot = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            lp_dot.setMargins(5,0,0,0);
            iv_dot.setLayoutParams(lp_dot);
            if(i == 0){
                iv_dot.setBackgroundResource(R.drawable.dot_white);
            }else{
                iv_dot.setBackgroundResource(R.drawable.dot_black);
            }
            ll_dot.addView(iv_dot);
        }

        pre_pos = 0;
        count = 0;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        int pos = (count++) % ll_dot.getChildCount();
                        ll_dot.getChildAt(pre_pos).setBackgroundResource(R.drawable.dot_black);
                        vf_show.setDisplayedChild(pos);
                        ll_dot.getChildAt(pos).setBackgroundResource(R.drawable.dot_white);
                        DeLog.d(TAG,"pre_pos = "+pre_pos+",pos="+pos);
                        pre_pos = pos;
                    }
                });
            }
        }, 0, 4000);

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: