zoukankan      html  css  js  c++  java
  • android autoswitched ImageSwitcher

      First,ImageSwitcher use setImageDrawable and setBackgroundDrawable to set the content.You'll need Timer class if you want to switch the content automatically.However,you can't change the UI content in a Timer,so you need a handler to send the message:

    private Timer switchTimer;
    private ImageSwitcher imageSwitcher;
    private Handler mDownloadHandler = new Handler() {
                @Override
                public void dispatchMessage(Message msg) {
                    switch (msg.what) {
                        case MSG_ADS_REFRESH:
                            int count=adsList.size();
                            if(count>0){
                                curads=(curads+1)%count;
                                Bitmap t=ImageUtilities.getCachedCover(adsList.get(curads).id);
                                imageSwitcher.setImageDrawable(new BitmapDrawable(t));
                            }
                            break;
                        default:
                            break;
                    }
                    super.dispatchMessage(msg);
                }
            };
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main)
            
            imageSwitcher=(ImageSwitcher)findViewById(R.id.store_main_ads);
            imageSwitcher.setFactory(new ViewFactory() {
                
                @Override
                public View makeView() {
                    ImageView iv = new ImageView(StoreMainActivity.this);
                    ImageSwitcher.LayoutParams lp=new ImageSwitcher.LayoutParams(
                            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    lp.gravity=Gravity.CENTER;
                    iv.setLayoutParams(lp);
                    return iv;
                }
            });
            imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
            imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
            adsList=new ArrayList<AdsImage>();
         switchTimer=new Timer();
            switchTimer.schedule(new TimerTask() {
                
                @Override
                public void run() {
                    mDownloadHandler.sendEmptyMessage(MSG_ADS_REFRESH);
                }
            },10, 7000);
    }

    Don't forget to cancel the timer when you exit(ignore may be OK):

      @Override
        protected void onDestroy() {
            switchTimer.cancel();
            super.onDestroy();
        }

     The adsList in code is the content list of the ImageSwitch.

  • 相关阅读:
    vue 的模板编译—ast(抽象语法树) 详解与实现
    Vue 组件(component)之 精美的日历
    nvm 装 nodejs 重启终端失效的解决方法
    vue 2 仿IOS 滚轮选择器 从入门到精通 (一)
    np.stack() 与 tf.stack() 的简单理解
    PHP 之 Ci框架下隐藏index.php
    Boosting 简单介绍
    Adaboost算法流程及示例
    Python 之 解码汉字乱码(如果gbk、utf8都试过不行,可以试试这个)
    Linux 之 tar和nc传文件
  • 原文地址:https://www.cnblogs.com/qiengo/p/2457709.html
Copyright © 2011-2022 走看看