android平台的视频播放器中需要的屏幕滑动源代码.

在android平台中, 大部分视频播放器的亮度调节, 音量调节,前进/后退几秒,是通过滑动屏幕的来实现的.


一般的操作行为是:
              上下滑动屏幕的左半侧是亮度调节,
              上下滑动屏幕的右半侧是音量调节, 
              横向滑动屏幕则是前进/后退几秒.
              
              这样的实现其实就是在ontouchevent中计算当前屏幕,然后得到相应的结果,原理不是很复杂,但对于写代码程序员来说, 效率是第一位的. 
              
              一下是基于我们的LanSoSdk开发的视频播放器关于计算屏幕源代码实现, 增加上注释,贴出来,供大家使用.
              您可以直接粘贴到您的代码中,并增加相关的函数,即可快速实现功能, 提高您的代码编写效率.
              
              

  代码大概流程, Override Activity的 onTouchEvent方法, 在其中判断touch事件, 计算后, 调用seek/ brightness/ volume等函数.


@Override
    public boolean onTouchEvent(MotionEvent event) {
        DisplayMetrics screen = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(screen);

        if (mSurfaceYDisplayRange == 0)
            mSurfaceYDisplayRange = Math.min(screen.widthPixels, screen.heightPixels);

        float x_changed, y_changed;
        if (mTouchX != -1f && mTouchY != -1f) {
            y_changed = event.getRawY() - mTouchY;
            x_changed = event.getRawX() - mTouchX;
        } else {
            x_changed = 0f;
            y_changed = 0f;
        }


        // coef is the gradient's move to determine a neutral zone
        float coef = Math.abs (y_changed / x_changed);
        float xgesturesize = ((x_changed / screen.xdpi) * 2.54f);
        float delta_y = Math.max(1f,((mInitTouchY - event.getRawY()) / screen.xdpi + 0.5f)*2f);

        switch (event.getAction()) 
        {

        case MotionEvent.ACTION_DOWN:
            // Audio
            mTouchY = mInitTouchY = event.getRawY();
            mVol = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            mTouchAction = TOUCH_NONE;
            // Seek
            mTouchX = event.getRawX();
            break;

        case MotionEvent.ACTION_MOVE:
            // No volume/brightness action if coef < 2 or a secondary display is connected
            if (mTouchAction != TOUCH_SEEK && coef > 2) 
            {
                if (Math.abs(y_changed/mSurfaceYDisplayRange) < 0.05)
                    return false;
                mTouchY = event.getRawY();
                mTouchX = event.getRawX();
                // Volume (Up or Down - Right side)
                if ( (int)mTouchX > (3 * screen.widthPixels / 5)){
                    doVolumeTouch(y_changed);
                }
                // Brightness (Up or Down - Left side)
                if ( (int)mTouchX < (2 * screen.widthPixels / 5)){
                    doBrightnessTouch(y_changed);
                }
            } else {
                // Seek (Right or Left move)
                doSeekTouch(Math.round(delta_y), xgesturesize, false);
            }
            break;

        case MotionEvent.ACTION_UP:
            // Seek
            if (mTouchAction == TOUCH_SEEK)
                doSeekTouch(Math.round(delta_y), xgesturesize, true);
            	mTouchX = -1f;
            	mTouchY = -1f;
            	break;
        }
        return mTouchAction != TOUCH_NONE;
    }


 @TargetApi(android.os.Build.VERSION_CODES.FROYO)
     private void initBrightnessTouch() {
         float brightnesstemp = 0.6f;
         // Initialize the layoutParams screen brightness
       boolean isFroyoOrLater=  android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO;
         
         try {
             if (isFroyoOrLater &&
                     Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
                 Settings.System.putInt(getContentResolver(),
                         Settings.System.SCREEN_BRIGHTNESS_MODE,
                         Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
                 mRestoreAutoBrightness = android.provider.Settings.System.getInt(getContentResolver(),
                         android.provider.Settings.System.SCREEN_BRIGHTNESS) / 255.0f;
             } else {
                 brightnesstemp = android.provider.Settings.System.getInt(getContentResolver(),
                     android.provider.Settings.System.SCREEN_BRIGHTNESS) / 255.0f;
             }
         } catch (SettingNotFoundException e) {
             e.printStackTrace();
         }
         WindowManager.LayoutParams lp = getWindow().getAttributes();
         lp.screenBrightness = brightnesstemp;
         getWindow().setAttributes(lp);
         mIsFirstBrightnessGesture = false;
     }
    /**
     * 恢复屏幕的亮度.
     * LanSoSdk  提供android/ios多媒体编解码核心业务
     */
    @TargetApi(android.os.Build.VERSION_CODES.FROYO)
    private void restoreBrightness() {
        if (mRestoreAutoBrightness != -1f) {
            int brightness = (int) (mRestoreAutoBrightness*255f);
            Settings.System.putInt(getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS,
                    brightness);
            Settings.System.putInt(getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE,
                    Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        }
    }

完整代码, 请从这里下载:  http://download.csdn.net/detail/sno_guo/9265991

           

你可能感兴趣的:(android,视频,源代码,滑动,音量调节)