自定义视图不响应触摸 [英] Custom View not responding to touches

查看:130
本文介绍了自定义视图不响应触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了应该改变的自定义视图它的背景图像时pressed,突出或禁用。该应用程序运行,但按钮不会改变它的背景。

I've created a custom view which should change it's background image when pressed, highlighted or disabled. The app runs but the button doesn't change it's background.

这是我的code:

public class CustomImageButton extends View {

public CustomImageButton(Context context) {
super(context);
setFocusable(true);
setClickable(true);
}

public CustomImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
setClickable(true);
}
public CustomImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
setClickable(true);
}

protected Drawable background = super.getBackground();


@Override
public void setBackgroundDrawable(Drawable d) {
// Replace the original background drawable (e.g. image) with a LayerDrawable that
// contains the original drawable slightly edited.

CustomImageButtonBackgroundDrawable layer = new CustomImageButtonBackgroundDrawable(d);
super.setBackgroundDrawable(layer);
}

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 int drawableWidth = super.getBackground().getMinimumWidth();
 int drawableHeight = super.getBackground().getMinimumHeight();
 setMeasuredDimension(drawableWidth, drawableHeight);
}

protected class CustomImageButtonBackgroundDrawable extends LayerDrawable { 

    protected Drawable lowerlayer;
    protected Drawable _highlightedDrawable;

    protected int _disabledAlpha = 100;
    protected Drawable _pressedDrawable;


    public CustomImageButtonBackgroundDrawable(Drawable d) {
          super(new Drawable[] { d });
    }

    @Override
    protected boolean onStateChange(int[] states) {
      boolean enabled = false;
      boolean highlighted = false;
      boolean pressed = false;

      for (int state : states) {
        if (state == android.R.attr.state_enabled)
            enabled = true;
        else if (state == android.R.attr.state_selected)
            highlighted = true;
        else if (state == android.R.attr.state_pressed)
            pressed = true;
      }

      mutate();
      if (enabled && highlighted) {
        ColorFilter colourFilter = new LightingColorFilter(Color.YELLOW, 1);
        ScaleDrawable resizedImage = new ScaleDrawable(background, 0, 1.25f, 1.25f);

        lowerlayer = resizedImage.getDrawable();
        lowerlayer.setColorFilter(colourFilter);

        Drawable[] aD = new Drawable[2];
        aD[0] = lowerlayer;
        aD[1] = background;
        LayerDrawable _highlightedDrawable = new LayerDrawable(aD);

        setBackgroundDrawable(_highlightedDrawable); // buttons need transparent backgrounds

      } else if (!enabled) {
        setColorFilter(null);
        setAlpha(_disabledAlpha);

      } else if (enabled && pressed){
        ScaleDrawable smaller = new ScaleDrawable(background, 0, 0.75f, 0.75f);

        setBackgroundDrawable(smaller.getDrawable());

      } else if(enabled){
        setBackgroundDrawable(background);  
        setColorFilter(null);
      }

      invalidateSelf();

      return super.onStateChange(states);
    }

}

}

下面是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">

<ImageButton
    android:id="@+id/title"
    android:layout_width="250dp"
    android:layout_height="58dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_margin ="25dp"
    android:background="@drawable/skintonetitle" />

<custombuttons.CustomImageButton
    android:id="@+id/skina1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/title"
    android:layout_below="@+id/title"
    android:layout_marginTop="35dp"
    android:background="@drawable/test_circle"
    android:clickable="true"
    android:focusable="true" />

</RelativeLayout>

件事情我已经错过了吗?

Something I've missed?

推荐答案

从视图,没有按钮扩展,所以它不是默认点击或可成为焦点。与调整

It extends from View, not button, so it's not clickable or focusable by default. Adjust with

android:clickable="true"
android:focusable="true"

在你的XML。

编辑:

您还可以,如果你想这样做在java中设置这些在你的视图类的构造函数:

You can also set these in the constructor of your View class if you want to do it in java:

setFocusable(true);
setClickable(true);

这篇关于自定义视图不响应触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