簡體   English   中英

具有透明背景的自定義圓形圖像進度欄

[英]Custom circular image progress bar with transparent background

我有計時器的圓形圖片:

在此處輸入圖片說明

我想創建一個包含此圖片的圓形進度條,這將使其像時鍾一樣緩慢地變空,直到完全消失(如動畫)。

例如,在一半的時間之后,我將其定義為:

在此處輸入圖片說明

現在,我以這種方式進行操作:我將計時器圖像作為圖像放置在屏幕上,並在其上放置了規則的圓形進度條,並使用顏色作為隱藏圖像的背景顏色(這樣我就得到了預期的效果) 。

但是現在我在應用程序的背景中只有幾種顏色時遇到了問題,因為我無法使進度條具有與背景相同的顏色,所以我在尋找一種新的制作方法,例如自定義進度條由具有透明背景的計時器圖像組成。

請幫助我如何更改代碼以獲取它:

ProgressBar XML:
<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:progress="100"
    android:rotation="90"
    style="?android:progressBarStyleHorizontal"
    android:progressDrawable="@drawable/prog_blue"/>

Prog_blue(progressBar的可繪制對象)-藍色是應用程序的背景色:

<?xml version="1.0" encoding="utf-8"?>
<scale
    android:fromDegrees="270"
    android:pivotX="50%"
    android:pivotY="50%"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <shape
        android:shape="ring"
        android:innerRadiusRatio="2.7"
        android:thickness="23dp"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:startColor="@color/prog_blue"
            android:endColor="@color/prog_blue"
            android:type="sweep"
            android:useLevel="false"/>
    </shape>
</scale>

您可以創建自定義View來實現

這是我的代碼:

ClockProgressBar.java

 import android.animation.ValueAnimator;
 import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.Path;
 import android.support.annotation.Nullable;
 import android.util.AttributeSet;
 import android.view.View;


 public class ClockProgressBar extends View {
 Paint tickPaint;
 private int shortTickLen=10;
 private int longTickLen=20;
 private int finalWidth=0;
 private int finalHeight=0;
 private int r=0;
 private Path ticksPath;
 int tickNumbers=60;
 private ValueAnimator animator;

 public ClockProgressBar(Context context) {
    super(context);
    ini();
 }

 public ClockProgressBar(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    ini();
 }

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    drawIntervals(canvas);

 }

void drawIntervals(Canvas canvas){


    double cX = getX()+finalWidth/2;
    double cY = getY()+finalHeight/2;
    ticksPath=new Path();
    for ( int i=1; i<= tickNumbers; i++){
        int len = shortTickLen;
        if ( i % 15 == 0 ){
            len = longTickLen;
        }else if ( i % 5 == 0 ){
            len = longTickLen;
        }
        double di = (double)i;
        double angleFrom12 = di/60.0*2.0*Math.PI;
        double angleFrom3 = Math.PI/2.0-angleFrom12;
        ticksPath.moveTo(
                (float)(cX+Math.cos(angleFrom3)*r),
                (float)(cY-Math.sin(angleFrom3)*r)
        );

        ticksPath.lineTo(
                (float)(cX+Math.cos(angleFrom3)*(r-len)),
                (float)(cY-Math.sin(angleFrom3)*(r-len))
        );
    }
    canvas.drawPath(ticksPath,tickPaint);


}

void ini(){
    tickPaint=new Paint();
    tickPaint.setStrokeCap(Paint.Cap.BUTT);
    tickPaint.setStrokeJoin(Paint.Join.ROUND);
    tickPaint.setAntiAlias(true);
    tickPaint.setStyle(Paint.Style.STROKE);
    tickPaint.setStrokeWidth(8);
    tickPaint.setStrokeMiter(2f);
    tickPaint.setColor(Color.BLACK);


}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    finalWidth = getMeasuredWidth() / 2;
    finalHeight = getMeasuredWidth() / 2;
    r = (finalWidth / 2);
    setMeasuredDimension(finalWidth, finalHeight );
}

public void startAnimation(){
     animator = ValueAnimator.ofInt(60, 0);
    animator.setDuration(5000);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        public void onAnimationUpdate(ValueAnimator animation) {
            tickNumbers= (int) animation.getAnimatedValue();
            invalidate();
        }
    });
    animator.setRepeatCount(Integer.MAX_VALUE);
    animator.start();
}
public void stopAnimation(){
    if (animator!=null){
        animator.cancel();
    }
    setVisibility(GONE);

}




  }

並在xml中使用

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<<Your Package Name>.ClockProgressBar
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/progress"/>

</LinearLayout>

並在您的活動中調用它,並使用startAnimation()stopAnimation()顯示和關閉View

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ClockProgressBar progressBar=findViewById(R.id.progress);
    progressBar.startAnimation();
}
}

最終結果

在此處輸入圖片說明 在此處輸入圖片說明 希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM