Android自定義帶動畫的半圓環(huán)型進度效果
本文實例為大家分享了Android半圓環(huán)型進度效果的具體代碼,供大家參考,具體內(nèi)容如下
package com.newair.ondrawtext;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.OvershootInterpolator;
/**
* Created by ouhimehime on 16/6/15.
* --------自定義控件-------
*/
public class CustomView extends View {
//畫筆
private Paint paint;
private RectF oval;
//圓弧顏色
private int roundColor;
//進度顏色
private int progressColor;
//文字內(nèi)容
private boolean textIsShow;
//字體大小
private float textSize = 14;
//文字顏色
private int textColor;
//最大進度
private int max = 1000;
//當(dāng)前進度
private int progress = 300;
//圓弧寬度
private int roundWidth = 30;
private int viewWidth; //寬度--控件所占區(qū)域
private float nowPro = 0;//用于動畫
private ValueAnimator animator;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs, context);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(attrs, context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initAttrs(attrs, context);
}
private void initAttrs(AttributeSet attr, Context context) {
TypedArray array = context.obtainStyledAttributes(attr, R.styleable.CustomView);
roundColor = array.getColor(R.styleable.CustomView_roundColor, Color.BLACK);//環(huán)形顏色
progressColor = array.getColor(R.styleable.CustomView_progressColor, Color.RED);//進度顏色
textIsShow = array.getBoolean(R.styleable.CustomView_textIsShow, false);//文字
textSize = array.getDimension(R.styleable.CustomView_textSize, 14);//文字大小
textColor = array.getColor(R.styleable.CustomView_textColor, Color.BLACK);//文字顏色
roundWidth = array.getInt(R.styleable.CustomView_roundWidth, 30);//圓環(huán)寬度
array.recycle();
//動畫
animator = ValueAnimator.ofFloat(0, progress);
animator.setDuration(1800);
animator.setInterpolator(new OvershootInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
nowPro = (float) animation.getAnimatedValue();
postInvalidate();
}
});
animator.start();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
final int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
if (widthSpecMode == MeasureSpec.AT_MOST) {//可獲得最大空間
setMeasuredDimension(widthMeasureSpec, (widthSpecSize / 2) + (int) (Math.cos(20) * (widthSpecSize / 2)));
} else if (widthMeasureSpec == MeasureSpec.EXACTLY) {//一般指精確值
setMeasuredDimension(widthMeasureSpec, (widthSpecSize / 2) + (int) (Math.cos(20) * (widthSpecSize / 2)));
} else {
setMeasuredDimension(widthMeasureSpec, (viewWidth / 2) + (int) (Math.cos(20) * (viewWidth / 2)));
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;//得到寬度以此來計算控件所占實際大小
//計算畫布所占區(qū)域
oval = new RectF();
oval.left = roundWidth + getPaddingLeft();
oval.top = roundWidth + getPaddingTop();
oval.right = viewWidth - roundWidth - getPaddingRight();
oval.bottom = viewWidth - roundWidth - getPaddingBottom();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true); //設(shè)置畫筆為無鋸齒
paint.setColor(roundColor); //設(shè)置畫筆顏色
paint.setStrokeWidth(roundWidth); //線寬
paint.setStyle(Paint.Style.STROKE); //空心
canvas.drawArc(oval, 160, 220, false, paint); //繪制圓弧
//畫進度層
paint.setColor(progressColor);
paint.setStrokeWidth(roundWidth + 1);
canvas.drawArc(oval, 160, 220 * nowPro / max, false, paint); //繪制圓弧
if (textIsShow) {
paint.setColor(textColor);
paint.setStrokeWidth(0);
paint.setTypeface(Typeface.DEFAULT);
paint.setTextSize(textSize * 2);
float textWidth = paint.measureText((int) ((nowPro / (float) max) * 100) + "%");
canvas.drawText((int) ((nowPro / (float) max) * 100) + "%", viewWidth / 2 - textWidth / 2, viewWidth / 2, paint);
}
}
private int getDefaultHeight() {
return 0;
}
private int getDefaultWidth() {
return 0;
}
public int getRoundColor() {
return roundColor;
}
public void setRoundColor(int roundColor) {
this.roundColor = roundColor;
}
public int getProgressColor() {
return progressColor;
}
public void setProgressColor(int progressColor) {
this.progressColor = progressColor;
}
public boolean getText() {
return textIsShow;
}
public void setText(boolean text) {
this.textIsShow = text;
}
public float getTextSize() {
return textSize;
}
public void setTextSize(float textSize) {
this.textSize = textSize;
}
public int getTextColor() {
return textColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
this.progress = progress;
}
}
自定義屬性
<declare-styleable name="CustomView">
<attr name="roundColor" format="color" />
<attr name="progressColor" format="color" />
<attr name="textIsShow" format="boolean" />
<attr name="textSize" format="dimension" />
<attr name="textColor" format="color" />
<attr name="roundWidth" format="integer" />
</declare-styleable>
用法
<com.newair.ondrawtext.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:progressColor="@android:color/holo_orange_dark"
app:roundColor="@android:color/holo_blue_dark"
app:roundWidth="45"
app:textColor="@android:color/black"
app:textIsShow="true"
app:textSize="14sp" />
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android FrameWork之Zygote啟動示例詳解
這篇文章主要為大家介紹了Android FrameWork之Zygote啟動示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
Android MediaPlayer 音頻倍速播放 調(diào)整播放速度問題
這篇文章主要介紹了Android MediaPlayer 音頻倍速播放,調(diào)整播放速度,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
Android Studio3.6中的View Binding初探及用法區(qū)別
這篇文章主要介紹了Android 中的View Binding初探及用法區(qū)別,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Android實現(xiàn)Service在前臺運行服務(wù)
這篇文章主要為大家詳細(xì)介紹了Android中實現(xiàn)Service在前臺運行服務(wù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Android開發(fā)中解析xml文件XmlUtils工具類與用法示例
這篇文章主要介紹了Android開發(fā)中解析xml文件XmlUtils工具類與用法,結(jié)合實例形式分析了Android開發(fā)中解析xml文件工具類定義與相關(guān)使用技巧,需要的朋友可以參考下2018-01-01
讓Android應(yīng)用不被殺死(killer)的方法
這篇文章主要介紹了讓Android應(yīng)用不被殺死(killer)的方法,本文講解了實現(xiàn)方法和原理分析,需要的朋友可以參考下2015-04-04
Android 應(yīng)用更換皮膚實現(xiàn)方法
本文主要介紹Android 應(yīng)用更換皮膚,Android應(yīng)用如果想更換皮膚這里幫大家整理了相關(guān)資料,有需要的小伙伴可以參考下2016-08-08
Android自定義view實現(xiàn)雪花特效實例代碼
實現(xiàn)雪花的效果其實也可以通過自定義View的方式來實現(xiàn)的,而且操作上也相對簡單一些,下面這篇文章主要給大家介紹了關(guān)于Android自定義view實現(xiàn)雪花特效的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12

