本文给大家分享Android视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现,具体代码如下所示:
import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.TextView; public class MainActivity extends Activity { private float startY = 0;//手指按下时的Y坐标 private float startX = 0;//手指按下时的Y坐标 private TextView brightnessTextView; private View mVideoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mVideoView = LayoutInflater.from(this).inflate(R.layout.activity_main,null); setContentView(mVideoView); brightnessTextView = (TextView) mVideoView.findViewById(R.id.text); } /* * 设置屏幕亮度 * 0 最暗 * 1 最亮 */ public void setBrightness(float brightness) { WindowManager.LayoutParams lp = getwindow().getAttributes(); lp.screenBrightness = lp.screenBrightness + brightness / 255.0f; if (lp.screenBrightness > 1) { lp.screenBrightness = 1; } else if (lp.screenBrightness < 0.1) { lp.screenBrightness = (float) 0.1; } getwindow().setAttributes(lp); float sb = lp.screenBrightness; brightnessTextView.setText((int) Math.ceil(sb * 100) + "%"); } @Override public boolean onTouchEvent(MotionEvent event) { int screenWidth = mVideoView.getWidth(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX = event.getX(); startY = event.getY(); break; case MotionEvent.ACTION_MOVE: float endY = event.getY(); float distanceY = startY - endY; if (startX > screenWidth / 2) { //右边 //在这里处理音量 } else { //屏幕左半部分上滑,亮度变大,下滑,亮度变小 final double FLING_MIN_disTANCE = 0.5; final double FLING_MIN_VELociTY = 0.5; if (distanceY > FLING_MIN_disTANCE && Math.abs(distanceY) > FLING_MIN_VELociTY) { setBrightness(10); } if (distanceY < FLING_MIN_disTANCE && Math.abs(distanceY) > FLING_MIN_VELociTY) { setBrightness(-10); } } break; } return super.onTouchEvent(event); } }
activity_main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/ic_launcher"> <TextView android:id="@+id/text" android:layout_width="150dp" android:layout_height="50dp" android:layout_gravity="center" android:background="@android:color/holo_orange_light" android:gravity="center" android:textColor="@android:color/white" /> </FrameLayout>
以上所述是小编给大家介绍的Android视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。