GT庫(kù)在很早的版本里就有出 通知欄封裝方法,但使用起來(lái)非常有局限性,接下來(lái)咋們來(lái)看看新版GT庫(kù)里的8種通知欄,是如何實(shí)現(xiàn)的。
通知欄效果圖:(博主真機(jī) 一加10T pro? Android版本12)
??
??
?
目錄
通知欄效果圖:(博主真機(jī) 一加10T pro? Adnroid版本12)
使用GT庫(kù)里的,當(dāng)然需要先依賴(lài)好GT庫(kù)啦:
第一種:最為常見(jiàn)的 普通 通知欄
第二種:大圖 通知欄
第三種:長(zhǎng)文本?通知欄
第四種:長(zhǎng)文本條 通知欄
第五種:消息 通知欄
第六種:進(jìn)度條 通知欄
第七種:簡(jiǎn)易 自定義通知欄
第八種:完全?自定義通知欄 (輕松定制音樂(lè)播放器~~~重磅來(lái)襲!?。。?/p>
學(xué)會(huì)了這些,今后定制啥通知欄,還不? 上手就來(lái)~~~
自定義 定制音樂(lè)播放器 整體源碼:
使用GT庫(kù)里的,當(dāng)然需要先依賴(lài)好GT庫(kù)啦:
GitHub - 1079374315/GTContribute to 1079374315/GT development by creating an account on GitHub.https://github.com/1079374315/GT
注意?。?!Android13及以上需要以下權(quán)限步驟,
權(quán)限步驟1:加入通知欄權(quán)限:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
權(quán)限步驟2:動(dòng)態(tài)申請(qǐng)通知欄權(quán)限 代碼:
//通知欄權(quán)限申請(qǐng)
GT.AppAuthorityManagement.Permission.init(activity, new String[]{
Manifest.permission.POST_NOTIFICATIONS
}).permissions(new GT.AppAuthorityManagement.Permission.OnPermissionListener() {
@Override
public void onExplainRequestReason(GT.AppAuthorityManagement.Permission.PermissionDescription onPDListener) {
onPDListener.setAcceptAdvice(true);//核心,設(shè)置拒絕授權(quán)
}
@Override
public boolean onForwardToSettings() {
//特殊權(quán)限特殊處理,如:需要進(jìn)入 系統(tǒng)設(shè)置 中或 應(yīng)用信息中的代碼可自定義填寫(xiě)
return true;//默認(rèn)是false 一定有改過(guò)來(lái)設(shè)置為 true
}
@Override
public void request(boolean allGranted, String[] grantedList, String[] deniedList, String message) {
if (allGranted) {
//通知欄權(quán)限授予,進(jìn)行發(fā)布通知欄操作
} else {
//未全部授權(quán),通知用戶(hù)
}
}
});
權(quán)限步驟3(非必要步驟):如果需要引導(dǎo)用戶(hù)開(kāi)啟 懸浮通知欄(類(lèi)似于微信消息彈窗) 可以直接加入以下代碼跳轉(zhuǎn)權(quán)限頁(yè):
GT.AppAuthorityManagement.openNotificationSettingsForApp(context);
下面我們來(lái)正文,重頭戲是最后一種通知欄 “完全自定義通知欄”
第一種:最為常見(jiàn)的 普通 通知欄
實(shí)現(xiàn)代碼:
?里面有個(gè) 是否直接啟動(dòng)的 參數(shù),如果填 true,創(chuàng)建就會(huì)自動(dòng)啟動(dòng)通知欄,如果填 false ,就還可以自定義屬性,類(lèi)似于這樣:
NotificationCompat.Builder builder = GT.GT_Notification.createNotificationForNormal(
JavaActivity.this,
com.gsls.gt.R.mipmap.gt_logo,//通知欄圖標(biāo)
com.gsls.gt.R.mipmap.gt_logo,//通知欄 右下角圖片
"通知欄標(biāo)題",//通知欄標(biāo)題
//通知欄內(nèi)容
"1.GT庫(kù)在很早的版本里就有出 " +
"通知欄封裝方法,但使用起來(lái)非常有局限性," +
"接下來(lái)咋們來(lái)看看新版GT庫(kù)里的8種通知欄,是如何實(shí)現(xiàn)的",
true,//通知欄單擊是否自動(dòng)取消
true,//鎖屏后是否彈出
new Intent(JavaActivity.this, MainActivity.class),//單擊跳轉(zhuǎn)的頁(yè)面
0,//發(fā)送通知欄的時(shí)間
true,//是否 直接啟動(dòng)通知欄
222//當(dāng)前通知的 Id編號(hào)
);
GT.GT_Notification.startNotification(builder,222);
第二種:大圖 通知欄
實(shí)現(xiàn)代碼:
//創(chuàng)建大圖通知欄
NotificationCompat.Builder notificationImg = GT.GT_Notification.createNotificationImg(
this,
R.mipmap.gt_logo,//通知欄圖標(biāo)
R.drawable.aaa,//通知欄 右下角圖片
R.drawable.eee,//通知欄大圖
"大圖通知標(biāo)題",//通知欄標(biāo)題
"我的內(nèi)容",//通知欄內(nèi)容
true,//是否單擊后取消通知
true,//是否鎖屏彈出
new Intent(this, MainActivity.class),//單擊意圖
-1//發(fā)送通知欄時(shí)間
);
//啟動(dòng)最終的通知欄
GT.GT_Notification.startNotification(notificationImg, 222);
如果你不想右上角有圖片可以這么寫(xiě):
//啟動(dòng)大圖通知欄
NotificationCompat.Builder notificationImg = GT.GT_Notification.createNotificationImg(
this,
-1,//取消通知欄圖片
-1,//取消通知欄右下角圖片
R.drawable.eee,//通知欄大圖
"大圖通知標(biāo)題",//通知欄標(biāo)題
"我的內(nèi)容",//通知欄內(nèi)容
true,//是否單擊后取消通知
true,//是否鎖屏彈出
new Intent(this, MainActivity.class),//單擊意圖
-1,//發(fā)送通知欄時(shí)間
222//通知欄編號(hào)ID
);
GT.GT_Notification.startNotification(notificationImg, 222);
效果圖:
是不是使用起來(lái)很簡(jiǎn)單~
第三種:長(zhǎng)文本?通知欄
?實(shí)現(xiàn)代碼:
//創(chuàng)建長(zhǎng)文字通知欄
NotificationCompat.Builder notificationImg =
GT.GT_Notification.createNotificationText(
this,
R.mipmap.gt_logo,//通知欄圖標(biāo)
R.drawable.aaa,//通知欄 右下角圖片
"大圖通知標(biāo)題",//通知欄標(biāo)題
"GT庫(kù)在很早的版本里就有出 通知欄封裝方法,但使用起來(lái)非常有局限性," +
"接下來(lái)咋們來(lái)看看新版GT庫(kù)里的8種通知欄,是如何實(shí)現(xiàn)的。" +
"GT庫(kù)目前總共封裝了8種通知欄,每一個(gè)通知欄使用起來(lái)都特別簡(jiǎn)單的," +
"最主要的還支持完全自定義通知欄。\nGT庫(kù)不止這一個(gè)好用的庫(kù)~",
true,//是否單擊后取消通知
true,//是否鎖屏彈出
new Intent(this, MainActivity.class),//單擊意圖
-1//發(fā)送通知欄時(shí)間
);
//啟動(dòng)最終的通知欄
GT.GT_Notification.startNotification(notificationImg, 222);
第四種:長(zhǎng)文本條 通知欄
?實(shí)現(xiàn)代碼:
//創(chuàng)建長(zhǎng)文字通知欄
NotificationCompat.Builder notificationImg =
GT.GT_Notification.createNotificationTexts(
this,
R.mipmap.gt_logo,//通知欄圖標(biāo)
R.drawable.aaa,//通知欄 右下角圖片
"大圖通知標(biāo)題",//通知欄標(biāo)題
false,//是否單擊后取消通知
true,//是否鎖屏彈出
new Intent(this, MainActivity.class),//單擊意圖
-1,//發(fā)送通知欄時(shí)間
222,
"1.GT庫(kù)在很早的版本里就有出",
"2.通知欄封裝方法,但使用起來(lái)非常有局限性",
"3.接下來(lái)咋們來(lái)看看新版GT庫(kù)里的8種通知欄"
);
//啟動(dòng)最終的通知欄
GT.GT_Notification.startNotification(notificationImg, 222);
目前我們是直接在創(chuàng)建的時(shí)候就添加了3條文本,如果我們想創(chuàng)建后添加并更新通知欄UI怎么做呢?我們可以這么操作:
//創(chuàng)建長(zhǎng)文字通知欄
NotificationCompat.Builder notificationImg =
GT.GT_Notification.createNotificationTexts(
this,
R.mipmap.gt_logo,//通知欄圖標(biāo)
R.drawable.aaa,//通知欄 右下角圖片
"大圖通知標(biāo)題",//通知欄標(biāo)題
false,//是否單擊后取消通知
true,//是否鎖屏彈出
new Intent(this, MainActivity.class),//單擊意圖
-1,//發(fā)送通知欄時(shí)間
222,
"1.GT庫(kù)在很早的版本里就有出",
"2.通知欄封裝方法,但使用起來(lái)非常有局限性",
"3.接下來(lái)咋們來(lái)看看新版GT庫(kù)里的8種通知欄"
);
//啟動(dòng)最終的通知欄
GT.GT_Notification.startNotification(notificationImg, 222);
//創(chuàng)建后動(dòng)態(tài)添加文本
GT.GT_Notification.addLineText(notificationImg,222,
"4.GT庫(kù)目前總共封裝了8種通知欄,",
"5.每一個(gè)通知欄使用起來(lái)都特別簡(jiǎn)單的,",
"6.最主要的還支持完全自定義通知欄。",
"7.GT庫(kù)不止這一個(gè)好用的庫(kù)~"
);
//更新最終修改的通知欄
GT.GT_Notification.startNotification(notificationImg, 222);
效果圖:
第五種:消息 通知欄
?
?實(shí)現(xiàn)代碼:
//創(chuàng)建消息通知欄
NotificationCompat.Builder notificationImg =
GT.GT_Notification.createNotificationMsgs(
this,
R.mipmap.gt_logo,//通知欄圖標(biāo)
-1,//通知欄 右下角圖片
"大圖通知標(biāo)題",//通知欄標(biāo)題
false,//是否單擊后取消通知
true,//是否鎖屏彈出
new Intent(this, MainActivity.class),//單擊意圖
-1,//發(fā)送通知欄時(shí)間
222,
"王重陽(yáng)" + GT.GT_Notification.SEPARATOR + "大清早是習(xí)武最好的時(shí)間!",
"郭靖" + GT.GT_Notification.SEPARATOR + "前輩的武功真是登峰造極呀!",
"周伯通" + GT.GT_Notification.SEPARATOR + "練武有什么好玩的",
"周伯通" + GT.GT_Notification.SEPARATOR + "大兄弟,你來(lái)啦!快來(lái)看看我這秀字的蜜蜂"
);
//啟動(dòng)最終的通知欄
GT.GT_Notification.startNotification(notificationImg, 222);
注意:GT.GT_Notification.SEPARATOR?字段是消息的分隔符,一定要用這個(gè)用于區(qū)分。
當(dāng)然,消息通知欄也支持創(chuàng)建后再動(dòng)態(tài)添加消息:
//創(chuàng)建消息通知欄
NotificationCompat.Builder notificationImg =
GT.GT_Notification.createNotificationMsgs(
this,
R.mipmap.gt_logo,//通知欄圖標(biāo)
-1,//通知欄 右下角圖片
"大圖通知標(biāo)題",//通知欄標(biāo)題
false,//是否單擊后取消通知
true,//是否鎖屏彈出
new Intent(this, MainActivity.class),//單擊意圖
-1,//發(fā)送通知欄時(shí)間
222,
"王重陽(yáng)" + GT.GT_Notification.SEPARATOR + "大清早是習(xí)武最好的時(shí)間!",
"郭靖" + GT.GT_Notification.SEPARATOR + "前輩的武功真是登峰造極呀!",
"周伯通" + GT.GT_Notification.SEPARATOR + "練武有什么好玩的",
"周伯通" + GT.GT_Notification.SEPARATOR + "大兄弟,你來(lái)啦!快來(lái)看看我這秀字的蜜蜂"
);
//啟動(dòng)最終的通知欄
GT.GT_Notification.startNotification(notificationImg, 222);
//創(chuàng)建后動(dòng)態(tài)添加消息
GT.GT_Notification.addLineMsg(notificationImg,222,
"郭靖" + GT.GT_Notification.SEPARATOR + "我不玩,華山論劍就只剩下2年了,我要繼續(xù)加油。",
"周伯通" + GT.GT_Notification.SEPARATOR + "華山論劍有什么好玩的,你不陪我玩,那我找小黃蓉玩去了。",
"黃蓉" + GT.GT_Notification.SEPARATOR + "老頑童!真是調(diào)皮,別去打擾靖哥哥習(xí)武了!",
"黃蓉" + GT.GT_Notification.SEPARATOR + "走!我?guī)銍L嘗我最新研究出來(lái)的菜品!"
);
//更新最終修改的通知欄
GT.GT_Notification.startNotification(notificationImg, 222);
效果圖:
?注意:發(fā)送的消息通知欄里是會(huì)有顯示限制的。
第六種:進(jìn)度條 通知欄
?實(shí)現(xiàn)代碼:
//創(chuàng)建進(jìn)度條知欄
NotificationCompat.Builder builder =
GT.GT_Notification.createNotificationProgress(
this,
R.mipmap.gt_logo,//通知欄圖標(biāo)
R.drawable.aaa,
"檢測(cè)到最新版本",
"正在更新中,請(qǐng)稍后...",
false,//單擊后是否自動(dòng)隱藏
true,//是否會(huì)懸浮狀態(tài)展示
true,//鎖屏是否顯示
new Intent(this, MainActivity.class),//單擊意圖
-1,//發(fā)送通知時(shí)間
100,//進(jìn)度最大值
0//當(dāng)前進(jìn)度
);
//啟動(dòng)最終的通知欄
GT.GT_Notification.startNotification(builder, 222);
既然是進(jìn)度條,那么怎么更新進(jìn)度條呢,沒(méi)錯(cuò),更新進(jìn)度條方法也是特別簡(jiǎn)單:
//創(chuàng)建進(jìn)度條知欄
NotificationCompat.Builder builder =
GT.GT_Notification.createNotificationProgress(
this,
R.mipmap.gt_logo,//通知欄圖標(biāo)
R.drawable.aaa,
"檢測(cè)到最新版本",
"正在更新中,請(qǐng)稍后...",
false,//單擊后是否自動(dòng)隱藏
true,//是否會(huì)懸浮狀態(tài)展示
true,//鎖屏是否顯示
new Intent(this, MainActivity.class),//單擊意圖
-1,//發(fā)送通知時(shí)間
100,//進(jìn)度最大值
0//當(dāng)前進(jìn)度
);
//啟動(dòng)最終的通知欄
GT.GT_Notification.startNotification(builder, 222);
//模擬網(wǎng)絡(luò)下載
new Thread(new Runnable() {
@Override
public void run() {
for (int progress = 0; progress <= 100; progress++) {
GT.Thread.sleep(50);//GT庫(kù)的延遲函數(shù),模擬網(wǎng)絡(luò)下載的速度
GT.GT_Notification.updateNotificationProgress(
builder,//修改該進(jìn)度條通知欄的進(jìn)度
"正在下載中..." + progress + "%",//進(jìn)度顯示
100,//進(jìn)度條最大值
progress,//進(jìn)度條當(dāng)前進(jìn)度
true,//是否隱藏在通知欄里進(jìn)行更新進(jìn)度
true,//完成后是否自動(dòng)隱藏
"下載完畢,正在安裝中!",//完成后顯示的文字
3000,//完成后等待3秒后取消通知
222//通知欄 編號(hào)ID
);
}
}
}).start();
效果圖:
總結(jié):這個(gè)使用起來(lái) 與 更新起來(lái) 方便吧,把建議都打在評(píng)論區(qū)吧~~
第七種:簡(jiǎn)易 自定義通知欄
?實(shí)現(xiàn)代碼:
//創(chuàng)建進(jìn)自定義知欄
NotificationCompat.Builder builder =
GT.GT_Notification.createNotificationFoldView(
this,
R.mipmap.gt_logo,//通知欄圖標(biāo)
R.layout.item_notification,//折疊布局
R.layout.item_notification2,//展開(kāi)布局
true,//單擊是否取消通知
true,//是鎖屏顯示
new Intent(this, MainActivity.class),//單擊意圖
-1,//發(fā)送通知時(shí)間
222//通知Id
);
//啟動(dòng)最終的通知欄
GT.GT_Notification.startNotification(builder, 222);
折疊布局:R.layout.item_notification
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:src="@drawable/music_head" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/Notification2Activity_music_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="歌曲名:最偉大的作品"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="歌手名:周杰倫"
android:textSize="12sp"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp">
<ImageButton
android:id="@+id/Notification2Activity_music_left"
android:layout_width="26dp"
android:layout_height="26dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@drawable/music_left" />
<ImageButton
android:id="@+id/Notification2Activity_music_play"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginStart="10dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@drawable/music_play" />
<ImageButton
android:id="@+id/Notification2Activity_music_right"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginStart="10dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@drawable/music_right" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
展開(kāi)布局:R.layout.item_notification2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:src="@drawable/music_head" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/Notification2Activity_music_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="歌曲名:最偉大的作品"
android:textColor="#000000"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="歌手名:周杰倫"
android:textSize="12sp"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp">
<ImageButton
android:id="@+id/Notification2Activity_music_left"
android:layout_width="26dp"
android:layout_height="26dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@drawable/music_left" />
<ImageButton
android:id="@+id/Notification2Activity_music_play"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginStart="10dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@drawable/music_play" />
<ImageButton
android:id="@+id/Notification2Activity_music_right"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginStart="10dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@drawable/music_right" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="110dp"
android:src="@drawable/ic_main2"
android:scaleType="fitXY"
/>
</LinearLayout>
</LinearLayout>
所用到的資源圖片:
注意:目前自定義通知欄,無(wú)法實(shí)現(xiàn)? 切換歌曲,暫停歌曲,只能單擊后直接跳轉(zhuǎn)到 相應(yīng)的Activity中而已,想要真正的實(shí)現(xiàn)這些單擊切換音樂(lè)、暫停音樂(lè)功能,
請(qǐng)看最后一種方法,“完全自定義通知欄”。
下面博主用了 第四版 加載布局,各位也要添加哦
?供復(fù)制:
//注冊(cè) gt-DataBinding 功能
annotationProcessor 'com.github.1079374315:GSLS_Tool:v1.4.4.9'
將紅色框框里的復(fù)制粘貼注冊(cè)好就可以使用第四版 加載布局了
第八種:完全?自定義通知欄 (輕松定制音樂(lè)播放器~~~重磅來(lái)襲!?。。?/h4>
為了降低GIF大小,所有錄制的GIF有點(diǎn)模糊,但實(shí)際效果是靜態(tài)圖這般。
實(shí)現(xiàn)基礎(chǔ)代碼:(去掉所有邏輯,僅僅保留UI層面的東西,為了讓伙伴們更好的理解)
//加載 折疊通知欄 與 展開(kāi)通知欄
@GT.Annotations.GT_Notification(value = R.layout.item_notification, value2 = R.layout.item_notification2)
public class NotificationDemo extends GT.GT_Notification.AnnotationNotification {
//必要的重寫(xiě) 構(gòu)造方法
public NotificationDemo(Context context) {
super(context);
}
@Override
protected void initView(Context context) {
super.initView(context);
//初始化通知欄 必要屬性
setInitData(
R.mipmap.gt_logo, //設(shè)置通知圖標(biāo)
false,//單擊是否取消通知
true,//鎖屏是否顯示
true,//是否用戶(hù)不可取消通知
null,//單擊意圖
-1,//發(fā)送通知時(shí)間
222);//通知欄編號(hào)Id
}
}
建議 發(fā)布通知欄 代碼:
//創(chuàng)建通知欄
NotificationDemo notificationDemo = new NotificationDemo(this);
GT.GT_Notification.startNotification(notificationDemo);//發(fā)送通知欄
還有另一種 發(fā)布通知欄?代碼:(如果你確定不需要再自定義什么了,就可以使用這種簡(jiǎn)短的方式)
//創(chuàng)建并發(fā)布 通知欄
new NotificationDemo(this).commit();
夠簡(jiǎn)短吧!xml布局用的是?簡(jiǎn)易 自定義通知欄 里的 折疊布局 與 展開(kāi)布局 可直接從上面復(fù)制下來(lái)即可。GT庫(kù)為簡(jiǎn)單而生
目前咋們這點(diǎn)代碼寫(xiě)的音樂(lè)播放器,運(yùn)行起來(lái)僅僅是個(gè)丑不拉幾的靜態(tài)UI,效果圖如下:
?接下來(lái)咋們來(lái)各個(gè)設(shè)置上默認(rèn)的美術(shù)圖片:
//加載 折疊通知欄 與 展開(kāi)通知欄
@GT.Annotations.GT_Notification(value = R.layout.item_notification, value2 = R.layout.item_notification2)
public class NotificationDemo extends GT.GT_Notification.AnnotationNotification {
//必要的重寫(xiě) 構(gòu)造方法
public NotificationDemo(Context context) {
super(context);
}
//重寫(xiě)必要的初始化方法
@Override
protected void initView(Context context) {
super.initView(context);
//初始化通知欄 必要屬性
setInitData(
R.mipmap.gt_logo, //設(shè)置通知圖標(biāo)
false,//單擊是否取消通知
true,//鎖屏是否顯示
true,//是否用戶(hù)不可取消通知
null,//單擊意圖
-1,//發(fā)送通知時(shí)間
222);//通知欄編號(hào)Id
//初始化UI屬性(博主建議:在初始化UI里的盡量使用本地資源)
setImageRes(R.id.iv_head, R.drawable.music_head);//設(shè)置頭像圖片
setImageRes(R.id.iv_main2, R.drawable.ic_main2);//設(shè)置展開(kāi)的解釋圖
setTextViewText(R.id.tv_name, "歌曲名:最偉大的作品");//設(shè)置歌曲名
setTextViewText(R.id.tv_author, "歌手名:周杰倫");//設(shè)置歌手
//設(shè)置網(wǎng)絡(luò)圖片(當(dāng)然也是可以在初始化UI里設(shè)置網(wǎng)絡(luò)資源的,可以自行解開(kāi)注釋看看效果)
// setImageUrl(R.id.ib_left, "http://t13.baidu.com/it/u=2287545062,2949645413&fm=224&app=112&f=JPEG?w=500&h=499");
}
}
此時(shí)運(yùn)行效果圖:
?此時(shí)效果圖,就有點(diǎn)兒 美噠噠了,注意:必須要使用GT庫(kù)封裝好的API去設(shè)置資源? 不然在頻繁更新網(wǎng)絡(luò)圖會(huì)遇見(jiàn)大坑。(要么全使用 GT庫(kù)API去設(shè)置資源,要么全部使用系統(tǒng)封裝的 API)??
如果使用系統(tǒng)原生的設(shè)置資源就可以這樣:(若使用系統(tǒng)原生提供的,后續(xù)在頻繁更新網(wǎng)絡(luò)圖會(huì)有問(wèn)題)
//初始化 折疊 UI屬性(使用系統(tǒng)原生設(shè)置資源)
remoteViews1.setImageViewResource(R.id.iv_head,R.drawable.music_head);
remoteViews1.setImageViewResource(R.id.iv_main2,R.drawable.ic_main2);
remoteViews1.setTextViewText(R.id.tv_name,"歌曲名:最偉大的作品");
remoteViews1.setTextViewText(R.id.tv_author,"歌手名:周杰倫");
//初始化 展開(kāi) UI屬性
remoteViews2.setImageViewResource(R.id.iv_head,R.drawable.music_head);
remoteViews2.setImageViewResource(R.id.iv_main2,R.drawable.ic_main2);
remoteViews2.setTextViewText(R.id.tv_name,"歌曲名:最偉大的作品");
remoteViews2.setTextViewText(R.id.tv_author,"歌手名:周杰倫");
使用系統(tǒng)原生的一般都要設(shè)置兩份代碼,因?yàn)橐环莶季?remoteViews1 是折疊布局的,另一份布局?remoteViews2 是展開(kāi)布局的。
此時(shí)的 音樂(lè)播放器 做的已經(jīng)是有模有樣了,接下來(lái)該怎么切換歌曲,暫停歌曲呢?
相信看過(guò)其他博主寫(xiě)的 通知欄教程的朋友們肯定會(huì)知道有這么一個(gè)方法
先是一個(gè)個(gè)添加Intent 的活動(dòng)標(biāo)識(shí),然后再通過(guò)發(fā)送廣播的方式進(jìn)行實(shí)現(xiàn)單擊切換歌曲
?這樣確實(shí)是可以做到切換歌曲的功能,但要是把這些添加到GT庫(kù)里來(lái),博主第一個(gè)會(huì)奔潰,尤其是 通知欄稍微復(fù)雜點(diǎn)的,會(huì)讓今后的代碼及其難以維護(hù)。我們來(lái)看看GT庫(kù)是怎么解決這一問(wèn)題的。
GT庫(kù) 實(shí)現(xiàn)的單擊事件:
//加載 折疊通知欄 與 展開(kāi)通知欄
@GT.Annotations.GT_Notification(value = R.layout.item_notification, value2 = R.layout.item_notification2)
public class NotificationDemo extends GT.GT_Notification.AnnotationNotification {
//必要的重寫(xiě) 構(gòu)造方法
public NotificationDemo(Context context) {
super(context);
}
//重寫(xiě)必要的初始化方法
@Override
protected void initView(Context context) {
super.initView(context);
//初始化通知欄 必要屬性
setInitData(
R.mipmap.gt_logo, //設(shè)置通知圖標(biāo)
false,//單擊是否取消通知
true,//鎖屏是否顯示
true,//是否用戶(hù)不可取消通知
null,//單擊意圖
-1,//發(fā)送通知時(shí)間
222);//通知欄編號(hào)Id
//初始化UI屬性(博主建議:在初始化UI里的盡量使用本地資源)
setImageRes(R.id.iv_head, R.drawable.music_head);//設(shè)置頭像圖片
setImageRes(R.id.iv_main2, R.drawable.ic_main2);//設(shè)置展開(kāi)的解釋圖
setTextViewText(R.id.tv_name, "歌曲名:最偉大的作品");//設(shè)置歌曲名
setTextViewText(R.id.tv_author, "歌手名:周杰倫");//設(shè)置歌手
}
//重寫(xiě)單擊方法,實(shí)現(xiàn)單擊事件
@GT.Annotations.GT_Click({R.id.ib_left, R.id.ib_PauseRestore, R.id.ib_right})
public void onClick(View view) {
super.onClick(view);
switch (view.getId()) {
case R.id.ib_left://上一首
GT.logt("上一首");
break;
case R.id.ib_PauseRestore://暫停/恢復(fù)
GT.logt("暫停/恢復(fù)");
break;
case R.id.ib_right://下一首
GT.logt("下一首");
break;
}
}
}
?哦呦呦,這代碼熟悉不,如果去掉 @GT.Annotations.GT_Click 注解,那你肯定會(huì)直接秒懂這代碼,怕不就是直接在xml布局中 onClick 設(shè)置的單擊事件吧。重寫(xiě)一個(gè)單擊方法后只需要將你想要單擊的組件注入注解中,再在下面的?switch 語(yǔ)句中寫(xiě)好執(zhí)行的邏輯,整個(gè)單擊事件就完成了。
接下來(lái),你運(yùn)行起來(lái),再單擊里面的切換歌曲/暫?;謴?fù)這些功能,就都有了反應(yīng)。
接下來(lái)我們?cè)賮?lái)學(xué)習(xí)最后一個(gè)環(huán)節(jié),暫停音樂(lè) 與 恢復(fù)音樂(lè) 音樂(lè)功能
GT庫(kù)封裝的通知欄設(shè)置布局,其實(shí)規(guī)則類(lèi)似于 事務(wù)提交一樣的方式去設(shè)計(jì)的,怎么理解呢,我們到實(shí)際代碼中去看看。
private boolean isPause = false;//暫停狀態(tài)
//重寫(xiě)單擊方法,實(shí)現(xiàn)單擊事件
@GT.Annotations.GT_Click({R.id.ib_left, R.id.ib_PauseRestore, R.id.ib_right})
public void onClick(View view) {
super.onClick(view);
switch (view.getId()) {
case R.id.ib_left://上一首
GT.logt("上一首");
break;
case R.id.ib_PauseRestore://暫停/恢復(fù)
GT.logt("暫停/恢復(fù)");
if (isPause) {
isPause = false;
GT.logt("點(diǎn)擊了暫停");
setTextViewText(R.id.tv_name, "暫停");//設(shè)置 歌曲名為 暫停
} else {
isPause = true;
GT.logt("點(diǎn)擊了恢復(fù)");
setTextViewText(R.id.tv_name, "恢復(fù)");//設(shè)置 歌曲名為 恢復(fù)
}
break;
case R.id.ib_right://下一首
GT.logt("下一首");
break;
}
}
我們?cè)趩螕羰录屑尤肓艘粋€(gè) boolean 值用來(lái)存儲(chǔ) 暫停恢復(fù)狀態(tài),然后沒(méi)點(diǎn)擊暫?;蚧謴?fù)就設(shè)置 歌曲名文本顯示為 暫停或恢復(fù) 文字,其實(shí)這段代碼是 設(shè)置無(wú)效的?因?yàn)樗m然確實(shí)是設(shè)置了但沒(méi)有去提交事務(wù),所以并不會(huì)真正的讓UI更新。想讓UI設(shè)置起效,必須要在每次設(shè)置的最后加上一個(gè)?commit(); 方法進(jìn)行提交事務(wù)進(jìn)行更新。
如這樣:
private boolean isPause = false;//暫停狀態(tài)
//重寫(xiě)單擊方法,實(shí)現(xiàn)單擊事件
@GT.Annotations.GT_Click({R.id.ib_left, R.id.ib_PauseRestore, R.id.ib_right})
public void onClick(View view) {
super.onClick(view);
switch (view.getId()) {
case R.id.ib_left://上一首
GT.logt("上一首");
break;
case R.id.ib_PauseRestore://暫停/恢復(fù)
GT.logt("暫停/恢復(fù)");
if (isPause) {
isPause = false;
GT.logt("點(diǎn)擊了暫停");
//設(shè)置 歌曲名為 暫停 并提交事務(wù)更新UI
setTextViewText(R.id.tv_name, "暫停").commit();
} else {
isPause = true;
GT.logt("點(diǎn)擊了恢復(fù)");
//設(shè)置 歌曲名為 恢復(fù) 并提交事務(wù)更新UI
setTextViewText(R.id.tv_name, "恢復(fù)").commit();
}
break;
case R.id.ib_right://下一首
GT.logt("下一首");
break;
}
}
那為啥之前咋們?cè)?初始化 方法里就不需要寫(xiě)?commit(); 去提交事務(wù)更新UI呢?
第一種,發(fā)布通知方法,會(huì)默認(rèn)提交更新
//創(chuàng)建通知欄
NotificationDemo notificationDemo = new NotificationDemo(this);
GT.GT_Notification.startNotification(notificationDemo);//發(fā)送通知欄
第二種,創(chuàng)建這個(gè)通知欄的時(shí)候,后面寫(xiě)了一個(gè)?commit(); 提交更新方法
//創(chuàng)建并發(fā)布 通知欄
new NotificationDemo(this).commit();
所以咋們?cè)趯?xiě)切換 暫停? 與 恢復(fù) 圖片切換的時(shí)候,就可以這么寫(xiě):
private boolean isPause = false;//暫停狀態(tài)
//重寫(xiě)單擊方法,實(shí)現(xiàn)單擊事件
@GT.Annotations.GT_Click({R.id.ib_left, R.id.ib_PauseRestore, R.id.ib_right})
public void onClick(View view) {
super.onClick(view);
switch (view.getId()) {
case R.id.ib_left://上一首
GT.logt("上一首");
break;
case R.id.ib_PauseRestore://暫停/恢復(fù)
GT.logt("暫停/恢復(fù)");
if (isPause) {
isPause = false;
GT.logt("點(diǎn)擊了暫停");
//設(shè)置 歌曲名為 暫停 并提交事務(wù)更新UI
setTextViewText(R.id.tv_name, "暫停");
setImageRes(R.id.ib_PauseRestore, R.drawable.music_play)
.commit();//提交事務(wù)進(jìn)行更新UI
} else {
isPause = true;
GT.logt("點(diǎn)擊了恢復(fù)");
//設(shè)置 歌曲名為 恢復(fù) 并提交事務(wù)更新UI
setTextViewText(R.id.tv_name, "恢復(fù)");
setImageRes(R.id.ib_PauseRestore, R.drawable.music_pause)
.commit();//提交事務(wù)進(jìn)行更新UI
}
break;
case R.id.ib_right://下一首
GT.logt("下一首");
//finish(); //關(guān)閉當(dāng)前通知欄
break;
}
}
此時(shí)加上這個(gè),你的暫停 與 恢復(fù)功能就可以高興的使用了,接下來(lái)就是一些其他的音樂(lè)播放器 處理邏輯了,博主不再詳細(xì)介紹,直接下面貼出整個(gè)源碼,
學(xué)會(huì)了這些,今后定制啥通知欄,還不? 上手就來(lái)~~~
自定義 定制音樂(lè)播放器 整體源碼:
//加載 折疊通知欄 與 展開(kāi)通知欄
@GT.Annotations.GT_Notification(value = R.layout.item_notification, value2 = R.layout.item_notification2)
public class NotificationDemo extends GT.GT_Notification.AnnotationNotification {
//必要的重寫(xiě) 構(gòu)造方法
public NotificationDemo(Context context) {
super(context);
}
private static List<String> urlList = new ArrayList<>();
//重寫(xiě)必要的初始化方法
@Override
protected void initView(Context context) {
super.initView(context);
//初始化通知欄 必要屬性
setInitData(
R.mipmap.gt_logo, //設(shè)置通知圖標(biāo)
false,//單擊是否取消通知
true,//鎖屏是否顯示
true,//是否用戶(hù)不可取消通知
null,//單擊意圖
-1,//發(fā)送通知時(shí)間
222);//通知欄編號(hào)Id
//初始化UI屬性(博主建議:在初始化UI里的盡量使用本地資源)
setImageRes(R.id.iv_head, R.drawable.music_head);//設(shè)置頭像圖片
setImageRes(R.id.iv_main2, R.drawable.ic_main2);//設(shè)置展開(kāi)的解釋圖
setTextViewText(R.id.tv_name, "歌曲名:最偉大的作品");//設(shè)置歌曲名
setTextViewText(R.id.tv_author, "歌手名:周杰倫");//設(shè)置歌手
//添加要切換的網(wǎng)圖,模擬后臺(tái)數(shù)據(jù)集
urlList.add("http://t13.baidu.com/it/u=2287545062,2949645413&fm=224&app=112&f=JPEG?w=500&h=499");
urlList.add("http://t15.baidu.com/it/u=4211663368,2899541822&fm=224&app=112&f=JPEG?w=500&h=500");
urlList.add("https://pics0.baidu.com/feed/3ac79f3df8dcd10005cb97e31d637c17bb122f88.jpeg?token=78ac62753e80c3ef5412d819fcccbc79");
urlList.add("https://pics3.baidu.com/feed/2e2eb9389b504fc23061ab7576c6381790ef6da2.jpeg?token=8216bfa1363a0759d3bbdc25cb374375");
//后續(xù)可自行添加 網(wǎng)圖
}
private boolean isPause = false;//暫停狀態(tài)
private int page = 0;
//重寫(xiě)單擊方法
@GT.Annotations.GT_Click({R.id.ib_left, R.id.ib_PauseRestore, R.id.ib_right})
public void onClick(View view) {
super.onClick(view);
switch (view.getId()) {
case R.id.ib_left://上一首
GT.toast("上一首");
--page;
if(page < 0){
page = 0;
return;
}
setTextViewText(R.id.tv_name, "上一首");
setImageUrl(R.id.iv_head, urlList.get(page)).commit();
break;
case R.id.ib_PauseRestore://暫停/恢復(fù)
if (isPause) {
isPause = false;
setTextViewText(R.id.tv_name, "暫停");
setImageRes(R.id.ib_PauseRestore, R.drawable.music_play)
.commit();
} else {
isPause = true;
setTextViewText(R.id.tv_name, "恢復(fù)");
setImageRes(R.id.ib_PauseRestore, R.drawable.music_pause)
.commit();
}
break;
case R.id.ib_right://下一首
GT.toast("下一首");
++page;
if(page > urlList.size()-1){
page = urlList.size()-1;
return;
}
setTextViewText(R.id.tv_name, "下一首");
setImageUrl(R.id.iv_head, urlList.get(page)).commit();
break;
}
}
}
滿(mǎn)滿(mǎn)干貨,本文總字?jǐn)?shù)?好了,又要禿了,拜了個(gè)拜~~~文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-625228.html
點(diǎn)個(gè)關(guān)注點(diǎn)個(gè)贊唄(〃'▽'〃)? ?關(guān)注博主最新發(fā)布庫(kù):GitHub - 1079374315/GT文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-625228.html
到了這里,關(guān)于A(yíng)ndroid——8種通知欄-超簡(jiǎn)單自定義的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!