溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android如何實(shí)現(xiàn)音樂播放器鎖屏頁

發(fā)布時間:2021-09-27 11:25:42 來源:億速云 閱讀:213 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“Android如何實(shí)現(xiàn)音樂播放器鎖屏頁”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Android如何實(shí)現(xiàn)音樂播放器鎖屏頁”這篇文章吧。

具體內(nèi)容如下

實(shí)現(xiàn)邏輯,其主要思路就是新建一個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,使其單獨(dú)存在一個activity task中。

android:exported="false"標(biāo)簽,這個標(biāo)簽是用來表示不能被其他應(yīng)用程序組件調(diào)用或跟它交互。

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方法中添加標(biāo)志,使其能夠在鎖屏頁上顯示:

@Overrideprotected 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開始需要把顏色設(shè)置透明,否則導(dǎo)航欄會呈現(xiàn)系統(tǒng)默認(rèn)的淺灰色      Window window = activity.getWindow();      View decorView = window.getDecorView();      //兩個 flag 要結(jié)合使用,表示讓應(yīng)用的主體內(nèi)容占用系統(tǒng)狀態(tài)欄的空間      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);  } }}

三、重寫物理返回鍵使其不能響應(yīng)返回鍵。

@Overridepublic void onBackPressed() {}

四、向右滑動銷毀頁面,這里我們要用到觸摸反饋的知識,自定義一個SlidingFinishLayout的view繼承RelativeLayout在LockActivity的布局文件中引用,這里重寫onTouchEvent方法:

@Overridepublic 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;}

這里只貼出了主要代碼,詳細(xì)代碼請看demo,文章末尾會有demo地址。

五、關(guān)于下方滑動解鎖text的實(shí)現(xiàn),是利用了顏色漸變器和矩陣平移實(shí)現(xiàn):

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;       }    }    //通過矩陣的平移實(shí)現(xiàn)    matrix.setTranslate(deltaX, 0);    gradient.setLocalMatrix(matrix);    postInvalidateDelayed(100);  }}

六、最后我們首先新建一個service做接收鎖屏鍵事件的邏輯,使其啟動后在任何頁面都可以響應(yīng)鎖屏事件讓LockActivity出現(xiàn)在鎖屏頁面上。

1、在AndroidManifest.xml中注冊service:

<service  android:name=".service.PlayService"  android:process=":main" />

2、在service中注冊廣播接收鎖屏事件,并跳轉(zhuǎn)鎖屏頁面:

ScreenBroadcastReceiver screenBroadcastReceiver;@Nullable@Overridepublic IBinder onBind(Intent intent) {  return null;}@Overridepublic 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); }

這樣,鎖屏頁面的實(shí)現(xiàn)就大概完成了,有一點(diǎn)要注意的是像小米、vivo、魅族等一些手機(jī)會有鎖屏顯示的權(quán)限,默認(rèn)是關(guān)閉的,需要手動打開。

以上是“Android如何實(shí)現(xiàn)音樂播放器鎖屏頁”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI