0

我在GitHub中创建了一个自定义的圆形图像按钮

private void onTouchCircle(float x, float y, boolean up) {
    float cx = getWidth()/2;
    float cy = getHeight()/2;
    float distance = (float) Math.sqrt(Math.pow((x - cx), 2) + Math.pow((y - cy), 2));
    float radius = getWidth()/2;
    if (distance < radius  && !up) {
        IS_PRESSED = true;
        getDrawable().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0X2E2E2E));
        invalidate();
    } else {
        IS_PRESSED = false;
        getDrawable().setColorFilter(null);

        if(distance<radius){
            BUTTON_PRESSED=true;
        }else{
            BUTTON_PRESSED=false;
        }
        invalidate();
    }

    if(up && BUTTON_PRESSED){
        BUTTON_PRESSED=false;
        Log.e(TAG, "distance -- "+distance+" -- radius --"+radius);
        if(onClickListener!=null){
            onClickListener.onCircularButtonClick(this);
        }
    }

}

如何通过像 android.widget.Button/android.widget.ImageButton 这样的 XML 调用单击?如果我可以为我的自定义控件调用类似于按钮/图像按钮的方法,如下所示,那就太好了。任何帮助表示赞赏。

<com.example.circularimageview.CircularImageView
        xmlns:imaginea="http://schemas.android.com/apk/res/com.example.circularimageview"
        android:id="@+id/imageView1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scaleType="centerCrop"
        android:src="@drawable/breakdancing_android"
        imaginea:alpha="1"
        imaginea:onCircularButtonClick="myFancyMethod" />

public void myFancyMethod(View v) {
    // does something very interesting
}
4

1 回答 1

1

看看 Android 在他们的基础View.java中是如何做到的

关键是:

final String handlerName = a.getString(attr);
if (handlerName != null) {
    setOnCircularClickListener(new OnCircularClickListener() {
        private Method mHandler;

        public void onCircularButtonClick(View v) {
            if (mHandler == null) {
                try {
                    mHandler = getContext().getClass().getMethod(handlerName,
                            View.class);
                } catch (NoSuchMethodException e) {
                    int id = getId();
                    String idText = id == NO_ID ? "" : " with id '"
                            + getContext().getResources().getResourceEntryName(
                                id) + "'";
                    throw new IllegalStateException("Could not find a method " +
                            handlerName + "(View) in the activity "
                            + getContext().getClass() + " for onClick handler"
                            + " on view " + CircularImageView.this.getClass() + idText, e);
                }
            }
            try {
                mHandler.invoke(getContext(), CircularImageView.this);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException("Could not execute non "
                        + "public method of the activity", e);
            } catch (InvocationTargetException e) {
                throw new IllegalStateException("Could not execute "
                        + "method of the activity", e);
            }
    });
}
于 2013-04-02T05:56:18.677 回答