对于想了解在android中双方四舍五入的进度条的读者,本文将是一篇不可错过的文章,我们将详细介绍安卓四舍五入,并且为您提供关于Android/Java:将数字四舍五入得小数、android–与服务器
对于想了解在android中双方四舍五入的进度条的读者,本文将是一篇不可错过的文章,我们将详细介绍安卓四舍五入,并且为您提供关于Android / Java:将数字四舍五入得小数、android – 与服务器同步时的进度条、android – 更新通知区域中的进度条、Android 电池电量进度条,上下滚动图片的进度条(battery)的有价值信息。
本文目录一览:- 在android中双方四舍五入的进度条(安卓四舍五入)
- Android / Java:将数字四舍五入得小数
- android – 与服务器同步时的进度条
- android – 更新通知区域中的进度条
- Android 电池电量进度条,上下滚动图片的进度条(battery)
在android中双方四舍五入的进度条(安卓四舍五入)
我想在android中创建一个自定义进度条.我已经使用了以下xml文件(progress_bar_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/background">
<shape>
<corners android:radius="8dip" />
<stroke android:width="2dip" android:color="#FFFF"/>
<solid android:color="#FFFF"/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="8dip" />
<stroke android:width="2dip" android:color="#FFFF"/>
<solid android:color="#FF00"/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="8dip" />
<stroke android:width="2dip" android:color="#FFFF"/>
<solid android:color="#FF00"/>
</shape>
</clip>
</item>
</layer-list>
除了事实上我希望双方的进度条都有进展之外,一切都很有效.上述代码使得进度条在左侧被舍入并且简单地在右侧切割(不是圆形).这可能是因为剪辑标签.你能帮帮我吗?我应该改变什么才能在我的进度条中取得进展?
完整的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/gradient_progress"
android:padding="10dip"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/progress_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textandroid:text="Uploading"
/>
<TextView
android:id="@+id/progress_percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textandroid:text="55%"
android:paddingLeft="10dip"
/>
</LinearLayout>
<ProgressBar
android:id="@+id/progress_bar"android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/progress_bar_horizontal"
android:maxHeight="12dip"
android:minHeight="12dip"
android:max="100"
/>
</LinearLayout>
</LinearLayout>
以下是我努力的结果:link text
我希望红色的条纹在右侧也有圆角.
非常感谢你的评论.
解决方法:
这是时代的更新答案:
Android源代码使用Patch 9文件来实现效果:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/res/res/drawable/progress_horizontal_holo_dark.xml/
所以从布局xml开始:
<ProgressBar
android:id="@+id/custom_progress_bar"android:indeterminateOnly="false"
android:progressDrawable="@drawable/custom_progress_bar_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="13"
android:progress="33"
android:secondaryProgress="66" />
你可以将很多这个移动到一个样式xml,但这不是重点.我们真正关心的是android:progressDrawable =“@ drawable / custom_progress_bar_horizontal” – 这将允许我们指定我们自己的自定义进度条:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background"
android:drawable="@android:drawable/custom_progress_bg" />
<item android:id="@android:id/secondaryProgress">
<scale android:scaleWidth="100%"
android:drawable="@android:drawable/custom_progress_secondary" />
</item>
<item android:id="@android:id/progress">
<scale android:scaleWidth="100%"
android:drawable="@android:drawable/custom_progress_primary" />
</item>
</layer-list>
或者您不必使用Patch 9作为背景 – 例如,这里是一个带有1dp边框的简单白色背景:
<item android:id="@android:id/background">
<shape>
<corners android:radius="6.5dp" />
<solid android:color="@android:color/white" />
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
</shape>
</item>
Android Asset Studio有一个很棒的工具来帮助您生成Patch 9文件:
http://android-ui-utils.googlecode.com/hg/asset-studio/dist/nine-patches.html
在工具之前使用填充的示例主xdpi png:
在工具之前使用填充的辅助xdpi png示例:
最后的输出:
Android / Java:将数字四舍五入得小数
如何将十进制数四舍五入为整数。
3.50 => 4
4.5 => 5
3.4 => 3
您如何用Java做到这一点?谢谢!
android – 与服务器同步时的进度条
我的应用程序需要不断与远程服务器连接以下载数据.我希望在下载数据时实现进度条,并显示下载过程中剩余的进度和估计时间.
任何人都可以建议最好的方法来做到这一点?
解决方法:
这取决于同步的类型…例如:
>如果您的同步过程只包含下载包含所有新数据(例如JSON文件)的1个文件,则很容易.你只需要测量你下载了多少文件(看看这个问题Download a file with Android, and showing the progress in a ProgressDialog)
>如果您的同步过程涉及下载不同的文件(例如,图像,文本等)……您可以根据要下载的文件数量来实现进度条(并希望它们的重量是多少).
>如果您下载不同的文件,但它们从一个同步到另一个不同,您可以先询问服务器同步将使用多少文件,以及每个文件的大小.然后像上面那样做.
理想情况下,您必须考虑接收文件的大小,以及使用它们所花费的时间.
android – 更新通知区域中的进度条
我有一个custom_notification_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="3dip" > <TextView android:id="@+id/text" android:text="Uploading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" /> <ProgressBarandroid:layout_height="wrap_content" android:layout_width="fill_parent" android:max="0" android:progress="0" android:layout_marginLeft="10dip" android:id="@+id/progressBar" /> </LinearLayout>
我还有一些创建通知的测试代码,它可以工作并显示进度条.
notificationmanager mManager = (notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.icon,title,System.currentTimeMillis()); RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.custom_notification_layout); contentView.setProgressBar(R.id.progressBar,10,false); contentView.setTextViewText(R.id.text,text); notification.contentView = contentView; Intent notificationIntent = new Intent(context,NotificationHandler.class); PendingIntent contentIntent = PendingIntent.getActivity(context,notificationIntent,0); notification.contentIntent = contentIntent; mManager.notify(APPID,notification);
最后我尝试更新进度条,这不起作用.
contentView.setProgressBar(R.id.progressBar,5,false);
实际更新通知的秘诀是什么?
解决方法
// update the notification object notification.contentView.setProgressBar(R.id.progressBar,false); // notify the notification manager on the update. mManager.notify(APPID,notification);
Android 电池电量进度条,上下滚动图片的进度条(battery)
最近,制作一个app,需要模拟一个电池电量的进度条,根据电量多少来设置百分比,进度条不断上下滚动,就像平时手机充电一样的电池电量进度条。我就自定义view实现了电量进度条。修改图片就可以达到自己想要的效果
一、自定义view,Battery.java,循环刷新界面,两张图片上下滚动,达到不断向右移动的效果。挺有意思的
package com.example.battery; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.View; public class Battery extends View { public float currentX = 80; public float currentY = 80; private float secondY = 80; private Paint mPaint = new Paint(); private Context mContext; private Handler mHandler; private Bitmap mBmp; private int speedTime = 20; private float with = 200; private float height = 50; private float percentage = 0.5f; public Battery(Context context) { super(context); this.mContext = context; } public Battery(Context context,AttributeSet set) { super(context,set); this.mContext = context; init(); } public void onDraw(Canvas canvas) { super.onDraw(canvas); with = this.getWidth(); height = this.getHeight(); mPaint.setColor(Color.BLUE); Resources res = mContext.getResources(); BitmapDrawable bmpDraw = (BitmapDrawable) res .getDrawable(R.drawable.loading_pic); mBmp = bmpDraw.getBitmap(); canvas.clipRect(0,with*percentage,height); canvas.drawBitmap(mBmp,currentY,mPaint); canvas.drawBitmap(mBmp,secondY,mPaint); } private void init() { percentage = 0; mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: currentX ++; currentY ++; if (mBmp != null && currentY > mBmp.getHeight()){ currentY = -mBmp.getHeight(); } if (mBmp != null){ secondY = currentY+mBmp.getHeight(); if (secondY >= mBmp.getHeight()){ secondY = currentY-mBmp.getHeight(); } } percentage = percentage + 0.003f; if (percentage > 1){ percentage = 0; } // 每次计算后都发送消息进入下一次循环,并刷新界面 mHandler.sendEmptyMessageDelayed(1,speedTime); postInvalidate(); break; } super.handleMessage(msg); postInvalidate(); } }; // 首次循环刷新界面 mHandler.sendEmptyMessageDelayed(1,speedTime); } }
二、MainActivity
package com.example.battery; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
三、activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" > <com.example.battery.Battery android:layout_width="300dp" android:layout_height="10dp" android:layout_gravity="center" android:gravity="center" android:padding="10dp" /> </LinearLayout>
四、附图片效果
五、下载路径
下载:http://download.csdn.net/detail/lqw770737185/7824695</>
本文地址:http://www.cnblogs.com/liqw/p/3938422.html
关于在android中双方四舍五入的进度条和安卓四舍五入的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Android / Java:将数字四舍五入得小数、android – 与服务器同步时的进度条、android – 更新通知区域中的进度条、Android 电池电量进度条,上下滚动图片的进度条(battery)等相关内容,可以在本站寻找。
本文标签: