编辑:
我创建了一个仅使用九个补丁可绘制的进度条和另一个依赖于剪辑可绘制的进度条,正如我在下面发布的那样,我已经将这两个项目都发布在了github
。有索尼设备的人可以测试这些项目并告诉我哪个适用于他们(它不工作的设备是一个Xperia M
)。我在评论中发布了链接,或者你可以告诉我在哪里可以免费在线测试。
此代码适用于我的 Galaxy Nexus,但似乎不适用于某些设备......
我使用以下 xml 文件创建了一个自定义进度条:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Background is a rectangle with a solid black fill -->
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<corners android:radius="8dp"/>
<solid android:color="#000000"/>
<stroke android:width="4dp"
android:color="#ffffff"/>
</shape>
</item>
<!-- The secondary progress,a gradient with a stroke and a padding -->
<item android:id="@android:id/secondaryProgress">
<clip>
<shape android:shape="rectangle">
<corners android:radius="8dp"/>
<gradient
android:startColor="#6c006d"
android:centerColor="#e100e0"
android:endColor="#ff28ff"
android:angle="0"
android:type="linear"
/>
<stroke android:width="4dp"
android:color="#ffffff"/>
</shape>
</clip>
</item>
<item android:id="@+id/progress">
<shape android:shape="rectangle">
<corners android:radius="8dp"/>
<stroke android:width="4dp"
android:color="#ffffff"/>
<scale android:scaleWidth="100%"
android:drawable="@drawable/untitled_1"/></shape>
</item>
</layer-list>
这是我用来在 XML 中定义 ProgressBar 的 XML:
<ProgressBar android:id="@+id/progressbar_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progressDrawable="@drawable/custom_progressbar"
android:layout_marginRight="5dp"
style="@android:style/Widget.ProgressBar.Horizontal"
/>
我的两个使用 Sony 智能手机的朋友报告了应用程序不显示进度或在进度条应该加载时抛出错误的问题。我在保留它的实例的片段中使用 AsyncTask 更新进度条,如下所示:
@Override
public void onProgressUpdate(Bitmap bitmap,int progress) {
// TODO Auto-generated method stub
int size = (int) ( bitmap .getHeight() * (1024.0 / bitmap .getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(bitmap , 1024, size, true);
img_currentOp.setImageBitmap(scaled);
updatebar.setProgress(progress);
}
此处
建议使用此组合的模式
这是我用于创建进度条的图像: