在android中检测手指离开图像按钮

时间:2013-12-04 06:51:30

标签: android

我有一个图像按钮,我试图在触摸按钮时录制音频&当用户离开按钮时我停止录音。我成功了。当用户开始在图像按钮的范围内移动手指时我有问题。我在ACTION_MOVE上使用它来处理这个问题,我有时没有取得100%的成功它会在休假时检测到,有时不会。请帮忙。以下是我正在使用的代码..

    @Override
public boolean onTouch(View v, MotionEvent event) {

    switch (v.getId()) {
        case R.id.btn_audio :

            final int action = event.getAction();

            // get the View's Rect relative to its parent
            v.getHitRect(rect);
            // offset the touch coordinates with the values from rect to obtain meaningful coordinates
            final float x = event.getX() + rect.left;
            final float y = event.getY() + rect.top;
            switch (action) {
                case MotionEvent.ACTION_DOWN :

                    audioBtn.setImageDrawable(getResources().getDrawable(R.drawable.img_audio_pressed));

                    mHandler.sendEmptyMessage(MSG_START_TIMER);
                    timer.setVisibility(View.VISIBLE);
                    playBtn.setVisibility(View.GONE);
                    break;
                case MotionEvent.ACTION_MOVE :

                    Log.e(TAG, rect.top+","+rect.bottom+","+rect.left+","+rect.right);
                    Log.e(TAG, (int)x+","+(int)y);

                    if (!rect.contains((int) x, (int) y)) {
                        Log.e(TAG, "In action move,outside");
                        audioBtn.setImageDrawable(getResources().getDrawable(R.drawable.img_audio));
                        playBtn.setVisibility(View.VISIBLE);
                        if (stop)
                            mHandler.sendEmptyMessage(MSG_STOP_TIMER);
                    } else {

                        Log.e(TAG, "In action move,inside");
                        audioBtn.setImageDrawable(getResources().getDrawable(R.drawable.img_audio_pressed));

                        timer.setVisibility(View.VISIBLE);
                        playBtn.setVisibility(View.GONE);
                    }
                    break;
                case MotionEvent.ACTION_UP :
                    // the user raised his finger, cancel the Runnable
                    audioBtn.setImageDrawable(getResources().getDrawable(R.drawable.img_audio));
                    playBtn.setVisibility(View.VISIBLE);
                    if (stop)
                        mHandler.sendEmptyMessage(MSG_STOP_TIMER);
                    break;
            }

            break;

        default :

            break;
    }

    return false;
}

XML ::

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/actvity_background"
android:padding="15dp" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/record_review"
        style="@style/textview.review"
        android:layout_below="@id/rating_app"
        android:layout_marginTop="20dp"
        android:text="Record a review" />

    <LinearLayout
        android:id="@+id/record"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/record_review"
        android:orientation="horizontal"
        android:splitMotionEvents="false"
        android:weightSum="2" >

        <RelativeLayout
            android:id="@+id/audio_record"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <ImageButton
                android:id="@+id/btn_audio"
                style="@style/imagebutton"
                android:layout_centerInParent="true"
                android:layout_marginTop="20dp"
                android:background="@android:color/transparent"
                android:contentDescription="@string/desc_provider_thumb"
                android:focusableInTouchMode="true"
                android:src="@drawable/img_audio" />

            <TextView
                android:id="@+id/timer"
                style="@style/textview.review"
                android:layout_below="@id/btn_audio"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:text="00:30"
                android:visibility="gone" />

            <ImageButton
                android:id="@+id/btn_play"
                style="@style/imagebutton"
                android:layout_below="@id/btn_audio"
                android:layout_marginLeft="6dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/timer"
                android:background="@android:color/transparent"
                android:contentDescription="@string/desc_provider_thumb"
                android:src="@drawable/img_play"
                android:visibility="gone" />

            <ImageButton
                android:id="@+id/btn_pause"
                style="@style/imagebutton"
                android:layout_below="@id/btn_audio"
                android:layout_marginLeft="6dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/timer"
                android:background="@android:color/transparent"
                android:contentDescription="@string/desc_provider_thumb"
                android:src="@drawable/img_pause"
                android:visibility="gone" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/video_record"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <ImageButton
                android:id="@+id/btn_video"
                style="@style/imagebutton"
                android:layout_centerInParent="true"
                android:layout_marginTop="20dp"
                android:background="@android:color/transparent"
                android:contentDescription="@string/desc_provider_thumb"
                android:src="@drawable/img_video" />

            <RelativeLayout
                android:id="@+id/video_preview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" >

                <ImageView
                    android:id="@+id/thumbnail"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:src="@drawable/img_dumy_home_video_thumbnail" />

                <ImageButton
                    android:id="@+id/play_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@drawable/img_play" />
            </RelativeLayout>

            <Button
                android:id="@+id/btn_rerecord"
                style="@style/button.review"
                android:layout_below="@id/video_preview"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="2dp"
                android:text="@string/re_record"
                android:visibility="gone" />
        </RelativeLayout>
    </LinearLayout>

    <Button
        android:id="@+id/btn_submit_review"
        style="@style/button.review"
        android:layout_below="@id/record"
        android:text="Submit review" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

return true

而不是

return false
你的onTouch方法中的

答案 1 :(得分:0)

而不是v.getHitRect(rect);使用getLocationInWindow()返回屏幕坐标而不是相对于父坐标的坐标。更新您的onTouch代码,如下所示:

//same here
...
...    
int[] location = new int[2];
v.getLocationInWindow(location); // get position on the screen.

rect  = new Rect(location[0], location[1], location[0]+v.getWidth(), location[1]+v.getHeight());

final float x = event.getX(); // screen x position
final float y = event.getY(); // screen y position
...
...
//same here