您的位置:首页 > 移动开发 > Android开发

android 通知栏中自定义进度条长方形样式

2013-09-30 13:12 399 查看
这段时间公司项目功能涉及到下载apk,在通知栏中显示下载进度,其中通知栏中进度条需要自定义。

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();
 		}
 	}
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: