0

I need to make an ImageView which will simulate 3D rotation when swiping left to right, so i use this layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:padding="@dimen/global_default_padding"
    android:background="@color/main_background"
    android:id="@+id/rotateImage_background">

        <Button         android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="BACK"
                        android:id="@+id/rotateImage_button_back"/>

        <ImageView      android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"

                        android:id="@+id/rotateImage_image_rotator"
                        android:src="@drawable/hardware_rotate"
                        />

</LinearLayout>

and this is my code:

@Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_MOVE){

            if(event.getHistorySize() == 0){

            }
            else{
                if(mPreRotationCounter < 10){
                    mPreRotationCounter++;
                }
                else{
                    mPreRotationCounter = 0;
                    Log.e("Rotate", "Should rotate, lol");

                    if(event.getX() > event.getHistoricalX(event.getHistorySize() -1)){
                        updateRotation(true);
                    }
                    else{
                        updateRotation(false);
                    }
                }
            }
        }
        else if(event.getAction() == MotionEvent.ACTION_DOWN){

        }
        else if(event.getAction() == MotionEvent.ACTION_DOWN){

        }

        return true;
    }

private void updateRotation(boolean direction){
        if(direction){
            if(mCurrentImageLevel < 7){
                mCurrentImageLevel++;
            }
            else{
                mCurrentImageLevel = 0;
            }
        }
        else{
            if(mCurrentImageLevel > 0){
                mCurrentImageLevel--;
            }
            else{
                mCurrentImageLevel = 7;
            }
        }
        mRotator.setImageLevel(mCurrentImageLevel);

    }

but when i try it, i get a ton of "reacting on signal 3" in my logCat, and it isnt as sensitive as it should be, but when i debug, then it works fine. What is funny too is that there is no problem when i test it on android 2.1, but when i test it on 4.0.3 or higher, then i get this problem.

4

1 回答 1

0

当等于 0 时,您什么也不做。event.getHistorySize()由于您使用的是 -1 索引,因此当event.getHistorySize()为 0 时会出现错误。试试这个:

if(event.getHistorySize() == 0) {
    return true;            
}
于 2013-08-08T15:30:54.810 回答