首页 > 代码库 > gallery左右滑动时图片淡入淡出

gallery左右滑动时图片淡入淡出

前几天,公司项目有一个功能要做成滑动图片的淡入淡出,要一边滑动一边改变,所以ViewFlipper左右滑动效果就不能了。网上找了很久,也找不到资料,所以自己写了一个,通过滑动改变imageView的透明度。当按下图片时,先记下imageView的位置,图片滑动时,位置发生变化,就可以算出移动的距离,从而可以算出alpha的值。当图片向左滑动时,设置imageView的Alpha即imageView.setAlpha(255-alpha),设置下一个nextView的Alpha即nextView.setAlpha(alpha);当图片向右滑动时,设置imageView的Alpha即imageView.setAlpha(255-alpha),设置上一个lastView的Alpha即lastView.setAlpha(alpha);效果如图所示

      

废话就不多说了,上代码哈

package com.gallery.gradient;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class MyGallery extends Gallery {

    private ImageAdapter adapter;
    private int position ;
    private ImageView imageView;
    private int[] viewLocation;
    private ImageView nextView;
    private ImageView lastView;
    private int winWeight;
    private Context context;
    
    public MyGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public MyGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }

    public MyGallery(Context context, int[] residList) {
        super(context);
        this.context = context;
        adapter = new ImageAdapter(context, residList);
        setAdapter(adapter);
    }

    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            position = getSelectedItemPosition();
            imageView = (ImageView) findViewWithTag(position);
            viewLocation = new int[2];
            imageView.getLocationInWindow(viewLocation);
            WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(
                    Context.WINDOW_SERVICE);
            winWeight = wm.getDefaultDisplay().getWidth();
            break;
        case MotionEvent.ACTION_MOVE:
            
            break;
        case MotionEvent.ACTION_UP:
//            int position = getSelectedItemPosition();
//            ImageView imageView = (ImageView) findViewWithTag(position);
//            if (imageView != null) {
//                System.out.println("imageView");
//                imageView.setAlpha(255);
//                int[] location = new int[2];
//                imageView.getLocationInWindow(location);
//            }
            break;

        default:
            break;
        }
        
        return super.onTouchEvent(event);
    }
    
//    @Override
//    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
//            float distanceY) {
//        if (e1.getX() > e2.getX()) {
//            lastView = (ImageView) findViewWithTag(position+1);
//        }else {
//            nextView = (ImageView) findViewWithTag(position-1);
//        }
//        
//        
//        return super.onScroll(e1, e2, distanceX, distanceY);
//    }
    
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        int[] location = new int[2];
        imageView.getLocationInWindow(location);
        
        if (location[0] != 0) {
            if (location[0] > viewLocation[0]) {
                int alpha = location[0] - viewLocation[0];
                alpha = alpha * 255 / winWeight;
                imageView.setAlpha(255-alpha);
                lastView = (ImageView) findViewWithTag(position-1);
                if (lastView != null) {
                    lastView.setAlpha((alpha));
                }
            }else {
                int alpha = viewLocation[0] - location[0];
                alpha = alpha * 255 / winWeight;
                imageView.setAlpha(255-alpha);
                nextView = (ImageView) findViewWithTag(position+1);
                if (nextView != null) {
                    nextView.setAlpha(alpha);
                }
            }
        }
        if (location[0] == 0) {
            if (lastView != null) {
                lastView.setAlpha((255));
            }
            if (nextView != null) {
                nextView.setAlpha(255);
            }
        }
    }
    
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

            float velocityY) {
        int kEvent;
        if(isScrollingLeft(e1, e2)){ //Check if scrolling left
          kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
        }
        else{ //Otherwise scrolling right
          kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
        }
        onKeyDown(kEvent, null);
        return true; 
    }
    
     private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
            return e2.getX() > e1.getX();
        }
     
     public class ImageAdapter extends BaseAdapter {
            private Context mcontext;
            private int[] residList;

            public ImageAdapter(Context context, int[] residList) {
                this.residList = residList;
                mcontext = context;
            }

            @Override
            public int getCount() {
                return residList.length;
            }

            @Override
            public Object getItem(int position) {
                return residList[position];
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            public int getResId(int position) {
                return residList[position];
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView img;
                if (convertView == null) {
                    img = new ImageView(mcontext);
                    img.setScaleType(ImageView.ScaleType.FIT_XY);
                    img.setLayoutParams(new Gallery.LayoutParams(-1, -1));
                    
                } else {
                    img = (ImageView) convertView;
                }
                img.setTag(position);
                img.setImageResource(residList[position]);
                return img;
            }
            
     }
}

代码下载链接 http://pan.baidu.com/s/1hqxaYTu