Computer >> Máy Tính >  >> Lập trình >> Android

Làm thế nào để tạo thanh tiến trình tùy chỉnh vòng kết nối trong Android?


Ví dụ này giải thích cách tôi tạo thanh tiến trình tùy chỉnh vòng kết nối trong android.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="8dp"
   tools:context=".MainActivity">
<ProgressBar
   android:id="@+id/circularProgressbar"
   style="?android:attr/progressBarStyleHorizontal"
   android:layout_width="250dp"
   android:layout_height="250dp"
   android:layout_centerInParent="true"
   android:indeterminate="false"
   android:max="100"
   android:progress="50"
   android:secondaryProgress="100" />
<TextView
   android:id="@+id/textView"
   android:layout_width="250dp"
   android:layout_height="250dp"
   android:gravity="center"
   android:text="25%"
   android:layout_centerInParent="true"
   android:textColor="@color/colorPrimaryDark"
   android:textSize="24sp" />
</RelativeLayout>

Bước 3 - Tạo tệp tài nguyên có thể vẽ được (roundprogressbar.xml) và thêm mã sau -

<layer-list xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   <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">
            <gradient
               android:centerColor="#00FF00"
               android:endColor="#00FF00"
               android:startColor="#00FF00"
               android:type="sweep" />
               <rotate
                  android:fromDegrees="0"
                  android:pivotX="50%"
                  android:pivotY="50%"
                  android:toDegrees="360" />
         </shape>
      </rotate>
   </item>
   <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>
</layer-list>

Bước 4 - Thêm mã sau vào src / MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   int status = 0;
   private Handler handler = new Handler();
   TextView textView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Resources resources = getResources();
      Drawable drawable = resources.getDrawable(R.drawable.circularprogressbar);
      final ProgressBar progressBar = findViewById(R.id.circularProgressbar);
      progressBar.setProgress(0);
      progressBar.setSecondaryProgress(100);
      progressBar.setMax(100);
      progressBar.setProgressDrawable(drawable);
      textView = findViewById(R.id.textView);
      new Thread(new Runnable() {
         @Override
         public void run() {
            while (status < 100) {
               status += 1;
               handler.post(new Runnable() {
                  @Override
                  public void run() {
                     progressBar.setProgress(status);
                     textView.setText(String.format("%d%%", status));
                  }
               });
               try {
                  Thread.sleep(16);
               }
               catch (InterruptedException e) {
                  e.printStackTrace();
               }
            }
         }
      }).start();
   }
}

Bước 5 - Thêm mã sau vào androidManifest.xml

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ studio android, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào Run Làm thế nào để tạo thanh tiến trình tùy chỉnh vòng kết nối trong Android? biểu tượng từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm thế nào để tạo thanh tiến trình tùy chỉnh vòng kết nối trong Android?