2011.08.18——— android 图片效果 ImageSwitcher+GestureDetector

2011.08.18——— android 图片效果 ImageSwitcher+GestureDetector

参考: http://www.williamhua.com/2009/04/android-touchscreen-gesture-recogniton/

实现一个滚动的图片效果,并且可以用手势来控制图片的滚动
package com.lp;

import android.app.Activity;
import android.os.Bundle;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;
import android.view.GestureDetector;

public class MainActivity extends Activity
	implements ViewSwitcher.ViewFactory {
	
	private Animation mInAnimationPrior;
    private Animation mInAnimationNext;
	
    private ImageSwitcher mSwitcher;
    private ImageSwitcher mArrow;
    private Button        mbutton;
    
    private int mPosition = 0;
    private GestureDetector mGestureDetector;
    
    
    private Handler   _handle;
    private Runnable  _runable;
    
    private static final float HORIZONTAL_SCROLL_DISTANCE = 10f;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.startpage);
        
        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        if(mSwitcher==null)
        {
        	return;
        }
        mSwitcher.setFactory(this);
        
        mArrow = (ImageSwitcher) findViewById(R.id.switcharrow);
        mArrow.setFactory(this);
        //mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
        //        android.R.anim.fade_in));
        //mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
        //        android.R.anim.fade_out));
        
        mbutton = (Button) findViewById(R.id.swtichbutton);
        mbutton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mPosition < (mImageIds.length -1)) {  
					//当点击按钮时 就取消自动切换图片
					_handle.removeCallbacks(_runable);
					
					mSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,
			                R.anim.slide_in_right));
			        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,
			                R.anim.slide_out_left));    				
					mSwitcher.setImageResource(mImageIds[++mPosition]);
					
					mArrow.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,
			                R.anim.slide_in_right));
					mArrow.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,
			                R.anim.slide_out_left));    				
					mArrow.setImageResource(mArrowIds[mPosition]);
				}
				else if (mPosition == (mImageIds.length -1)) {
					_handle.removeCallbacks(_runable);
//					Intent intent = new Intent();
//					intent.setClass(MainActivity.this, RegisterActivity.class);
//	            	startActivity(intent);
//	            	MainActivity.this.finish();
					Toast.makeText(MainActivity.this, "注册", 0).show();
				}
				
			}
		});
        
        setupOnTouchListeners(findViewById(R.id.rootview));
        mSwitcher.setImageResource(mImageIds[mPosition]);
        mArrow.setImageResource(mArrowIds[mPosition]);
        
        _handle = new Handler();
        _runable = new Runnable() {		
			@Override
			public void run() {
				
				if (mPosition == (mImageIds.length -1)) {
//					Intent intent = new Intent();
//					intent.setClass(MainActivity.this, RegisterActivity.class);
//	            	startActivity(intent);
//	            	MainActivity.this.finish();
					Toast.makeText(MainActivity.this, "注册", 0).show();
				}
				else
				{
					mSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,
			                R.anim.slide_in_right));
			        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,
			                R.anim.slide_out_left));    				
					mSwitcher.setImageResource(mImageIds[++mPosition]);
					
					mArrow.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,
			                R.anim.slide_in_right));
					mArrow.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,
			                R.anim.slide_out_left));    				
					mArrow.setImageResource(mArrowIds[mPosition]);
					
					_handle.postDelayed(_runable, 3000);
				}
			}
		}; 
    }
    
    public View makeView() {
        ImageView i = new ImageView(this);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        return i;
    }
    
    private void setupOnTouchListeners(View rootView) {
    	mGestureDetector = new GestureDetector(this, new MyGestureListener());
    	
    	OnTouchListener rootListener = new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {                
                mGestureDetector.onTouchEvent(event);

                // We do not use the return value of
                // mGestureDetector.onTouchEvent because we will not receive
                // the "up" event if we return false for the "down" event.
                return true;
            }
        };
       
        rootView.setOnTouchListener(rootListener);
    }
    
    private class MyGestureListener extends
    	GestureDetector.SimpleOnGestureListener {

    	@Override
    	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
    		
    		    		
    		//
    		if ( Math.abs(velocityY) <= Math.abs(velocityX) 
    				&& Math.abs(velocityX) > HORIZONTAL_SCROLL_DISTANCE) {
    			//
    			System.out.println(velocityX);
    			if (velocityX > 0) {
    				if (mPosition > 0) {   					
    					_handle.removeCallbacks(_runable);    					
    					//
    					mSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,
    			                R.anim.slide_in_left));
    			        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,
    			                R.anim.slide_out_right));
    					mSwitcher.setImageResource(mImageIds[--mPosition]);
    					
    					mArrow.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,
    			                R.anim.slide_in_left));
    					mArrow.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,
    			                R.anim.slide_out_right));
    					mArrow.setImageResource(mArrowIds[mPosition]);
    				}
    			} else {
    				if (mPosition < (mImageIds.length -1)) {    					
    					_handle.removeCallbacks(_runable);
    					
    					mSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,
    			                R.anim.slide_in_right));
    			        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,
    			                R.anim.slide_out_left));    				
    					mSwitcher.setImageResource(mImageIds[++mPosition]);
    					
    					mArrow.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,
    			                R.anim.slide_in_right));
    					mArrow.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,
    			                R.anim.slide_out_left));    				
    					mArrow.setImageResource(mArrowIds[mPosition]);
    				}
    				else if (mPosition == (mImageIds.length -1)) {
    					_handle.removeCallbacks(_runable);
//    					Intent intent = new Intent();
//    					intent.setClass(MainActivity.this, RegisterActivity.class);
//    	            	startActivity(intent);
//    	            	MainActivity.this.finish();
    					Toast.makeText(MainActivity.this, "注册", 0).show();
    	            	return true;
    				}
    			}    		
    		}
    		
    		return true;
    	}
    }
    
    private Integer[] mImageIds = {
            R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2};
    
    private Integer[] mArrowIds = {
            R.drawable.switch_arrow, R.drawable.switch_arrow_1, R.drawable.switch_arrow_2};
    
    public void onResume()
    {
        super.onResume();  
        
        //_handle.postDelayed(_runable, 3000);
    }
    
    public void onPause()
    {
        super.onPause();
        _handle.removeCallbacks(_runable);
    }
    
    
    //
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {		
		
		if (keyCode == KeyEvent.KEYCODE_BACK) {   
		
			 _handle.removeCallbacks(_runable);
			//dialog
			AlertDialog.Builder builder = new AlertDialog.Builder(this);			
			builder.setMessage("是否退出注册过程?");			
			builder.setPositiveButton("是的", 
						new DialogInterface.OnClickListener(){
			     			public void onClick(DialogInterface dialog, int id) {			     				
			     				
			     			}
						});	
			builder.setNegativeButton("不是", 
					new DialogInterface.OnClickListener(){
     					public void onClick(DialogInterface dialog, int id) {
     						_handle.postDelayed(_runable, 3000);
     			}
			});
			
			builder.show();			         
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
		
	}
    
}



side_in_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
	<translate android:fromXDelta="-50%p" android:toXDelta="0"
            android:duration="@android:integer/config_longAnimTime"/>
	<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_longAnimTime" />
</set>

side_in_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<translate android:fromXDelta="50%p" android:toXDelta="0"
            android:duration="@android:integer/config_longAnimTime"/>
	<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_longAnimTime" />
</set>


其他的类似。。


你可能感兴趣的:(android)