44

バーの中央にプログレスを表示する種類のCircluarDeterminateProgressBarを作成したいと思います。これを作成するデフォルトの方法はありますか、それとも独自のカスタムを作成する必要がありますか?

4

4 に答える 4

62

Androidの円形プログレスバーの詳細な例をブログdemonuts.comに書いています。完全なソースコードと説明もここで気に入っています。

これが、ライブラリのない純粋なコードで、円の内側にパーセンテージが表示された円形のプログレスバーを作成した方法です。

ここに画像の説明を入力してください

まず、というドローアブルファイルを作成しますcircular.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/secondaryProgress">
        <shape
            android:innerRadiusRatio="6"
            android:shape="ring"
            android:thicknessRatio="20.0"
            android:useLevel="true">


            <gradient
                android:centerColor="#999999"
                android:endColor="#999999"
                android:startColor="#999999"
                android:type="sweep" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <rotate
            android:fromDegrees="270"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="270">

            <shape
                android:innerRadiusRatio="6"
                android:shape="ring"
                android:thicknessRatio="20.0"
                android:useLevel="true">


                <rotate
                    android:fromDegrees="0"
                    android:pivotX="50%"
                    android:pivotY="50%"
                    android:toDegrees="360" />

                <gradient
                    android:centerColor="#00FF00"
                    android:endColor="#00FF00"
                    android:startColor="#00FF00"
                    android:type="sweep" />

            </shape>
        </rotate>
    </item>
</layer-list>

今あなたのactivity_main.xml 追加で次のように:

  <?xml version="1.0" encoding="utf-8"?>
<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="@color/dialog"
    tools:context="com.example.parsaniahardik.progressanimation.MainActivity">

    <ProgressBar

        android:id="@+id/circularProgressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:indeterminate="false"
        android:max="100"
        android:progress="50"
        android:layout_centerInParent="true"
        android:progressDrawable="@drawable/circular"
        android:secondaryProgress="100"
        />

    <ImageView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:background="@drawable/whitecircle"
        android:layout_centerInParent="true"/>

    <TextView
        android:id="@+id/tv"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:gravity="center"
        android:text="25%"
        android:layout_centerInParent="true"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="20sp" />

</RelativeLayout>

activity_main.xml私はパーセンテージの周りに白い背景を表示するために白い背景を持つ1つの円形の画像を使用しました。これが画像です:

ここに画像の説明を入力してください

この画像の色を変更して、パーセンテージテキストの周りにカスタム色を設定できます。

最後に次のコードを追加しますMainActivity.java

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.DecelerateInterpolator;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    int pStatus = 0;
    private Handler handler = new Handler();
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Resources res = getResources();
        Drawable drawable = res.getDrawable(R.drawable.circular);
        final ProgressBar mProgress = (ProgressBar) findViewById(R.id.circularProgressbar);
        mProgress.setProgress(0);   // Main Progress
        mProgress.setSecondaryProgress(100); // Secondary Progress
        mProgress.setMax(100); // Maximum Progress
        mProgress.setProgressDrawable(drawable);

      /*  ObjectAnimator animation = ObjectAnimator.ofInt(mProgress, "progress", 0, 100);
        animation.setDuration(50000);
        animation.setInterpolator(new DecelerateInterpolator());
        animation.start();*/

        tv = (TextView) findViewById(R.id.tv);
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (pStatus < 100) {
                    pStatus += 1;

                    handler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            mProgress.setProgress(pStatus);
                            tv.setText(pStatus + "%");

                        }
                    });
                    try {
                        // Sleep for 200 milliseconds.
                        // Just to display the progress slowly
                        Thread.sleep(8); //thread will take approx 1.5 seconds to finish
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

水平方向のプログレスバーを作成する場合は、次のリンクをたどってください。ソースコードを含む多くの貴重な例があります:
http ://www.skholingua.com/android-basic/user-interface/form-widgets/progressbar

于 2016-05-11T11:04:19.303 に答える
44

まず、次のように、progress_circle.xmlをres/drawableディレクトリに追加します。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:drawable="@drawable/progress_circular_background"/>
<item>
    <shape
        android:innerRadiusRatio="3.4"
        android:shape="ring"
        android:thicknessRatio="6.0" >
        <gradient
            android:endColor="#ffffffff"
            android:startColor="#ffffff"
            android:type="sweep"
            android:useLevel="true" />
    </shape>
</item>
<item>
    <rotate
        android:drawable="@drawable/progress_particle"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
</item>

</layer-list>

「progress_particle.png」と「progress_circular_background.png」(引用符付き)の両方のAndroidのデフォルトのドローアブルが欠落していたため、画像をGoogleで検索しました。あなたはおそらくそれらをカスタマイズしたいと思うでしょうが、彼らはあなたが始めるためにそうするでしょう。

次に、xmlレイアウトで:

<ProgressBar
    android:id="@+id/timer_progress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:indeterminate="false"
    android:max="60"
    android:progressDrawable="@drawable/progress_circle" />

秒タイマーに使用しているので、最大は60ですが、何か違うものがあるかもしれません。

秘訣は、循環的な進行であっても、style = "?android:attr/progressBarStyleHorizo​​ntal"を使用する必要があるということです。

于 2012-10-15T23:24:46.517 に答える
8

質問の確定部分について、マテリアルデザインライブラリが確定円形プログレスバーをサポートするようになりました。

<com.google.android.material.progressindicator.CircularProgressIndicator
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

詳細については、こちらを参照してください。

他の回答は、進行状況ラベルを中央に挿入する方法に役立つ場合があります。

于 2021-01-08T09:25:08.820 に答える
3

確定的な循環進行状況インジケーターの例

<com.google.android.material.progressindicator.CircularProgressIndicator
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:progress="75"
  app:indicatorColor="#FF0000"
  app:indicatorSize="100dp"
  app:trackColor="#D3D3D3"
  app:trackThickness="10dp" />

参考文献

于 2021-06-14T14:14:10.580 に答える