簡體   English   中英

android ImageView的onTouch和onClick nevet被稱為?

[英]android ImageView's onTouch and onClick nevet get called?

為包含圖像的顏色選擇器創建一個自定義視圖,我將在觸摸bot上從該圖像獲取顏色,而不會調用onTouch(View v,MotionEvent ev)或onClick(View v)。

所以這是我的視圖類:

public class ChangeColor extends RelativeLayout {

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

    private void init(){
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.change_color, this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        ImageView iv = (ImageView)findViewById(R.id.colorScaleImageView);
        final Bitmap bitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();

        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("ImageView", "onClick();");
            }
        });
        iv.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent ev) {
                Log.d("ChangeColor", "onTouch();");
                switch (ev.getAction()){
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    int c = bitmap.getPixel((int)ev.getX(), (int)ev.getY());
                    SettingsManager.setColor(c);
                    Log.d("ChangeColor", "" + c);
                    return false;
                default:
                    return true;
                }
            }
        });
    }
}

和布局xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/colorScaleImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/seekBar1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/background_color" />

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_alignParentLeft="true"
    android:focusable="false"
    android:maxHeight="8dp"
    android:minHeight="8dp"
    android:paddingLeft="7dp"
    android:paddingRight="7dp"
    android:progressDrawable="@drawable/seekbar_progress"
    android:thumb="@drawable/seekbar_thumb_"
    android:thumbOffset="7dp" />

</RelativeLayout>

因此,LogCat中不會出現任何日志消息。

PS我在SO上沒有看到類似的問題,但它們並沒有幫助我。 PPS該視圖用在片段中,而片段用在活動中。 我使用OnClickListener在該活動中添加了全屏視圖,以查看單擊是否轉到父視圖,並且只有在圖像外部點擊才能看到從該背景視圖記錄的日志消息,而在圖像上單擊則看不到任何消息。

謝謝。

您已經擴展了相對布局,但是在此示例中未對其進行調用。

在XML中,您應該實現開發的類而不是RelativeLayout

嘗試

<?xml version="1.0" encoding="utf-8"?>
<**namespace**.ChangeColor xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/colorScaleImageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/seekBar1"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:clickable="true"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/background_color" />

</**namespace**.ChangeColor>

**編輯第一次是正確的。 要實現它,您必須將ChangeColor附加到另一個布局,在它被調用時試圖擴大的布局中不能調用它。 例如 ChangeColor使change_color.xml膨脹,您不能在ChangeColor.init()膨脹的布局中調用標記NAMESPACE.ChangeColor。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM