Android自定義View實(shí)現(xiàn)圓形加載進(jìn)度條
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)圓形加載進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
話不多說(shuō),咱們直接看代碼
首先第一種:
1、創(chuàng)建自定義View類
public class MyRelative extends View { ?? ? ? public MyRelative(Context context) { ? ? ? ? this(context, null); //手動(dòng)改成this... ? ? } ? ? ? public MyRelative(Context context, @Nullable AttributeSet attrs) { ? ? ? ? this(context, attrs, 0);//手動(dòng)改成this... ? ? } ? ? ? @SuppressLint("ResourceAsColor") ? ? public MyRelative(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ?? ? ? } }
2、自定義屬性,(在values文件夾下創(chuàng)建一個(gè)XML,取名為atts_circle_view.xml)
<?xml version="1.0" encoding="utf-8"?> <resources> ? ? <declare-styleable name="MyRelative"> //這個(gè)name最好和你創(chuàng)建的自定義View類名一樣 ? ? ? ? <!--外圓顏色--> ? ? ? ? <attr name="outer_color" format="color" /> ? ? ? ? <!--內(nèi)圓顏色--> ? ? ? ? <attr name="inner_color" format="color" /> ? ? ? ? <!--圓形寬度--> ? ? ? ? <attr name="border_width" format="dimension" /> ? ? ? ? <!--字體顏色--> ? ? ? ? <attr name="step_text_color" format="color" /> ? ? ? ? <!--字體大小--> ? ? ? ? <attr name="step_text_size" format="dimension" /> ? ? ? ? <!--步數(shù)最大值--> ? ? ? ? <attr name="max_step" format="integer"/> ? ? ? ? <!--當(dāng)前步數(shù)--> ? ? ? ? <attr name="curren_step" format="integer"/> ? ? </declare-styleable> </resources>
3、在第三個(gè)構(gòu)造方法中得到自定義屬性
@SuppressLint("ResourceAsColor") ? ? public MyRelative(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRelative); ? ? ? ? //圓弧寬度 ? ? ? ? mBorderWidth = (int) typedArray.getDimension(R.styleable.MyRelative_border_width, 10); ? ? ? ? //外圓弧顏色 ? ? ? ? mOuterColor = typedArray.getColor(R.styleable.MyRelative_outer_color, mOuterColor); ? ? ? ? //內(nèi)圓弧顏色 ? ? ? ? mInnerColor = typedArray.getInteger(R.styleable.MyRelative_inner_color, mInnerColor); ? ? ? ? //字體顏色 ? ? ? ? mTextColor = typedArray.getColor(R.styleable.MyRelative_step_text_color, mTextColor); ? ? ? ? //字體大小 ? ? ? ? mTextSize = (int) typedArray.getDimensionPixelSize(R.styleable.MyRelative_step_text_size, mTextSize); ? ? ? ? //最大步數(shù) ? ? ? ? mMaxStep = typedArray.getInteger(R.styleable.MyRelative_max_step, 10000); ? ? ? ? //當(dāng)前步數(shù) ? ? ? ? mCurrentStep = typedArray.getInteger(R.styleable.MyRelative_curren_step, 8000); ? ? ? ? typedArray.recycle(); }
4、重寫onMeasure方法(測(cè)量view大小)
?@Override ? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec); ? ? ? ? //測(cè)量寬高 ? ? ? ? int width = MeasureSpec.getSize(widthMeasureSpec); ? ? ? ? int height = MeasureSpec.getSize(heightMeasureSpec); ? ? ? ? //將控件截成正方形 ? ? ? ? //三目運(yùn)算符取長(zhǎng)度短的一邊作為寬高 ? ? ? ? setMeasuredDimension(width > height ? height : width, width > height ? height : width); ? ? }
5、重寫onDraw方法(繪制)
?@Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? super.onDraw(canvas); ? ? ? ? //繪制內(nèi)圓弧 ? ? ? ? int center = getWidth() / 2; ? ? ? ? int r = (getWidth() - mBorderWidth) / 2; ? ? ? ? RectF rectF = new RectF(mBorderWidth / 2, mBorderWidth / 2, center + r, center + r); ? ? ? ? canvas.drawArc(rectF, 135, 270, false, mInnerPaint); ? ? ? ? //繪制外圓弧 ? ? ? ? if (mMaxStep == 0) { ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? float radio = (float) mCurrentStep / mMaxStep; ? ? ? ? canvas.drawArc(rectF, 135, 270 * radio, false, mOuterPaint); ? ? ? ? //文字 ? ? ? ? String mText = mCurrentStep + ""; ? ? ? ? Rect rect = new Rect(); ? ? ? ? mTextPaint.getTextBounds(mText, 0, mText.length(), rect); ? ? ? ? int dx = getWidth() / 2 - rect.width() / 2; ? ? ? ? Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt(); ? ? ? ? int dy = fontMetricsInt.bottom - fontMetricsInt.top; ? ? ? ? int baseLine = getHeight() / 2 + dy / 2; ? ? ? ? canvas.drawText(mText, dx, getHeight() / 2 + rect.height() / 2, mTextPaint); ? ? }
6、要想效果炫酷怎么能少了動(dòng)畫
寫一個(gè)方法可以直接在activity中調(diào)用
public void setAnimator(int mMaxStep, int mCurrentStep, int duration) { ? ? ? ? this.mMaxStep = mMaxStep; ? ? ? ? ValueAnimator animator = ObjectAnimator.ofFloat(0, mCurrentStep); ? ? ? ? animator.setInterpolator(new DecelerateInterpolator()); ? ? ? ? animator.setDuration(duration); ? ? ? ? animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onAnimationUpdate(ValueAnimator valueAnimator) { ? ? ? ? ? ? ? ? float value = (float) animator.getAnimatedValue(); ? ? ? ? ? ? ? ? setmCurrentStep((int) value); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? animator.start(); ? ? }
下面附上全部代碼
package com.example.customviewdome; ? import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; import android.view.animation.DecelerateInterpolator; ? import androidx.annotation.Nullable; ? public class MyRelative extends View { ? ? //圓弧寬度 ? ? private int mBorderWidth; ? ? //外圓弧默認(rèn)顏色 ? ? private int mOuterColor = R.color.salmon; ? ? //內(nèi)圓弧默認(rèn)顏色 ? ? private int mInnerColor = R.color.sandybrown; ? ? //字體默認(rèn)顏色 ? ? private int mTextColor = R.color.salmon; ? ? //字體默認(rèn)大小 ? ? private int mTextSize = 40; ? ? //步數(shù) ? ? private int mCurrentStep; ? ? //創(chuàng)建內(nèi)圓畫筆 ? ? private Paint mInnerPaint; ? ? //創(chuàng)建外圓畫筆 ? ? private Paint mOuterPaint; ? ? //創(chuàng)建文字畫筆 ? ? private Paint mTextPaint; ? ? //最大步數(shù)值 ? ? private int mMaxStep; ? ? ? public void setmCurrentStep(int mCurrentStep) { ? ? ? ? this.mCurrentStep = mCurrentStep; ? ? ? ? invalidate(); ? ? } ? ? ? public void setmMaxStep(int mMaxStep) { ? ? ? ? this.mMaxStep = mMaxStep; ? ? } ? ? ? public MyRelative(Context context) { ? ? ? ? this(context, null); ? ? } ? ? ? public MyRelative(Context context, @Nullable AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? ? //1、分析需求 ? ? //2、自定義屬性 ? ? //3、獲得自定義屬性 ? ? //4、重寫onMeasure ? ? //5、繪制 ? ? //6、其他(動(dòng)畫等等) ? ? @SuppressLint("ResourceAsColor") ? ? public MyRelative(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRelative); ? ? ? ? //圓弧寬度 ? ? ? ? mBorderWidth = (int) typedArray.getDimension(R.styleable.MyRelative_border_width, 10); ? ? ? ? //外圓弧顏色 ? ? ? ? mOuterColor = typedArray.getColor(R.styleable.MyRelative_outer_color, mOuterColor); ? ? ? ? //內(nèi)圓弧顏色 ? ? ? ? mInnerColor = typedArray.getInteger(R.styleable.MyRelative_inner_color, mInnerColor); ? ? ? ? //字體顏色 ? ? ? ? mTextColor = typedArray.getColor(R.styleable.MyRelative_step_text_color, mTextColor); ? ? ? ? //字體大小 ? ? ? ? mTextSize = (int) typedArray.getDimensionPixelSize(R.styleable.MyRelative_step_text_size, mTextSize); ? ? ? ? //最大步數(shù) ? ? ? ? mMaxStep = typedArray.getInteger(R.styleable.MyRelative_max_step, 10000); ? ? ? ? //當(dāng)前步數(shù) ? ? ? ? mCurrentStep = typedArray.getInteger(R.styleable.MyRelative_curren_step, 8000); ? ? ? ? typedArray.recycle(); ? ? ? ? //創(chuàng)建畫筆 ? ? ? ? mInnerPaint = new Paint(); ? ? ? ? mInnerPaint.setStyle(Paint.Style.STROKE); ? ? ? ? mInnerPaint.setAntiAlias(true); ? ? ? ? mInnerPaint.setColor(mInnerColor); ? ? ? ? mInnerPaint.setStrokeWidth(mBorderWidth); ? ? ? ? mInnerPaint.setStrokeCap(Paint.Cap.ROUND); ? ? ? ? //創(chuàng)建畫筆 ? ? ? ? mOuterPaint = new Paint(); ? ? ? ? mOuterPaint.setStyle(Paint.Style.STROKE); ? ? ? ? mOuterPaint.setAntiAlias(true); ? ? ? ? mOuterPaint.setColor(mOuterColor); ? ? ? ? mOuterPaint.setStrokeWidth(mBorderWidth); ? ? ? ? mOuterPaint.setStrokeCap(Paint.Cap.ROUND); ? ? ? ? //創(chuàng)建文字畫筆 ? ? ? ? mTextPaint = new Paint(); ? ? ? ? mTextPaint.setAntiAlias(true); ? ? ? ? mTextPaint.setColor(mTextColor); ? ? ? ? mTextPaint.setTextSize(mTextSize); ? ? } ? ? ? @Override ? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec); ? ? ? ? //測(cè)量寬高 ? ? ? ? int width = MeasureSpec.getSize(widthMeasureSpec); ? ? ? ? int height = MeasureSpec.getSize(heightMeasureSpec); ? ? ? ? //將控件截成正方形 ? ? ? ? //三目運(yùn)算符取長(zhǎng)度短的一邊作為寬高 ? ? ? ? setMeasuredDimension(width > height ? height : width, width > height ? height : width); ? ? } ? ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? super.onDraw(canvas); ? ? ? ? //繪制內(nèi)圓弧 ? ? ? ? int center = getWidth() / 2; ? ? ? ? int r = (getWidth() - mBorderWidth) / 2; ? ? ? ? RectF rectF = new RectF(mBorderWidth / 2, mBorderWidth / 2, center + r, center + r); ? ? ? ? canvas.drawArc(rectF, 135, 270, false, mInnerPaint); ? ? ? ? //繪制外圓弧 ? ? ? ? if (mMaxStep == 0) { ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? float radio = (float) mCurrentStep / mMaxStep; ? ? ? ? canvas.drawArc(rectF, 135, 270 * radio, false, mOuterPaint); ? ? ? ? //文字 ? ? ? ? String mText = mCurrentStep + ""; ? ? ? ? Rect rect = new Rect(); ? ? ? ? mTextPaint.getTextBounds(mText, 0, mText.length(), rect); ? ? ? ? int dx = getWidth() / 2 - rect.width() / 2; ? ? ? ? Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt(); ? ? ? ? int dy = fontMetricsInt.bottom - fontMetricsInt.top; ? ? ? ? int baseLine = getHeight() / 2 + dy / 2; ? ? ? ? canvas.drawText(mText, dx, getHeight() / 2 + rect.height() / 2, mTextPaint); ? ? } ? ? ? public void setAnimator(int mMaxStep, int mCurrentStep, int duration) { ? ? ? ? this.mMaxStep = mMaxStep; ? ? ? ? ValueAnimator animator = ObjectAnimator.ofFloat(0, mCurrentStep); ? ? ? ? animator.setInterpolator(new DecelerateInterpolator()); ? ? ? ? animator.setDuration(duration); ? ? ? ? animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onAnimationUpdate(ValueAnimator valueAnimator) { ? ? ? ? ? ? ? ? float value = (float) animator.getAnimatedValue(); ? ? ? ? ? ? ? ? setmCurrentStep((int) value); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? animator.start(); ? ? } }
第二種圓形進(jìn)度條
步驟和以上差不多,下面直接貼出源碼
自定義屬性
<?xml version="1.0" encoding="utf-8"?> <resources> ? ? <declare-styleable name="CircleLoadingView"> ? ? ? ? <!--內(nèi)圓顏色--> ? ? ? ? <attr name="in_color" format="color" /> ? ? ? ? <!--外圓顏色--> ? ? ? ? <attr name="out_color" format="color" /> ? ? ? ? <!--字體顏色--> ? ? ? ? <attr name="text_color" format="color" /> ? ? ? ? <!--字體大小--> ? ? ? ? <attr name="text_size" format="dimension" /> ? ? ? ? <!--圓圈顏色--> ? ? ? ? <attr name="dot_color" format="color" /> ? ? </declare-styleable> </resources>
源碼
package com.example.customviewdome; ? import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; import android.view.animation.DecelerateInterpolator; ? import androidx.annotation.Nullable; ? public class CircleLoadingView extends View { ? ? private Context mContext; ? ? //內(nèi)圓顏色 ? ? private int mInnerColor; ? ? //外圓顏色 ? ? private int mOuterColor; ? ? //圓點(diǎn)顏色 ? ? private int mDotColor; ? ? //字體顏色 ? ? private int mTextColor; ? ? //字體大小 ? ? private int mTextSize; ? ? //創(chuàng)建內(nèi)圓畫筆 ? ? private Paint mInnerPaint; ? ? //view的寬度 ? ? private int mWidth; ? ? //view的高度 ? ? private int mHeight; ? ? //當(dāng)前進(jìn)度 ? ? private int mProgress = 0; ? ? //創(chuàng)建文字畫筆 ? ? private Paint mTextPaint; ? ? //創(chuàng)建小圓圈畫筆 ? ? private Paint mDotPaint; ? ? //小圓圈起點(diǎn)位置 ? ? private int mDotProgress; ? ? ? //仿華為圓形加載框 ? ? public CircleLoadingView(Context context) { ? ? ? ? this(context, null); ? ? } ? ? ? public CircleLoadingView(Context context, @Nullable AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? ? //1、自定義屬性 ? ? //2、測(cè)量控件大小 ? ? //3、繪制內(nèi)圓 ? ? //4、繪制外圓 ? ? @SuppressLint("ResourceAsColor") ? ? public CircleLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? mContext = getContext(); ? ? ? ? TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleLoadingView); ? ? ? ? mInnerColor = typedArray.getColor(R.styleable.CircleLoadingView_in_color, R.color.antiquewhite); ? ? ? ? mOuterColor = typedArray.getColor(R.styleable.CircleLoadingView_out_color, R.color.aqua); ? ? ? ? mTextColor = typedArray.getColor(R.styleable.CircleLoadingView_text_color, R.color.aqua); ? ? ? ? mDotColor = typedArray.getColor(R.styleable.CircleLoadingView_dot_color, R.color.blueviolet); ? ? ? ? mTextSize = typedArray.getDimensionPixelSize(R.styleable.CircleLoadingView_text_size, 20); ? ? ? ? typedArray.recycle(); ? ? ? ? //創(chuàng)建畫筆 ? ? ? ? mInnerPaint = new Paint(); ? ? ? ? mInnerPaint.setAntiAlias(true); ? ? ? ? mInnerPaint.setColor(mInnerColor); ? ? ? ? mInnerPaint.setStrokeWidth(DensityUtil.dip2px(mContext, 3)); ? ? ? ? mInnerPaint.setStrokeCap(Paint.Cap.ROUND); ? ? ? ? mInnerPaint.setStyle(Paint.Style.STROKE); ? ? ? ? //創(chuàng)建文字畫筆 ? ? ? ? mTextPaint = new Paint(); ? ? ? ? mTextPaint.setAntiAlias(true); ? ? ? ? mTextPaint.setTextSize(mTextSize); ? ? ? ? mTextPaint.setColor(mTextColor); ? ? ? ? mTextPaint.setStyle(Paint.Style.STROKE); ? ? ? ? //創(chuàng)建小圓圈畫筆 ? ? ? ? mDotPaint = new Paint(); ? ? ? ? mDotPaint.setAntiAlias(true); ? ? ? ? mDotPaint.setStrokeWidth(DensityUtil.dip2px(mContext, 10)); ? ? ? ? mDotPaint.setStyle(Paint.Style.FILL); ? ? ? ? mDotPaint.setColor(mDotColor); ? ? } ? ? ? @Override ? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec); ? ? ? ? int widthMode = MeasureSpec.getMode(widthMeasureSpec); ? ? ? ? int heightMode = MeasureSpec.getMode(heightMeasureSpec); ? ? ? ? int widthSize = MeasureSpec.getSize(widthMeasureSpec); ? ? ? ? int heightSize = MeasureSpec.getSize(heightMeasureSpec); ? ? ? ? //獲取寬度 ? ? ? ? if (widthMode == MeasureSpec.EXACTLY) { ? ? ? ? ? ? //當(dāng)寬度為精準(zhǔn)值或match_parent時(shí)直接使用 ? ? ? ? ? ? mWidth = widthSize; ? ? ? ? } else { ? ? ? ? ? ? //當(dāng)寬度為wrap_content設(shè)置控件大小為120dp ? ? ? ? ? ? mWidth = DensityUtil.dip2px(mContext, 220); ? ? ? ? } ? ? ? ? //獲取高度 ? ? ? ? if (heightMode == MeasureSpec.EXACTLY) { ? ? ? ? ? ? mHeight = heightSize; ? ? ? ? } else { ? ? ? ? ? ? mHeight = DensityUtil.dip2px(mContext, 220); ? ? ? ? } ? ? ? ? setMeasuredDimension(mWidth > mHeight ? mHeight : mWidth, mWidth > mHeight ? mHeight : mWidth); ? ? } ? ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? super.onDraw(canvas); ? ? ? ? //繪制圓形 ? ? ? ? canvas.save(); ? ? ? ? for (int i = 0; i < 100; i++) { ? ? ? ? ? ? if (mProgress > i) { ? ? ? ? ? ? ? ? mInnerPaint.setColor(mInnerColor); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? mInnerPaint.setColor(mOuterColor); ? ? ? ? ? ? } ? ? ? ? ? ? canvas.drawLine(mWidth / 2, 0, mWidth / 2, DensityUtil.dip2px(mContext, 10), mInnerPaint); ? ? ? ? ? ? canvas.rotate(3.6f, mWidth / 2, mHeight / 2); ? ? ? ? } ? ? ? ? canvas.restore(); ? ? ? ? //繪制文字 ? ? ? ? String progreStr = mProgress + ""; ? ? ? ? Rect rect = new Rect(); ? ? ? ? mTextPaint.getTextBounds(progreStr, 0, progreStr.length(), rect); ? ? ? ? int dx = getWidth() / 2 - rect.width() / 2; ? ? ? ? canvas.drawText(progreStr, dx, getHeight() / 2 + rect.height() / 2, mTextPaint); ? ? ? ? //繪制小圓圈 ? ? ? ? canvas.save(); ? ? ? ? canvas.rotate(mDotProgress * 3.6f, mWidth / 2, mHeight / 2); ? ? ? ? canvas.drawCircle(mWidth / 2 - (mInnerPaint.getStrokeWidth() * 2), DensityUtil.dip2px(mContext, 10) + DensityUtil.dip2px(mContext, 8), DensityUtil.dip2px(mContext, 3), mDotPaint); ? ? ? ? canvas.restore(); ? ? } ? ? ? public void setProgress(int progress) { ? ? ? ? mProgress = progress; ? ? ? ? mDotProgress = progress; ? ? ? ? invalidate(); ? ? } ? ? ? public void setAnimation(int progress, int time) { ? ? ? ? ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, progress); ? ? ? ? valueAnimator.setDuration(time); ? ? ? ? valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onAnimationUpdate(ValueAnimator valueAnimator) { ? ? ? ? ? ? ? ? float value = (float) valueAnimator.getAnimatedValue(); ? ? ? ? ? ? ? ? setProgress((int) value); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? valueAnimator.start(); ? ? } ? }
在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" ? ? android:background="@color/black" ? ? android:gravity="center" ? ? android:orientation="vertical" ? ? tools:context=".MainActivity"> ? ? ? <com.example.customviewdome.MyRelative ? ? ? ? android:id="@+id/my_view" ? ? ? ? android:layout_width="500dp" ? ? ? ? android:layout_height="200dp" ? ? ? ? app:border_width="10dp" ? ? ? ? app:inner_color="@color/gray" ? ? ? ? app:outer_color="@color/indianred" ? ? ? ? app:step_text_color="@color/indianred" ? ? ? ? app:step_text_size="30dp" /> ? ? ? <com.example.customviewdome.CircleLoadingView ? ? ? ? android:id="@+id/circleloading" ? ? ? ? android:layout_width="200dp" ? ? ? ? android:layout_height="200dp" ? ? ? ? android:layout_marginTop="15dp" ? ? ? ? app:text_size="30dp" ? ? ? ? app:dot_color="@color/red" ? ? ? ? app:text_color="@color/indianred" ? ? ? ? app:in_color="@color/indianred" ? ? ? ? app:out_color="@color/gray" /> ? ? </LinearLayout>
在activity中實(shí)例化
package com.example.customviewdome; ? import androidx.appcompat.app.AppCompatActivity; ? import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.DecelerateInterpolator; ? public class MainActivity extends AppCompatActivity { ? ? ? private MyRelative my_view; ? ? private CircleLoadingView circleloading; ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? circleloading = findViewById(R.id.circleloading); ? ? ? ? circleloading.setAnimation(80, 5000); ? ? ? ? my_view = findViewById(R.id.my_view); ? ? ? ? my_view.setAnimator(100,80,5000); ? ? } }
本文中使用到的DensityUtil類是為了將dp轉(zhuǎn)換為px來(lái)使用,以便適配不同的屏幕顯示效果
public class DensityUtil { ? ? public static int dip2px(Context context, float dpValue) { ? ? ? ? final float scale = context.getResources().getDisplayMetrics().density; ? ? ? ? return (int) (dpValue * scale + 0.5f); ? ? } }
以上就是兩個(gè)效果的源碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條
- Android Webview添加網(wǎng)頁(yè)加載進(jìn)度條實(shí)例詳解
- Android 進(jìn)度條 ProgressBar的實(shí)現(xiàn)代碼(隱藏、出現(xiàn)、加載進(jìn)度)
- Android自定義View仿華為圓形加載進(jìn)度條
- Android自定義View實(shí)現(xiàn)加載進(jìn)度條效果
- Android開(kāi)發(fā)之ProgressBar字體隨著進(jìn)度條的加載而滾動(dòng)
- Android自定義View基礎(chǔ)開(kāi)發(fā)之圖片加載進(jìn)度條
- Android自定義帶加載動(dòng)畫效果的環(huán)狀進(jìn)度條
- Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條
- Android自定義帶進(jìn)度條WebView仿微信加載過(guò)程
相關(guān)文章
Android實(shí)現(xiàn)GridView中ImageView動(dòng)態(tài)變換的方法
這篇文章主要介紹了Android實(shí)現(xiàn)GridView中ImageView動(dòng)態(tài)變換的方法,以實(shí)例形式較為詳細(xì)的分析了GridView中ImageView動(dòng)態(tài)變換的頁(yè)面布局及功能實(shí)現(xiàn)相關(guān)技巧,需要的朋友可以參考下2015-10-10Android Room數(shù)據(jù)庫(kù)容易遇到的問(wèn)題以及解決方法
這篇文章給大家介紹了我們?cè)贏ndroid Room數(shù)據(jù)庫(kù)容易遇到的坑以及解決方法,文中有詳細(xì)的代碼示例供我們參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-09-09Android點(diǎn)擊事件之多點(diǎn)觸摸與手勢(shì)識(shí)別的實(shí)現(xiàn)
這篇文章主要介紹了Android點(diǎn)擊事件之多點(diǎn)觸摸與手勢(shì)識(shí)別的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Android獲取系統(tǒng)時(shí)間以及網(wǎng)絡(luò)時(shí)間
這篇文章主要為大家詳細(xì)介紹了Android獲取系統(tǒng)時(shí)間以及網(wǎng)絡(luò)時(shí)間的方法,感興趣的小伙伴們可以參考一下2016-07-07