【问题标题】:after implementing OnTouchEvent,the onClick is invalid实现OnTouchEvent后,onClick无效
【发布时间】:2012-08-28 03:52:57
【问题描述】:

现在我想实现一个自定义的imageview,它可以自动将背景变成灰色,但现在的问题是我的view无法响应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);
    }
}

【问题讨论】:

    标签: java android onclick touch invalidation


    【解决方案1】:

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

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

    【讨论】:

    • 我试试这个,但它仍然无法响应onclick事件,似乎我没有返回super.OnTouchEvent,我的视图无法响应onClick,但是invalidate()会无效。
    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 2015-10-13
    相关资源
    最近更新 更多