網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android自定義View實現(xiàn)圓形加載進度條的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
話不多說,咱們直接看代碼
首先第一種:
1、創(chuàng)建自定義View類
public class MyRelative extends View {
??
? ? public MyRelative(Context context) {
? ? ? ? this(context, null); //手動改成this...
? ? }
?
? ? public MyRelative(Context context, @Nullable AttributeSet attrs) {
? ? ? ? this(context, attrs, 0);//手動改成this...
? ? }
?
? ? @SuppressLint("ResourceAsColor")
? ? public MyRelative(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? ??
? ? }
}
2、自定義屬性,(在values文件夾下創(chuàng)建一個XML,取名為atts_circle_view.xml)
<?xml version="1.0" encoding="utf-8"?> <resources> ? ? <declare-styleable name="MyRelative"> //這個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ò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方法(測量view大小)
?@Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec);
? ? ? ? //測量寬高
? ? ? ? int width = MeasureSpec.getSize(widthMeasureSpec);
? ? ? ? int height = MeasureSpec.getSize(heightMeasureSpec);
? ? ? ? //將控件截成正方形
? ? ? ? //三目運算符取長度短的一邊作為寬高
? ? ? ? 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、要想效果炫酷怎么能少了動畫
寫一個方法可以直接在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、其他(動畫等等)
? ? @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);
? ? ? ? //測量寬高
? ? ? ? int width = MeasureSpec.getSize(widthMeasureSpec);
? ? ? ? int height = MeasureSpec.getSize(heightMeasureSpec);
? ? ? ? //將控件截成正方形
? ? ? ? //三目運算符取長度短的一邊作為寬高
? ? ? ? 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();
? ? }
}
第二種圓形進度條
步驟和以上差不多,下面直接貼出源碼
自定義屬性
<?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;
? ? //圓點顏色
? ? 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)前進度
? ? private int mProgress = 0;
? ? //創(chuàng)建文字畫筆
? ? private Paint mTextPaint;
? ? //創(chuàng)建小圓圈畫筆
? ? private Paint mDotPaint;
? ? //小圓圈起點位置
? ? private int mDotProgress;
?
? ? //仿華為圓形加載框
? ? public CircleLoadingView(Context context) {
? ? ? ? this(context, null);
? ? }
?
? ? public CircleLoadingView(Context context, @Nullable AttributeSet attrs) {
? ? ? ? this(context, attrs, 0);
? ? }
?
? ? //1、自定義屬性
? ? //2、測量控件大小
? ? //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時直接使用
? ? ? ? ? ? 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中實例化
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來使用,以便適配不同的屏幕顯示效果
public class DensityUtil {
? ? public static int dip2px(Context context, float dpValue) {
? ? ? ? final float scale = context.getResources().getDisplayMetrics().density;
? ? ? ? return (int) (dpValue * scale + 0.5f);
? ? }
}
以上就是兩個效果的源碼
原文鏈接:https://blog.csdn.net/weixin_43566836/article/details/123319578
相關(guān)推薦
- 2022-09-01 Linux實現(xiàn)項目的自動化部署_Linux
- 2022-07-09 Docker可視化、數(shù)據(jù)持久化
- 2022-08-21 Caffe卷積神經(jīng)網(wǎng)絡(luò)視覺層Vision?Layers及參數(shù)詳解_python
- 2022-08-31 C++淺析引用的定義與使用_C 語言
- 2022-05-12 Nginx反向代理 對響應(yīng)網(wǎng)頁中的字符串進行替換設(shè)置
- 2022-08-12 python關(guān)于字典及遍歷的常用方法_python
- 2023-07-10 Gateway服務(wù)網(wǎng)關(guān)
- 2022-07-16 SpringMVC基礎(chǔ)工作原理以及實例
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支