android 通知栏中自定义进度条长方形样式
2013-09-30 13:12
399 查看
这段时间公司项目功能涉及到下载apk,在通知栏中显示下载进度,其中通知栏中进度条需要自定义。
1.效果
底图push_download_progress_bg.9.png和进度图push_download_progress.9.png
![](http://img.blog.csdn.net/20130930131917062?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2h1dGht/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20130930132021468?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2h1dGht/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
效果
![](http://img.blog.csdn.net/20130930132231484?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2h1dGht/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2.通知栏布局
布局文件push_notification.xml
其中将ProgressBar的属性indeterminate和indeterminateOnly置为false,background设为自定义的背景图。
此处为关键:属性progressDrawable设为push_horizontal.xml
网上很多自定义进度条是直接将progressDrawable设置为图片或者是将图片在push_horizontal.xml
<item android:id="@android:id/progress" android:drawable="@drawable/push_download_progress"> 中这样设置,但是这样设置后进度固定为初始进度值,此处是将图片设置在clip节点下(为什么这样设置,可能是与drawable的不同类型有关)。
3. 设置进度(直接贴代码)
1.效果
底图push_download_progress_bg.9.png和进度图push_download_progress.9.png
效果
2.通知栏布局
布局文件push_notification.xml
<?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="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/push_icon" android:layout_width="@android:dimen/notification_large_icon_width" android:layout_height="@android:dimen/notification_large_icon_height" android:scaleType="center"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingLeft="12dp" android:paddingRight="12dp" android:orientation="vertical"> <TextView android:id="@+id/push_text" android:textAppearance="@android:style/TextAppearance.StatusBar.EventContent.Title" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" android:fadingEdge="horizontal" /> <TextView android:id="@+id/push_text1" android:textAppearance="@android:style/TextAppearance.StatusBar.EventContent" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="-2dp" android:layout_marginBottom="-2dp" android:singleLine="true" android:fadingEdge="horizontal" android:ellipsize="marquee" android:visibility="gone" /> <ProgressBar android:id="@+id/push_download_progressBar" android:indeterminate="false" android:indeterminateOnly="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:progress="0" android:max="100" android:minHeight="13dip" android:maxHeight="13dip" android:progressDrawable="@drawable/push_horizontal" android:background="@drawable/push_download_progress_bg" android:visibility="gone" /> </LinearLayout> </LinearLayout>
其中将ProgressBar的属性indeterminate和indeterminateOnly置为false,background设为自定义的背景图。
此处为关键:属性progressDrawable设为push_horizontal.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/progress"> <clip android:drawable="@drawable/push_download_progress"> </clip> </item> </layer-list>
网上很多自定义进度条是直接将progressDrawable设置为图片或者是将图片在push_horizontal.xml
<item android:id="@android:id/progress" android:drawable="@drawable/push_download_progress"> 中这样设置,但是这样设置后进度固定为初始进度值,此处是将图片设置在clip节点下(为什么这样设置,可能是与drawable的不同类型有关)。
3. 设置进度(直接贴代码)
static class DownloadAPK implements Runnable { public void run() { ...... mNotification = new Notification(R.drawable.ic_launcher, title, System.currentTimeMillis()); mNotification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.push_notificaticon); mNotification.contentView.setViewVisibility(R.id.push_icon, View.VISIBLE); mNotification.contentView.setViewVisibility(R.id.push_download_progressBar, View.VISIBLE); mNotification.contentView.setViewVisibility(R.id.push_text, View.VISIBLE); mNotification.contentView.setViewVisibility(R.id.push_text1, View.GONE); mNotification.contentView.setTextViewText(R.id.push_text, title); mNotification.contentView.setImageViewBitmap(R.id.push_icon, icon); //更新进度 mNotification.contentView.setProgressBar(R.id.push_download_progressBar, 100, progress, false); mNotification.contentIntent = null; mNotification.flags |= Notification.FLAG_NO_CLEAR; mNotificationManager.notify(id, mNotification); ...... String path = Constants.DOWNLOAD_APK_DIR + "/"+name; InputStream is = null; FileOutputStream fos = null; try { //下载apk is = getDownLoadPushApkInputStream(url); final int length = 1024; byte[] b = new byte[length]; int len = -1; File file = new File(path).getParentFile(); if (!file.exists()) { file.mkdirs(); } fos = new FileOutputStream(path); while ((len = is.read(b))!=-1) { fos.write(b, 0, len); //计算进度 completeSize += len; int oldProgress = progress; progress = Math.min((int)((completeSize / (float)size) * 100), 100); if(progress != oldProgress) { if(DEBUG)Log.e(TAG, "download apk progress = " + progress); //更新进度 mNotification.contentView.setProgressBar(R.id.push_download_progressBar, 100, progress, false); mNotificationManager.notify(id, mNotification); } } success = true; } catch(Exception e) { } finally { try { if (fos != null)fos.close(); if(is != null)is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
相关文章推荐
- Android自定义通知样式适配
- 自定义View实现Android圆形进度条,支持自定义显示的样式
- Android自定义通知样式适配
- Android 自定义Notification消息通知样式并实现与Activity简单交互
- Android Material Design 进度条 自定义进度条样式
- Android自定义通知样式适配
- android自定义ProgressBar 修改进度条样式 出现setProgress无效问题
- Android:自定义ProgressBar的样式(背景色/一级、二级进度条颜色)
- Android Notification自定义通知样式你要知道的事
- Android Notification自定义通知样式你要知道的事
- Android 通知栏Notification,NotificationManager,RemoteViews使用流程,自定义通知栏样式简介
- Android 自定义样式通知栏的坑RemoteServiceException
- Android中通知的简单使用和自定义通知样式
- android文件下载及自定义通知显示下载进度
- Android 自定义Notification通知样式并设置按钮监听.
- Android:自定义ProgressBar的样式(背景色/一级、二级进度条颜色)
- Android Notification自定义通知样式你要知道的事
- Android Notification自定义通知样式你要知道的事
- android 进度条seekbar样式自定义
- Android_自定义两种样式进度条