单击时使gridView中的图像更暗,同时它将打开另一个活动

时间:2014-09-09 08:45:21

标签: android gridview

我想将一些imageView添加到gridView中,当我单击imageView时,它将跳转到一个大图片查看器活动。当我的手指触摸imageView时,视图会变暗。

这是我实现它的方法。

gridView.setAdapter(new ImageAdapter());

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                JumpToImageViewer(imageUrlsFinal, position);
            }
        });

在ImageAdapter中,我在ImageView上设置了setOnTouchListener。并重写OnTouch方法。 当我得到ACTION_DOWN时,我使imageView变暗,当得到ACTION_UP时,使它正常。然后返回false。

有问题: 1,当我在onTouch方法中返回false时,然后我单击了imageView,并且JumpToImageViewer执行得很好,但是imageView只是变暗了,但是无法返回。

2,当我在OnTouch中使用return true时,imageView可以成功更改颜色。但是不会运行JumpToImageViewer。

而且我知道,也许使用return false会使监听器从imageview更改为其父级gridview,这就是为什么它没有效果。

但我无法找到解决这种情况的解决方案。我如何才能意识到单击GridView中的imageView将跳转到另一个Activity。同时imageView将改变其颜色。

非常感谢。

1 个答案:

答案 0 :(得分:0)

这是我的ImageAdaper。

public class ImageAdapter extends BaseAdapter {
private Activity mActivity;
private List<String> mImageUrls;
private int mImageWidth;

public ImageAdapter(Activity activity, List<String> imageUrls, int imageWidth) {
    this.mActivity = activity;
    this.mImageUrls = imageUrls;
    this.mImageWidth = imageWidth;
}

@Override
public int getCount() {
    return mImageUrls.size();
}

@Override
public Object getItem(int i) {
    return mImageUrls.get(i);
}

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

@Override
public View getView(int pos, View view, ViewGroup viewGroup) {
    final ImageView imageView;
    if (null == view) {
        imageView = new ImageView(mActivity);
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setLayoutParams(new GridView.LayoutParams(mImageWidth, mImageWidth));
    } else {
        imageView = (ImageView)view;
    }

    String imageUrl = mImageUrls.get(pos);

    ImageCacheManager.getInstance().getImageLoader().get(imageUrl, ImageLoader.getImageListener(imageView, R.drawable.logologo, R.drawable.logologo));

    AnimationUtils.addTouchColorChange(imageView, true);

    return imageView;
}
}



public class AnimationUtils {
/**
 * 给试图添加点击效果,让背景变深 
 * */
public static void addTouchColorChange(View view, boolean isClick){
    view.setOnTouchListener( VIEW_TOUCH_DARK ) ;

    if(!isClick){
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }
}

/**
 * 给试图添加点击效果,让背景变淡
 * */
public static void addTouchLight(View view , boolean isClick){
    view.setOnTouchListener( VIEW_TOUCH_LIGHT ) ;

    if(!isClick){
        view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            }
        });
    }
}


/**
 * 让控件点击时,颜色变深 
 * */
public static final OnTouchListener VIEW_TOUCH_DARK = new OnTouchListener() {
    /*设定透明值,越小透明度越大*/
    public int transparent_level = 150;

    public final float[] DARKER = new float[] { 1, 0, 0, 0, -50, 0, 1,
        0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 };
    public final float[] RESTORE = new float[] { 1, 0, 0, 0, 0, 0,
        1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };
    public final float[] TRANSPARENT = new float[] { 1, 0, 0, 0, 0,
                                                     0, 1, 0, 0, 0,
                                                     0, 0, 1, 0, 0,
                                                     0, 0, 0, 1, transparent_level - 255
    };
    int r, g, b;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if(v instanceof ImageView){
                ImageView iv = (ImageView) v;
                iv.setColorFilter(new ColorMatrixColorFilter(DARKER)) ;
            } else if (v instanceof TextView){
                TextView tv = (TextView) v;
                String tvColor = Integer.toHexString(tv.getCurrentTextColor());
                b = Integer.parseInt(tvColor.substring(6, 8), 16);
                g = Integer.parseInt(tvColor.substring(4, 6), 16);
                r = Integer.parseInt(tvColor.substring(2, 4), 16);
                tv.setTextColor(Color.argb(transparent_level, r, g, b));
                Drawable[] drawables = tv.getCompoundDrawables();
                if(null != drawables[0]) {
                    drawables[0].setColorFilter(new ColorMatrixColorFilter(TRANSPARENT));
                }
            } else{
                v.getBackground().setColorFilter( new ColorMatrixColorFilter(DARKER) );
                v.setBackgroundDrawable(v.getBackground());
            }
            break;
        case MotionEvent.ACTION_UP:
            if(v instanceof ImageView){
                ImageView iv = (ImageView) v;
                iv.setColorFilter( new ColorMatrixColorFilter(RESTORE) ) ;
            } else if (v instanceof TextView){
                TextView tv = (TextView) v;
                tv.setTextColor(Color.argb(255, r, g, b));
                Drawable[] drawables = tv.getCompoundDrawables();
                if(null != drawables[0]) {
                    drawables[0].setColorFilter(new ColorMatrixColorFilter(RESTORE));
                }
            } else{
                v.getBackground().setColorFilter(
                    new ColorMatrixColorFilter(RESTORE));
                v.setBackgroundDrawable(v.getBackground());
            }
            break;
        default:
            if(v instanceof ImageView){
                ImageView iv = (ImageView) v;
                iv.setColorFilter( new ColorMatrixColorFilter(RESTORE)) ;
            }
            break;
        }
        return false;
    }
};
}