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;
    }

}

 

posted @ 2013-11-01 14:01  slowalker  阅读(172)  评论(0编辑  收藏  举报