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.

  • 相关阅读:
    Android sqlite日期存储
    Ubuntu10.04中间Leach协议一键安装
    Chapter 1 Securing Your Server and Network(1):选择SQL Server业务经理
    lua迭代器和仿制药for
    设定值范围+区间覆盖
    新手可以学习cocos2dx 3.0 组态(两)
    王立平--string.Empty
    Javascript操作阵列
    Dojo仪表板
    时间复杂度
  • 原文地址:https://www.cnblogs.com/qiengo/p/2457709.html
Copyright © 2011-2022 走看看