Android实现音乐播放器锁屏页
本文实例为大家分享了Android音乐播放器锁屏页的具体代码,供大家参考,具体内容如下
首页我们先看一下效果图
下边来说一下实现逻辑,其主要思路就是新建一个activity使其覆盖在锁屏页上边。
一、我们新建一个LockActivty,既然是四大组件之一,必不可少的在AndroidManifest.xml中注册:
<activity android:name=".LockActivity" android:excludeFromRecents="true" android:exported="false" android:launchMode="singleInstance" android:noHistory="true" android:screenOrientation="portrait" android:taskAffinity="com.ztk.lock" android:theme="@style/LockScreenTheme"/>
这里注意,LockActivty的启动模式,我们使用singleInstance,使其单独存在一个activity task中。
android:exported="false"标签,这个标签是用来表示不能被其他应用程序组件调用或跟它交互。
android:noHistory="true",表示该Activity在task中不留历史痕迹。
style文件如下:
<style name="LockScreenTheme" parent="AppTheme"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:colorBackgroundCacheHint">@null</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowAnimationStyle">@null</item> <item name="android:windowContentOverlay">@null</item> </style>
二、在LockActivty的onCreate方法中添加标志,使其能够在锁屏页上显示:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); fullScreen(this); setContentView(R.layout.activity_lock); }
这里同时也加入全屏的代码 fullScreen(this):
public static void fullScreen(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色 Window window = activity.getWindow(); View decorView = window.getDecorView(); //两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间 int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; decorView.setSystemUiVisibility(option); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.TRANSPARENT); } else { Window window = activity.getWindow(); WindowManager.LayoutParams attributes = window.getAttributes(); int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; attributes.flags |= flagTranslucentStatus; window.setAttributes(attributes); } } }
三、重写物理返回键使其不能响应返回键。
@Override public void onBackPressed() {}
四、向右滑动销毁页面,这里我们要用到触摸反馈的知识,自定义一个SlidingFinishLayout的view 继承RelativeLayout在LockActivity的布局文件中引用,这里重写onTouchEvent方法:
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: downX = tempX = (int) event.getRawX(); downY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveX = (int) event.getRawX(); int deltaX = tempX - moveX; tempX = moveX; if (Math.abs(moveX - downX) > mTouchSlop && Math.abs((int) event.getRawY() - downY) < mTouchSlop) { isSliding = true; } if (moveX - downX >= 0 && isSliding) { mParentView.scrollBy(deltaX, 0); } break; case MotionEvent.ACTION_UP: i sSliding = false; if (mParentView.getScrollX() <= -viewWidth / 4) { isFinish = true; scrollRight(); } else { scrollOrigin(); isFinish = false; } break; default: break; } return true; }
这里只贴出了主要代码,详细代码请看demo,文章末尾会有demo地址。
五、关于下方滑动解锁text的实现,是利用了颜色渐变器和矩阵平移实现:
public class HintTextView extends AppCompatTextView { private Paint paint; private int mWidth; private LinearGradient gradient; private Matrix matrix; /** * 渐变的速度 */ private int deltaX; public HintTextView(Context context) { super(context, null); } public HintTextView(Context context, AttributeSet attrs) { super(context, attrs); } { paint = getPaint(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if(mWidth == 0 ){ mWidth = getMeasuredWidth(); //颜色渐变器 gradient = new LinearGradient(0, 0, mWidth, 0, new int[]{Color.GRAY, Color.WHITE, Color.GRAY}, new float[]{0.3f,0.5f,1.0f}, Shader.TileMode.CLAMP); paint.setShader(gradient); matrix = new Matrix(); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(matrix !=null){ deltaX += mWidth / 8; if(deltaX > 2 * mWidth){ deltaX = -mWidth; } } //通过矩阵的平移实现 matrix.setTranslate(deltaX, 0); gradient.setLocalMatrix(matrix); postInvalidateDelayed(100); } }
六、最后我们首先新建一个service做接收锁屏键事件的逻辑,使其启动后在任何页面都可以响应锁屏事件让LockActivity出现在锁屏页面上。
1、在AndroidManifest.xml中注册service:
<service android:name=".service.PlayService" android:process=":main" />
2、在service中注册广播接收锁屏事件,并跳转锁屏页面:
ScreenBroadcastReceiver screenBroadcastReceiver; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); screenBroadcastReceiver = new ScreenBroadcastReceiver(); final IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_OFF); registerReceiver(screenBroadcastReceiver, filter); } public class ScreenBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { handleCommandIntent(intent); } } private void handleCommandIntent(Intent intent) { final String action = intent.getAction(); if (Intent.ACTION_SCREEN_OFF.equals(action) ){ Intent lockScreen = new Intent(this, LockActivity.class); lockScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(lockScreen); } } @Override public void onDestroy() { super.onDestroy(); unregisterReceiver(screenBroadcastReceiver); }
这样,锁屏页面的实现就大概完成了,有一点要注意的是像小米、vivo、魅族等一些手机会有锁屏显示的权限,默认是关闭的,需要手动打开。
demo地址:LockDemo
栏 目:Android代码
下一篇:浅谈Glide缓存key的问题
本文标题:Android实现音乐播放器锁屏页
本文地址:http://www.codeinn.net/misctech/13351.html