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.

  • 相关阅读:
    在VS2005中 GridView导入Excel的两点小技巧
    ASP.NET页面事件:顺序与回传详解
    .NET泛型编程简介
    关于ASP.NET在IIS一些问题的经验总结
    ASP.NET生成静态页面实现方法
    ASP.NET 2.0防止同一用户同时登陆
    【经验总结】简陋无比的观察者模式实现
    javascript变量作用域一点总结
    javascript中"/"运算符常见错误
    【经验总结】构造函数的强制调用
  • 原文地址:https://www.cnblogs.com/qiengo/p/2457709.html
Copyright © 2011-2022 走看看