Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中自定义进度条_Android_Android Progressbar - Fatal编程技术网

如何在Android中自定义进度条

如何在Android中自定义进度条,android,android-progressbar,Android,Android Progressbar,我正在开发一个应用程序,我想在其中显示一个ProgressBar,但我想替换默认的安卓ProgressBar 那么我如何定制进度条 我需要一些图形和动画吗 我读了下面的帖子,但没能让它发挥作用: 在您的xml中 <ProgressBar android:id="@+id/progressBar1" android:layout_width="fill_parent" android:layout_height="wrap_content"

我正在开发一个应用程序,我想在其中显示一个
ProgressBar
,但我想替换默认的安卓
ProgressBar

那么我如何定制
进度条

我需要一些图形和动画吗

我读了下面的帖子,但没能让它发挥作用:

在您的xml中

<ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@style/CustomProgressBar" 
        android:layout_margin="5dip" />
custom\u progress\u bar\u horizontal
是存储在drawable文件夹中的xml,用于定义自定义进度条。有关更多详细信息,请参见此


我希望这将对您有所帮助。

自定义
进度条需要定义进度条背景和进度的属性

res->drawable
文件夹中创建名为
customprogressbar.XML
的XML文件:

自定义_progressbar.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Define the background properties like color etc -->
    <item android:id="@android:id/background">
    <shape>
        <gradient
                android:startColor="#000001"
                android:centerColor="#0b131e"
                android:centerY="1.0"
                android:endColor="#0d1522"
                android:angle="270"
        />
    </shape>
   </item>

  <!-- Define the progress properties like start color, end color etc -->
  <item android:id="@android:id/progress">
    <clip>
        <shape>
            <gradient
                android:startColor="#007A00"
                android:centerColor="#007A00"
                android:centerY="1.0"
                android:endColor="#06101d"
                android:angle="270"
            />
        </shape>
    </clip>
    </item>
</layer-list> 
在运行时执行以下操作

// Get the Drawable custom_progressbar                     
    Drawable draw=res.getDrawable(R.drawable.custom_progressbar);
// set the drawable as progress drawable
    progressBar.setProgressDrawable(draw);

编辑:更正了xml布局

自定义progressbar的颜色,即在微调器类型的情况下,需要一个xml文件,并在各自的java文件中启动代码。

创建一个xml文件并将其命名为progressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    tools:context=".Radio_Activity" >

    <LinearLayout
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ProgressBar
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ProgressBar>
    </LinearLayout>

</LinearLayout>

如果出现这样复杂的
ProgressBar

android:clipOrientation="horizontal"

使用
ClipDrawable

注意:在本例中,我没有使用
ProgressBar
。我已经做到了 通过使用
动画
剪裁图像,可以实现此功能

一种
Drawable
,根据该
Drawable
的当前级别值剪辑另一个
Drawable
。您可以根据标高控制子级
可绘制
在宽度和高度上被剪裁的程度,还可以通过重力控制子级在整个容器中的放置位置<代码>最常用于实现进度条之类的功能,方法是使用
setLevel()
增加可绘制的级别

注意:当标高为0时,可拉伸部分完全被剪裁,不可见 当等级为10000时完全显示

我用这两张图片制作了这个
CustomProgressBar

scall.png

ballon_progress.png

MainActivity.java


您可以从下载完整的演示。

如果您想在代码中执行此操作,下面是一个示例:

