zoukankan      html  css  js  c++  java
  • Android:ImageSwitcher

    ImageSwitcher is a subclass of ViewSwitcher. You can implement a ablum by ImageSwitcher.

    The following is a little demo:

    package com.slowalker.imageswitcherdemo;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.util.Log;
    import android.view.ActionMode;
    import android.view.Menu;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.view.ViewGroup.LayoutParams;
    import android.view.animation.AnimationUtils;
    import android.widget.FrameLayout;
    import android.widget.ImageSwitcher;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    
    public class MainActivity extends Activity implements ImageSwitcher.ViewFactory {
    
        private static final String TAG = MainActivity.class.getSimpleName();
        private static final boolean DEBUG = true;
        
        private ImageSwitcher mGallery;
        
        private int[] imgs = new int[] {
                R.drawable.ic_launcher,
                R.drawable.ic_launcher,
                R.drawable.ic_launcher,
                R.drawable.ic_launcher
        };
        
        private int index = 0;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mGallery = new ImageSwitcher(this);
            /*Set ViewFactory for the ImageSwitcher.*/
            mGallery.setFactory(this);
            mGallery.setImageResource(imgs[index]);
            mGallery.setOnTouchListener(new OnTouchListener() {
                
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (DEBUG) {
                        Log.d(TAG, "onTouch");
                        Log.d(TAG, "action = " + event.getAction());
                    }
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        if (DEBUG) {
                            Log.d(TAG, "ACTION_DOWN");
                        }
                        index = (index + 1) % imgs.length;
                        mGallery.setImageResource(imgs[index]);
                    }
                    return false;
                }
            });
            
            /*Set Animation for the ImageSwitcher.*/
            mGallery.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));
            mGallery.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));
            
            setContentView(mGallery);
        }
    
        @Override
        public View makeView() {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                                                            FrameLayout.LayoutParams.WRAP_CONTENT));
            return iv;
        }
    
    }
  • 相关阅读:
    电子电路基础复习 —— 电阻
    Linux 网络编程(IO模型)
    Linux 2.6 源码学习-内存管理-buddy算法
    【转】MySQL性能优化的21个最佳实践
    linux 2.6 驱动笔记(三)
    linux 2.6 驱动笔记(二)
    公共代码参考(httpclient)
    linux 2.6 驱动笔记(一)
    bzoj 2401: 陶陶的难题I 数论
    bzoj 3144: [Hnoi2013]切糕 最小割
  • 原文地址:https://www.cnblogs.com/slowalker/p/3401761.html
Copyright © 2011-2022 走看看