码迷,mamicode.com
首页 > 微信 > 详细

android 入门-防微信拍摄视频 按钮事件处理

时间:2015-06-09 21:20:53      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

package com.cc.view;

import com.cc.R;
import com.cc.R.layout;
import com.cc.R.menu;

import android.opengl.Visibility;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GestureDetectorAty extends Activity {

    private Button mButton;
    private GestureDetector mGestureDetector;

    private LinearLayout mLayout;
    private TextView mCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_gesture_detector);
        mLayout = (LinearLayout) findViewById(R.id.displayrecord);

        mCancel = (TextView) findViewById(R.id.tv_cancelrecord);

        mGestureDetector = new GestureDetector(this, new MyOnGestureListener(
                this));

        mButton = (Button) findViewById(R.id.btn_textgesture);
        mButton.setFocusableInTouchMode(true);
        mButton.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    break;
                }
                case MotionEvent.ACTION_MOVE: {
                    
                    final float p1x = event.getX();
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    mLayout.setVisibility(View.GONE);
                    mCancel.setVisibility(View.GONE);
                    break;
                }
                default:
                    break;
                }
                mGestureDetector.onTouchEvent(event);
                // 一定要返回true,不然获取不到完整的事件
                return true;
            }

        });

    }

    public class MyOnGestureListener extends SimpleOnGestureListener {
        private GestureDetectorAty mActivity;

        public MyOnGestureListener(Context context) {
            this.mActivity = (GestureDetectorAty) context;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub
            super.onLongPress(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            float p2y = e2.getY();
            float p1y = e1.getY();
            if (p1y - p2y > 50) {
                if (e2.getX() - e1.getX() > -50 | e2.getX() - e1.getX() < 50) {

                }
            }

            // TODO Auto-generated method stub
            return super.onFling(e1, e2, velocityX, velocityY);
        }

        // 按住 且为达到onLongPress之前 调用 一旦调用就不会有onSingleTapUp
        // 点击了触摸屏,但是没有移动和弹起的动作。onShowPress和onDown的区别在于
        // onDown是,一旦触摸屏按下,就马上产生onDown事件,但是onShowPress是onDown事件产生后,
        // 一段时间内,如果没有移动鼠标和弹起事件,就认为是onShowPress事件。
        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub
            super.onShowPress(e);
        
        }

        // 按下的时候触发
        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            mLayout.setVisibility(View.VISIBLE);
            return super.onDown(e);
        }

        // 离开屏幕的一刹那
        // 轻击触摸屏后,弹起。如果这个过程中产生了onLongPress、onScroll和onFling事件,就不会
        // 产生onSingleTapUp事件。
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            isScroll = true;
            mLayout.setVisibility(View.GONE);

            return super.onSingleTapUp(e);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            // TODO Auto-generated method stub
            return super.onSingleTapConfirmed(e);
        }

        private boolean isScroll = true;

        // 滚动事件,当在触摸屏上迅速的移动,会产生onScroll。由ACTION_MOVE产生
        // e1:第1个ACTION_DOWN MotionEvent
        // e2:最后一个ACTION_MOVE MotionEvent
        // distanceX:距离上次产生onScroll事件后,X抽移动的距离
        // distanceY:距离上次产生onScroll事件后,Y抽移动的距离
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            // TODO Auto-generated method stub
            if (e1.getY() - e2.getY() > 50) {
                if (e2.getX() - e1.getX() > -50 | e2.getX() - e1.getX() < 50) {
                    if (isScroll) {
                        isScroll = false;
                        mCancel.setVisibility(View.VISIBLE);
                        mLayout.setVisibility(View.GONE);
                        
                    }
                }
            } else if (e1.getY() - e2.getY() < 10) {
                if (e2.getX() - e1.getX() > -50 | e2.getX() - e1.getX() < 50) {
                    isScroll = true;
                    if (isScroll) {
                        mCancel.setVisibility(View.GONE);
                        mLayout.setVisibility(View.VISIBLE);
                    }
                }
            }
            return super.onScroll(e1, e2, distanceX, distanceY);
        }
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".GestureDetectorAty" >

    <Button
        android:id="@+id/btn_textgesture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:text="@string/app_name" />

    <LinearLayout
        android:id="@+id/displayrecord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_textgesture"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:orientation="horizontal"
        android:visibility="gone" >

        <ImageView
            android:id="@+id/arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon" />

        <TextView
            android:id="@+id/tv_displayrecord"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始录制"
            android:textColor="@color/green" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_cancelrecord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_textgesture"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="150dp"
        android:background="@color/red"
        android:visibility="gone"
        android:text="取消录制"
        android:textColor="@color/white" />

</RelativeLayout>

 技术分享

android 入门-防微信拍摄视频 按钮事件处理

标签:

原文地址:http://www.cnblogs.com/luquanmingren/p/4564254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!