pd = new ProgressDialog(MainActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
pd.getWindow().setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
TextView tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setTextSize(20);
tv.setText("Waiting...");
pd.setCustomTitle(tv);
pd.setIndeterminate(true);
pd.show();

使用TextView,您可以选择更改文本的颜色、大小和字体。否则,您可以像往常一样调用setMessage()。

创建类似hotstar的自定义ProgressBar

  • 在布局文件上添加进度条,并使用可绘制文件设置UndeterminatedRavable
  • activity_main.xml

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/player_progressbar"
        android:indeterminateDrawable="@drawable/custom_progress_bar"
        />
    
    
    
  • 在res\drawable中创建新的xml文件
  • 自定义_progress_bar.xml

    <?xml version="1.0" encoding="utf-8"?>
        <rotate  xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="1080" >
    
        <shape
            android:innerRadius="35dp"
            android:shape="ring"
            android:thickness="3dp"
            android:useLevel="false" >
           <size
               android:height="80dp"
               android:width="80dp" />
    
           <gradient
                android:centerColor="#80b7b4b2"
                android:centerY="0.5"
                android:endColor="#f4eef0"
                android:startColor="#00938c87"
                android:type="sweep"
                android:useLevel="false" />
        </shape>
    
    </rotate>
    

    在Android中创建自定义进度条的最简单方法:

  • 初始化并显示对话框:

    MyProgressDialog progressdialog = new MyProgressDialog(getActivity());
    progressdialog.show();
    
  • 创建方法:

    public class MyProgressDialog extends AlertDialog {
          public MyProgressDialog(Context context) {
              super(context);
              getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
          }
    
          @Override
          public void show() {
              super.show();
              setContentView(R.layout.dialog_progress);
          }
    }
    
  • 创建布局XML:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@android:color/transparent"
      android:clickable="true">
    
        <RelativeLayout
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerInParent="true">
    
          <ProgressBar
             android:id="@+id/progressbarr"
             android:layout_width="@dimen/eightfive"
             android:layout_height="@dimen/eightfive"
             android:layout_centerInParent="true"
            android:indeterminateDrawable="@drawable/progresscustombg" />
    
          <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerHorizontal="true"
             android:layout_below="@+id/progressbarr"
             android:layout_marginTop="@dimen/_3sdp"
             android:textColor="@color/white"
             android:text="Please wait"/>
        </RelativeLayout>
    </RelativeLayout>
    
    
    
  • 创建shape progresscustombg.xml并放置res/drawable:

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
      android:fromDegrees="0"
      android:pivotX="50%"
      android:pivotY="50%"
      android:toDegrees="360" >
    
        <shape
           android:innerRadiusRatio="3"
           android:shape="ring"
           android:thicknessRatio="20"
           android:useLevel="false" >
            <size
                android:height="@dimen/eightfive"
                android:width="@dimen/eightfive" />
    
            <gradient
                android:centerY="0.50"
                android:endColor="@color/color_green_icash"
                android:startColor="#FFFFFF"
                android:type="sweep"
                android:useLevel="false" />
        </shape>
    
    </rotate>
    
    
    

  • 有两种类型的进度条称为确定进度条(固定持续时间)和不确定进度条(未知持续时间)

    这两种进度条类型的可绘制项都可以通过将可绘制项定义为xml资源来定制。有关进度条样式和自定义的详细信息,请访问

    自定义固定或水平进度条:

    下面的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"
            android:gravity="center_vertical|fill_horizontal">
            <shape android:shape="rectangle"
                android:tint="?attr/colorControlNormal">
                <corners android:radius="8dp"/>
                <size android:height="20dp" />
                <solid android:color="#90caf9" />
            </shape>
        </item>
        <item android:id="@android:id/progress"
            android:gravity="center_vertical|fill_horizontal">
            <scale android:scaleWidth="100%">
                <shape android:shape="rectangle"
                    android:tint="?attr/colorControlActivated">
                    <corners android:radius="8dp"/>
                    <size android:height="20dp" />
                    <solid android:color="#b9f6ca" />
                </shape>
            </scale>
        </item>
    </layer-list>
    
    
    
    自定义不确定进度条

    下面的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"
            android:top="16dp"
            android:bottom="16dp">
                    <rotate
                        android:fromDegrees="45"
                        android:pivotX="50%"
                        android:pivotY="50%"
                        android:toDegrees="315">
                        <shape android:shape="rectangle">
                            <size
                                android:width="80dp"
                                android:height="80dp" />
                            <stroke
                                android:width="6dp"
                                android:color="#b71c1c" />
                        </shape>
                    </rotate>           
        </item>
    </layer-list>
    

    使用自定义可拉伸:

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:drawable="@drawable/my_drawable"
    android:pivotX="50%"
    android:pivotY="50%" />
    
    
    

    @drawable/loading
    src\main\res\drawable\loading.gif
    文件,它的大小是200乘200

    什么是res??res.getDrawable不可用found@hirengamitres是“资源”。您的错误可能表明找不到该文件。“C:/whereYourProjectFolderIs/res/drawable/customprogressbar.xml”@Johnson,res代表资源。如果将该行替换为Drawable draw=getResources().getDrawable(R.Drawable.custom_progressbar);它应该可以工作。为什么有必要在xml文件上设置可在运行时绘制的进度?@NicolásArias可能出于任何可能的原因(例如,从“等待响应”中的绿色更改为“等待错误处理”中的红色),您可能想更改其外观试试这个自定义进度条有没有办法为整个应用程序设置颜色过滤器,我的意思是,在每个进度条中,比如在样式或其他方面设置?thnxcode非常有趣,但它给了我img对象上的nullpointer异常:查找小部件的Id,您的异常将得到解决。这是我见过的最好的答案。工作起来很有魅力。谢谢
    public class MyProgressDialog extends AlertDialog {
          public MyProgressDialog(Context context) {
              super(context);
              getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
          }
    
          @Override
          public void show() {
              super.show();
              setContentView(R.layout.dialog_progress);
          }
    }
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@android:color/transparent"
      android:clickable="true">
    
        <RelativeLayout
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerInParent="true">
    
          <ProgressBar
             android:id="@+id/progressbarr"
             android:layout_width="@dimen/eightfive"
             android:layout_height="@dimen/eightfive"
             android:layout_centerInParent="true"
            android:indeterminateDrawable="@drawable/progresscustombg" />
    
          <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerHorizontal="true"
             android:layout_below="@+id/progressbarr"
             android:layout_marginTop="@dimen/_3sdp"
             android:textColor="@color/white"
             android:text="Please wait"/>
        </RelativeLayout>
    </RelativeLayout>
    
    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
      android:fromDegrees="0"
      android:pivotX="50%"
      android:pivotY="50%"
      android:toDegrees="360" >
    
        <shape
           android:innerRadiusRatio="3"
           android:shape="ring"
           android:thicknessRatio="20"
           android:useLevel="false" >
            <size
                android:height="@dimen/eightfive"
                android:width="@dimen/eightfive" />
    
            <gradient
                android:centerY="0.50"
                android:endColor="@color/color_green_icash"
                android:startColor="#FFFFFF"
                android:type="sweep"
                android:useLevel="false" />
        </shape>
    
    </rotate>
    
     <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@android:id/background"
            android:gravity="center_vertical|fill_horizontal">
            <shape android:shape="rectangle"
                android:tint="?attr/colorControlNormal">
                <corners android:radius="8dp"/>
                <size android:height="20dp" />
                <solid android:color="#90caf9" />
            </shape>
        </item>
        <item android:id="@android:id/progress"
            android:gravity="center_vertical|fill_horizontal">
            <scale android:scaleWidth="100%">
                <shape android:shape="rectangle"
                    android:tint="?attr/colorControlActivated">
                    <corners android:radius="8dp"/>
                    <size android:height="20dp" />
                    <solid android:color="#b9f6ca" />
                </shape>
            </scale>
        </item>
    </layer-list>
    
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@android:id/progress"
            android:top="16dp"
            android:bottom="16dp">
                    <rotate
                        android:fromDegrees="45"
                        android:pivotX="50%"
                        android:pivotY="50%"
                        android:toDegrees="315">
                        <shape android:shape="rectangle">
                            <size
                                android:width="80dp"
                                android:height="80dp" />
                            <stroke
                                android:width="6dp"
                                android:color="#b71c1c" />
                        </shape>
                    </rotate>           
        </item>
    </layer-list>
    
    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:drawable="@drawable/my_drawable"
    android:pivotX="50%"
    android:pivotY="50%" />
    
    <ProgressBar
            android:id="@+id/progressBar"
            android:indeterminateDrawable="@drawable/progress_circle"
    ...
    />
    
    <ProgressBar
        android:indeterminateDrawable="@drawable/loading"
        style="?android:attr/progressBarStyleLarge"
        android:layout_gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:scaleY="0.5"
        android:scaleX="0.5"
        android:id="@+id/progressBarGallery"/>