手势控制音量、亮度

来源:互联网 发布:淘宝付费推广方式 编辑:程序博客网 时间:2024/05/19 22:50
/** * Created by hghl on 2017/8/23. */public class GestureActivity extends Activity implements OnTouchListener {    private Button mButton = null;    private int systemBrightness;    private AudioManager audioManager = null;    GestureDetector mGesture = null;    private boolean moveLight = false;    private float lightChange = 0f;    private boolean moveVoice = false;    private float voiceChange = 0;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        audioManager =(AudioManager) getSystemService(Service.AUDIO_SERVICE);        mButton = (Button)findViewById(R.id.button1);        mButton.setOnTouchListener(this);        try {            systemBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);            lightChange = systemBrightness;        } catch (Settings.SettingNotFoundException e) {            e.printStackTrace();        }    }    @Override    public boolean onTouch(View v, MotionEvent event) {        // TODO Auto-generated method stub        if (mGesture == null) {            mGesture = new GestureDetector(this, new GestureListener());        }        if (event.getAction() == MotionEvent.ACTION_UP) {            voiceChange = 0;        }        return mGesture.onTouchEvent(event);    }//    private int verticalMinDistance = 20;//    private int minVelocity = 0;    class GestureListener extends SimpleOnGestureListener {        @Override        public boolean onDoubleTap(MotionEvent e) {            // TODO Auto-generated method stub            Log.i("TEST===========", "onDoubleTap");            return super.onDoubleTap(e);        }        @Override        public boolean onDown(MotionEvent e) {            int viewWidth4 =  Math.round(mButton.getWidth() / 3.0f);            moveLight = false;            moveVoice = false;            if (viewWidth4 > e.getX()) {        //调节亮度                moveLight = true;            } else if ((viewWidth4 * 2) < e.getX()) {       //调节音量                moveVoice = true;            }            // TODO Auto-generated method stub            Log.i("TEST===========", "onDown");            return super.onDown(e);        }        @Override        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {            // TODO Auto-generated method stub//            if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {//                Toast.makeText(GestureActivity.this, "向左手势", Toast.LENGTH_SHORT).show();//            } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {//                Toast.makeText(GestureActivity.this, "向右手势", Toast.LENGTH_SHORT).show();//            }            return super.onFling(e1, e2, velocityX, velocityY);        }        @Override        public void onLongPress(MotionEvent e) {            // TODO Auto-generated method stub            Log.i("TEST===========", "onLongPress");            super.onLongPress(e);        }        @Override        public boolean onScroll(MotionEvent e1, MotionEvent e2,                                float distanceX, float distanceY) {            // TODO Auto-generated method stub            Log.i("TEST===========", "onScroll:distanceX = " + distanceX + " distanceY = " + distanceY);            if(moveLight) {                changeLight(distanceY);            }            if (moveVoice) {                changeVoice((Math.round(distanceY)));            }            return super.onScroll(e1, e2, distanceX, distanceY);        }        @Override        public boolean onSingleTapUp(MotionEvent e) {            // TODO Auto-generated method stub            Log.i("TEST===========", "onSingleTapUp");            return super.onSingleTapUp(e);        }        @Override        public boolean onDoubleTapEvent(MotionEvent e) {            Log.i("TEST===========", "onDoubleTapEvent");            return super.onDoubleTapEvent(e);        }    }    private void changeVoice(int distanceY){        voiceChange = voiceChange + distanceY;        float baseNumber = mButton.getHeight() / 24.0f;        if (voiceChange >= baseNumber) {            voiceChange = 0;            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);        } else if ((0 - voiceChange) >= baseNumber) {            voiceChange = 0;            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);        }    }    private void changeLight(float distanceY){        int height = mButton.getHeight();        lightChange = lightChange +  distanceY / height * 255f;        if (lightChange > 255) {            lightChange = 255f;        } else if (lightChange < 0) {            lightChange = 0;        }        if(moveLight) {            WindowManager.LayoutParams lp = getWindow().getAttributes();            lp.screenBrightness = Float.valueOf(lightChange) * (1f / 255f);            getWindow().setAttributes(lp);        }    }}
阅读全文
0 0
原创粉丝点击