Android视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现
程序员文章站
2023-12-16 10:11:04
本文给大家分享android视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现,具体代码如下所示:
import android.app.activity;...
本文给大家分享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视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现,希望对大家有所帮助