ImageView上的OnTouchListener无法正常工作?

时间:2016-03-01 15:36:20

标签: android imageview android-animation android-progressbar

在我的应用程序中我有一个图像视图(它是一个圆形,它被一个进度条包围 - 圆形,再次)。我正在附加一个onTouchListener因为我需要检查用户何时触摸图像视图以启动进度条动画,当用户的手指释放图像视图时我需要取消进度条动画。但是,出于某种原因,只执行action_down而且我不知道原因。

enter image description here

这是我的布局:

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

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cantDecideContainer"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp">

        <ImageView
            android:id="@+id/grey_circle_bg"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:src="@drawable/perfect_grey_circle"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"/>

        <ImageView
            android:id="@+id/cant_devide_black_circle_bg"
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/perfect_black_circle"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"/>

        <com.studentsins.lust.UI.CircleProgressBar
            android:id="@+id/cantDecideProgressBar"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:progress="0"
            app:progressBarThickness="3dp"
            android:layout_alignTop="@+id/grey_circle_bg"
            android:layout_alignLeft="@+id/grey_circle_bg"
            android:layout_alignStart="@+id/grey_circle_bg"
            android:layout_alignBottom="@+id/grey_circle_bg"
            android:layout_alignRight="@+id/grey_circle_bg"
            android:layout_alignEnd="@+id/grey_circle_bg"/>

    </RelativeLayout>

这是我的代码:

 private Animator.AnimatorListener progressBarAnimationListener =  new  Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animator) {
        mCantDecideProgressBar.setProgress(0);
    }
    @Override
    public void onAnimationEnd(Animator animator) {
        // Log.d(TAG, "onAnimationEnd");
        mCantDecideProgressBar.setProgress(100);
        //mNumSins.setText(numberOfSins+"");
    }
    @Override
    public void onAnimationCancel(Animator animator) {}
    @Override
    public void onAnimationRepeat(Animator animator) {}
};
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            cantDecideProgressAnimator.cancel();
            Log.d(TAG, "ACTION_UP canceled");
        }

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            cantDecideProgressAnimator.start();
            Log.d(TAG, "ACTION_DOWN executed");
        }
        return false;
    }
};


mCantDecideGg = (ImageView)view.findViewById(R.id.cant_devide_black_circle_bg);

    mCantDecideGg.setOnTouchListener(onTouchListener);
    mCantDecideProgressBar = (CircleProgressBar)view.findViewById(R.id.cantDecideProgressBar);
    //set up the project animator to animate the progress bar from 0 to 100
    cantDecideProgressAnimator = ObjectAnimator.ofFloat(mCantDecideProgressBar, "progress", 0.0f, 100.0f);
    //add the animation listener to the progress animator to check when the progress has started,finished...
    cantDecideProgressAnimator.addListener(progressBarAnimationListener);
    //set the duration of the animation to 1.2 seconds
    cantDecideProgressAnimator.setDuration(1200);

日志:

03-01 15:28:36.081 24093-24119/com.studentsins.lust I/OpenGLRenderer: Initialized EGL, version 1.4
03-01 15:28:38.201 24093-24093/com.studentsins.lust D/MainActivity: ACTION_DOWN executed
03-01 15:28:45.822 24093-24093/com.studentsins.lust D/MainActivity: ACTION_DOWN executed
03-01 15:29:04.651 24093-24093/com.studentsins.lust D/MainActivity: ACTION_DOWN executed

2 个答案:

答案 0 :(得分:3)

根据这个答案: In onTouchEvent, ACTION_UP doesn't work 您似乎应该在触摸事件中返回true

答案 1 :(得分:3)

private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        cantDecideProgressAnimator.cancel();
        Log.d(TAG, "ACTION_UP canceled");
        return true;
    }

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        cantDecideProgressAnimator.start();
        Log.d(TAG, "ACTION_DOWN executed");
        return true;
    }
    return false;
}

};