編輯:關於Android編程
做視頻用到了就記錄下,從github(https://github.com/curtis2/SuperVideoPlayer 謝謝)上扒了一個項目的手勢調亮度和音量(但是有bug!!!就是調節音量感覺會突然消失),本文重點是如何調節音量
package com.example.administrator.gesturetext; import android.content.Context; import android.media.AudioManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.Display; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.RelativeLayout; public class MainActivity extends AppCompatActivity { private FrameLayout mVolumeBrightnessLayout; private ImageView mOperationBg; private ImageView mOperationPercent; private GestureDetector mGestureDetector; private AudioManager mAudioManager; private int mMaxVolume; private int mVolume = -1; private float mBrightness = -1f; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RelativeLayout rlContentContainer = (RelativeLayout) findViewById(R.id.rl_content_container); mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); mVolumeBrightnessLayout = (FrameLayout) findViewById(R.id.operation_volume_brightness); mOperationBg = (ImageView) findViewById(R.id.operation_bg); mOperationPercent = (ImageView) findViewById(R.id.operation_percent); mGestureDetector = new GestureDetector(MainActivity.this, new VolumeBrightnesGestureListener()); rlContentContainer.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (mGestureDetector.onTouchEvent(motionEvent)) { return true; } switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: endGesture(); break; } return true; } }); } private Handler mDismissHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0) { mVolumeBrightnessLayout.setVisibility(View.GONE); } } }; private void endGesture() { mVolume = -1; mBrightness = -1f; // 隱藏 mDismissHandler.removeMessages(0); mDismissHandler.sendEmptyMessageDelayed(0, 500); } private class VolumeBrightnesGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { float mOldX = e1.getX(), mOldY = e1.getY(); int y = (int) e2.getRawY(); Display disp = getWindowManager().getDefaultDisplay(); int windowWidth = disp.getWidth(); int windowHeight = disp.getHeight(); //1440 2392 if (mOldX > windowWidth * 3.0 / 5) { onVolumeSlide((mOldY - y) / windowHeight); return true; } else if (mOldX < windowWidth *2.0/ 5.0) { onBrightnessSlide((mOldY - y) / windowHeight); return true; } return false; } } /** * 聲音高低 * * @param percent */ private void onVolumeSlide(float percent) { if (mVolume == -1) { mVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); if (mVolume < 0) mVolume = 0; mOperationBg.setImageResource(R.drawable.video_volumn_bg); mVolumeBrightnessLayout.setVisibility(View.VISIBLE); } int index = (int) (percent * mMaxVolume) + mVolume; if (index > mMaxVolume) index = mMaxVolume; else if (index < 0) index = 0; mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0); ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams(); lp.width = findViewById(R.id.operation_full).getLayoutParams().width * index / mMaxVolume; mOperationPercent.setLayoutParams(lp); } /** * 處理屏幕亮暗 * * @param percent */ private void onBrightnessSlide(float percent) { if (mBrightness < 0) { mBrightness = getWindow().getAttributes().screenBrightness; if (mBrightness <= 0.00f) mBrightness = 0.50f; if (mBrightness < 0.01f) mBrightness = 0.01f; mOperationBg.setImageResource(R.drawable.video_brightness_bg); mVolumeBrightnessLayout.setVisibility(View.VISIBLE); } WindowManager.LayoutParams lpa = getWindow().getAttributes(); lpa.screenBrightness = mBrightness + percent; if (lpa.screenBrightness > 1.0f) lpa.screenBrightness = 1.0f; else if (lpa.screenBrightness < 0.01f) lpa.screenBrightness = 0.01f; getWindow().setAttributes(lpa); ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams(); lp.width = (int) (findViewById(R.id.operation_full).getLayoutParams().width * lpa.screenBrightness); mOperationPercent.setLayoutParams(lp); } }
(1)屏幕亮暗度
WindowManager.LayoutParams lpa = getWindow().getAttributes(); lpa.screenBrightness = mBrightness + percent; if (lpa.screenBrightness > 1.0f) lpa.screenBrightness = 1.0f; else if (lpa.screenBrightness < 0.01f) lpa.screenBrightness = 0.01f; getWindow().setAttributes(lpa);注意!!!:如果要跟系統同步 需要寫WRITTING_SETTING權限,Android6.0及以上需要寫個intent手動設置,在清單文件中寫沒用的(這個太傻比了,估計沒人會要這麼做)
(2)音量
我們這裡只設置了媒體音樂
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE) mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0);
public static final int STREAM_ALARM = 4; public static final int STREAM_DTMF = 8; public static final int STREAM_MUSIC = 3; public static final int STREAM_NOTIFICATION = 5; public static final int STREAM_RING = 2; public static final int STREAM_SYSTEM = 1
可以看出有很多種,套路都是一樣的
代碼鏈接待添加
大家好,眾所周知,android裡兩個相同方向的ScrollView是不能嵌套的,那要是有這樣的需求怎麼辦?(這個需求一般都是不懂android的人提出來的)難道就真的不
一、問題在哪裡?問題來源於app開發中一個很常見的場景——用戶頭像要展示成圓的: 二、怎麼搞?機智的我,第一想法就是,切一張中間圓形透明、四周與底色相同、尺寸與
u-boot對環境變量的處理主要包括兩部分:一是環境變量初始化,二是環境變量的設定、刪除等操作。下面將分別進行討論。這裡所使用的u-boot版本為2015.7,硬件為I.
簡介系統自帶的搜索頁面類 — UISearchDisplayController和UISearchController, 讓你更方便快捷的進行搜索功能開發.