java - Android 中的自定义进度条?

标签 java android xml progress-bar progress

<分区>

我还不知道如何做到这一点。我的进度条应该是云的形状。有人可以指导我阅读书籍、教程或只是给出正确的分步方法吗?

感谢您的宝贵时间。

最佳答案

如图here您可以使用 ImageView 来获得类似效果的自定义滚动条。

该示例中自定义进度条的布局 XML 是:

<RelativeLayout android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center" android:layout_height="wrap_content"
    android:paddingLeft="30sp" android:paddingRight="30sp">
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/progress_1"
        android:id="@+id/imgOne" android:tag="1"></ImageView>
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/progress_2"
        android:id="@+id/imgTwo" android:layout_toRightOf="@id/imgOne"
        android:tag="2"></ImageView>
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/progress_3"
        android:id="@+id/imgThree" android:layout_toRightOf="@id/imgTwo"
        android:tag="3"></ImageView>
    <TextView android:id="@+id/TextView01" android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imgThree" android:layout_width="wrap_content"
        android:layout_alignTop="@id/imgThree" android:layout_alignBottom="@id/imgThree"
        android:gravity="bottom" android:text="Please Wait..."></TextView>
</RelativeLayout>

然后他在类文件中创建了一个图像列表:

/**
 * Loads the layout and sets the initial set of images
 */
private void prepareLayout() {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.myprogressbar, null);
    addView(view);

    imageHolders = new ArrayList<ImageView>();
    imageHolders.add((ImageView) view.findViewById(R.id.imgOne));
    imageHolders.add((ImageView) view.findViewById(R.id.imgTwo));
    imageHolders.add((ImageView) view.findViewById(R.id.imgThree));

    // Prepare an array list of images to be animated
    images = new ArrayList<String>();

    images.add("progress_1");
    images.add("progress_2");
    images.add("progress_3");
    images.add("progress_4");
    images.add("progress_5");
    images.add("progress_6");
    images.add("progress_7");
    images.add("progress_8");
    images.add("progress_9");
}

然后他启动一个 hibernate 0.3 秒的线程并使用 handler.sendEmptyMessage(0); 调用处理程序,最后在处理程序中他完成图像的其余工作:

@Override
public void handleMessage(Message msg) {
    int currentImage = 0;
    int nextImage = 0;
    // Logic to change the images
    for (ImageView imageView : imageHolders) {
        currentImage = Integer.parseInt(imageView.getTag().toString());
        if (currentImage < 9) {
            nextImage = currentImage + 1;
        } else {
            nextImage = 1;
        }
        imageView.setTag("" + nextImage);
        imageView.setImageResource(getResources().getIdentifier(
                images.get(nextImage - 1), "drawable",
                "com.beanie.example"));
    }
    super.handleMessage(msg);
}

另请查看 herehere .

关于java - Android 中的自定义进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4581812/

相关文章:

java - 无法在 JDK7 上运行 Maven 3.6.3

java - 自定义 ListView 未完全显示(从底部裁剪)

java - 如何控制 Activity 的宽度或可见性

java - MyBatis Spring MVC 错误 : Invalid bound statement (not found)

java - 用于持久化内存队列的 JPA(或其他替代方案)

android - 美元符号后的正则表达式抓取数字

android - android ListView 中的数据重复

java - 匹配通配符严格,但找不到元素 'import' 的声明

c# - 解析 XML 数据时出错

Java Axis 客户端创建双安全 header