Android手势无法正常使用

时间:2018-12-28 13:14:14

标签: android

  • 我在活动中有一些图像,当我吞咽图像时需要执行手势,所以下面我发布了我的代码(有完整的代码,但是手势不起作用)
  • 由于下面是我的代码(活动类),实现了手势
  • 我已经在代码中创建了手势,但是它不起作用,
  • 当我单击它不打印任何日志时,它也没有调用onTouch事件
  • 我该如何解决这个问题。
  • 最后有onFling个方法
public class PageView extends AppCompatActivity implements ReadOutTextAnimation.ReadingPageCompleted, OnGestureListener {

    public static final String TAG = PageView.class.getName();
    private Button mPlayBtn;
    private Button mBtnNext;
    private Button mBackBtn;
    private ImageView mBgImage;
    private Integer   mPageNumber;
    private String mMainFolder;
    private Handler handler = new Handler();
    private Runnable mTimerCallback;
    private boolean mIsPaused;
    private GestureDetector gestureDetector;
    private boolean isoneclick;
    private LinearLayout mLenearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_view);
        gestureDetector = new GestureDetector(this, this);

//         mLenearLayout = (LinearLayout)findViewById(R.id.clickhide);
//         mLenearLayout.setVisibility(View.INVISIBLE);
         isoneclick = true;
        if (AppManager.getInstance().getNeedsBGMusic())
            SoundManager.getInstance().resumeBackgroundMusic();
        mPageNumber = 1;
        mMainFolder = String.valueOf(BaseApplication.getInstance().getAppContext().getDir("Books", Context.MODE_PRIVATE));
        mMainFolder += "/Downloads" + AppManager.getInstance().getCurrentBook().getBookId().toString();
        mTimerCallback = new Runnable() {
            @Override
            public void run() { loadNextPage(); }
        };
        ReadOutTextAnimation.getInstance().setListner(this);

        mBgImage = findViewById(R.id.page_view_bgimage);
        mBgImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSlideViewButtonClick(v);
                //loadNextPage();
            }
        });


        //Next Button Action
        mBtnNext = findViewById(R.id.page_view_nextbtn);
        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadNextPage();
            }
        });

        //Back Button Action
        mBackBtn = findViewById(R.id.page_view_backbtn);
        mBackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadPreviousPage();
            }
        });

        //Play & Pause Button Action
        mPlayBtn = findViewById(R.id.page_view_palybtn);
        mPlayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mIsPaused){
                    resumeApp();
                } else {
                    pauseApp();
                }
            }
        });
        loadPageDetails();
        enableBtn();
        Button mDoneBtn = findViewById(R.id.page_view_done);
        mDoneBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }



    @Override
    protected void onPause() {
        super.onPause();
        pauseApp();
    }



    private void enableBtn(){
        if(mPageNumber > 1) {
            mBackBtn.setEnabled(true);
            mBtnNext.setEnabled(true);
            mBackBtn.setAlpha(.9f);
        }else {
            mBackBtn.setAlpha(.5f);
            mBtnNext.setEnabled(true);
        }
    }

    private void loadPageDetails(){
        ReadOutTextAnimation.getInstance().stopReadOut();

        PageDetailProperties mCurrentPage = AppManager.getInstance().getCurrentBook().getPageDetailsForNumber(mPageNumber);
        if (mCurrentPage.getPageAudioDurationJSON() == null)
        {
            loadNextPage();
            return;
        }
        File imgFile = new File(mMainFolder,ContentfulConstants.PAGE_IMAGE+ mPageNumber.toString() +".png");
        if (imgFile.exists()){
            Bitmap bookImage = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            mBgImage.setImageBitmap(bookImage);
        }

        File soundFile = new File(mMainFolder,ContentfulConstants.PAGE_AUDIO+mPageNumber.toString()+".mp3");
        if (soundFile.exists()){
            SoundManager.getInstance().playPageAudio(soundFile.getPath());
        }

        ReadOutTextAnimation.getInstance().startReadOut(mPageNumber);
        mIsPaused = false;
        mPlayBtn.setBackgroundResource(R.drawable.pause);
    }

    private void loadNextPage(){
        handler.removeCallbacks(mTimerCallback);
        mPageNumber++;
        if (mPageNumber > AppManager.getInstance().getCurrentBook().getPageDetail().size()){
            mPageNumber = AppManager.getInstance().getCurrentBook().getPageDetail().size();

            ReadOutTextAnimation.getInstance().setListner(null);
            ReadOutTextAnimation.getInstance().stopReadOut();
            SoundManager.getInstance().stopALL();
            Intent intent = new Intent(this , BookCompleteView.class);
            this.finish();
            startActivity(intent);
        } else {
            loadPageDetails();
            enableBtn();
        }
    }

    private void loadPreviousPage(){
        mPageNumber--;
        handler.removeCallbacks(mTimerCallback);
        if (mPageNumber < 1){
            mPageNumber = 1;
        } else {
            loadPageDetails();
        }
        enableBtn();
    }

    @Override
    public void readingPageCompleted() {
        handler.postDelayed(mTimerCallback, 700);
    }

    @Override
    public void onBackPressed() {
        ReadOutTextAnimation.getInstance().setListner(null);
        handler.removeCallbacks(mTimerCallback);
        ReadOutTextAnimation.getInstance().stopReadOut();
        SoundManager.getInstance().stopALL();
        super.onBackPressed();
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {


    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "touch" , Toast.LENGTH_SHORT).show();
         gestureDetector.onTouchEvent(motionEvent);
         return false;
    }

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        System.out.println("Gesture event touch ");
        if (motionEvent.getX() - motionEvent1.getX() > 50) {
            loadNextPage();
            return true;
        }
        if (motionEvent1.getX() - motionEvent.getX() > 50) {
            loadPreviousPage();
            return true;
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

如果我对您的理解正确。您必须为图像视图而不是活动设置触摸监听器。

mBgImage.setOnTouchListener(yourTouchListener);