0

现在我想实现一个自定义图像视图,它可以自动将背景变为灰色,但现在的问题是我的视图无法响应 onclick 事件,如果我返回 super.OnTouchEvent.invalidate() 将不起作用,所以效果无效,我现在不知道怎么办。

package com.test;

import android.content.Context;
import android.graphics.ColorMatrixColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * the class for the ImageView to automatic turn background to gray
 * 
 * @author jinningwei
 * 
 */
public class AutoGrayImageView extends ImageView {

    public AutoGrayImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private boolean canAutoGray = true;

    /**
     * set the function of autogray whether enabled
     * 
     * @param enabled
     */
    public void enableAutoGray(boolean enabled) {
        canAutoGray = enabled;
    }

    private float grayscale = 0.2f;

    /**
     * set gray degree
     * 
     * @param grayscale
     */
    public void setGrayscale(float grayscale) {
        if (grayscale > 0.5f) {
            grayscale = 0.5f;
            return;
        }
        if (grayscale < 0.0f) {
            grayscale = 0.0f;
            return;
        }
        this.grayscale = grayscale;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (canAutoGray) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float[] gray_matrix = { grayscale, grayscale, grayscale, 0.0f, 0.0f, // red
                        grayscale, grayscale, grayscale, 0.0f, 0.0f, // green
                        grayscale, grayscale, grayscale, 0.0f, 0.0f, // blue
                        0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // alpha
                };
                this.getBackground().setColorFilter(new ColorMatrixColorFilter(gray_matrix));
                invalidate();
                return true;
            case MotionEvent.ACTION_UP:
                float[] reset_matrix = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // red
                        0.0f, 1.0f, 0.0f, 0.0f, 0.0f,// green
                        0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // blue
                        0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // alpha
                };
                this.getBackground().setColorFilter(new ColorMatrixColorFilter(reset_matrix));
                invalidate();
                return false;
            }
        }
        return super.onTouchEvent(event);
    }
}

4

1 回答 1

0

我认为,如果您要覆盖该方法,那么您的代码应该是这样的

public boolean onTouchEvent(MotionEvent event) {
     super.onTouchEvent(event);
    if (canAutoGray) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            float[] gray_matrix = { grayscale, grayscale, grayscale, 0.0f, 0.0f, // red
                    grayscale, grayscale, grayscale, 0.0f, 0.0f, // green
                    grayscale, grayscale, grayscale, 0.0f, 0.0f, // blue
                    0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // alpha
            };
            this.getBackground().setColorFilter(new ColorMatrixColorFilter(gray_matrix));
            invalidate();
            return true;
        case MotionEvent.ACTION_UP:
            float[] reset_matrix = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // red
                    0.0f, 1.0f, 0.0f, 0.0f, 0.0f,// green
                    0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // blue
                    0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // alpha
            };
            this.getBackground().setColorFilter(new ColorMatrixColorFilter(reset_matrix));
            invalidate();
            return false;
        }
    }
    return true;
}
于 2012-08-28T04:01:30.690 回答