首先我们先来进行 一些代码需要的属性 与配置
第一步:Values文件 创建 attr_super_circle.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SuperCircleView">
<!-- 圆的半价 -->
<attr name="min_circle_radio" format="integer"></attr>
<!-- 圆环的宽度 -->
<attr name="ring_width" format="float"></attr>
<!-- 分成几段 -->
<attr name="select" format="integer"></attr>
<!-- 每段间隔的角度 -->
<attr name="selec_angle" format="integer"></attr>
<!-- 圆的颜色 -->
<attr name="circle_color" format="color"></attr>
<!-- 圆的颜色 -->
<attr name="max_circle_color" format="color"></attr>
<!-- 圆环的默认颜色 -->
<attr name="ring_normal_color" format="color"></attr>
<!-- 圆环要显示的彩色的区域-->
<attr name="ring_color_select" format="integer"></attr>
<!-- 是否显示断 -->
<attr name="is_show_select" format="boolean"></attr>
</declare-styleable>
</resources>
第二步:再color中配置。
<color name="red_color">#F00</color>
<color name="white">#ffffff</color>
<color name="huise">#C5C7C7</color>
<color name="huise2">#EAECF0</color>
<color name="text_color">#CFD5DE</color>
第三步: dimens 文件夹中配置
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
好了我们的配置也差不多了 可以进行 代码的处理了 ;
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.SweepGradient;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.example.myapplication.R;
/**
* Created by xujiajian on 18/12/14.
*/
public class SuperCircleView extends View {
private final String TAG = "SuperCircleView";
private int mViewWidth; //view的宽
private int mViewHeight; //view的高
private int mViewCenterX; //view宽的中心点
private int mViewCenterY; //view高的中心点
private int mMinRadio; //最里面白色圆的半径
private float mRingWidth; //圆环的宽度
private int mSelect; //分成多少段
private int mSelectAngle; //每个圆环之间的间隔
private int mMinCircleColor; //最里面圆的颜色
private int mMaxCircleColor; //最外面圆的颜色
private int mRingNormalColor; //默认圆环的颜色
private Paint mPaint;
private int color[] = new int[3]; //渐变颜色
private float mRingAngleWidth; //每一段的角度
private RectF mRectF; //圆环的矩形区域
private int mSelectRing = 0; //要显示几段彩色
private boolean isShowSelect = false; //是否显示断
public SuperCircleView(Context context) {
this(context, null);
}
public SuperCircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SuperCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuperCircleView);
mMinRadio = a.getInteger(R.styleable.SuperCircleView_min_circle_radio, 400);
mRingWidth = a.getFloat(R.styleable.SuperCircleView_ring_width, 40);
mSelect = a.getInteger(R.styleable.SuperCircleView_select, 7);
mSelectAngle = a.getInteger(R.styleable.SuperCircleView_selec_angle, 3);
mMinCircleColor = a.getColor(R.styleable.SuperCircleView_circle_color, context.getResources().getColor(R.color.white));
mMaxCircleColor = a.getColor(R.styleable.SuperCircleView_max_circle_color, context.getResources().getColor(R.color.huise2));
mRingNormalColor = a.getColor(R.styleable.SuperCircleView_ring_normal_color, context.getResources().getColor(R.color.huise));
isShowSelect = a.getBoolean(R.styleable.SuperCircleView_is_show_select, false);
mSelectRing = a.getInt(R.styleable.SuperCircleView_ring_color_select, 0);
a.recycle();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
this.setWillNotDraw(false);
color[0] = Color.parseColor("#8EE484");
color[1] = Color.parseColor("#97C0EF");
color[2] = Color.parseColor("#8EE484");
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mViewWidth = getMeasuredWidth();
mViewHeight = getMeasuredHeight();
mViewCenterX = mViewWidth / 2;
mViewCenterY = mViewHeight / 2;
mRectF = new RectF(mViewCenterX - mMinRadio - mRingWidth / 2, mViewCenterY - mMinRadio - mRingWidth / 2, mViewCenterX + mMinRadio + mRingWidth / 2, mViewCenterY + mMinRadio + mRingWidth / 2);
mRingAngleWidth = (360 - mSelect * mSelectAngle) / mSelect;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 显示彩色断大于总共的段数是错误的
*/
if (isShowSelect && mSelectRing > mSelect) {
return;
}
mPaint.setColor(mMaxCircleColor);
canvas.drawCircle(mViewCenterX, mViewCenterY, mMinRadio + mRingWidth + 20, mPaint);
mPaint.setColor(mMinCircleColor);
canvas.drawCircle(mViewCenterX, mViewCenterY, mMinRadio, mPaint);
//画默认圆环
drawNormalRing(canvas);
//画彩色圆环
drawColorRing(canvas);
}
/**
* 画彩色圆环
*
* @param canvas
*/
private void drawColorRing(Canvas canvas) {
Paint ringColorPaint = new Paint(mPaint);
ringColorPaint.setStyle(Paint.Style.STROKE);
ringColorPaint.setStrokeWidth(mRingWidth);
ringColorPaint.setShader(new SweepGradient(mViewCenterX, mViewCenterX, color, null));
if (!isShowSelect) {
canvas.drawArc(mRectF, 270, mSelectRing, false, ringColorPaint);
return;
}
if (mSelect == mSelectRing && mSelectRing != 0 && mSelect != 0) {
canvas.drawArc(mRectF, 270, 360, false, ringColorPaint);
} else {
Log.d(TAG, (mRingAngleWidth * mSelectRing + mSelectAngle + mSelectRing) + "");
canvas.drawArc(mRectF, 270, mRingAngleWidth * mSelectRing + mSelectAngle * mSelectRing, false, ringColorPaint);
}
ringColorPaint.setShader(null);
ringColorPaint.setColor(mMaxCircleColor);
for (int i = 0; i < mSelectRing; i++) {
canvas.drawArc(mRectF, 270 + (i * mRingAngleWidth + (i) * mSelectAngle), mSelectAngle, false, ringColorPaint);
}
}
/**
* 画默认圆环
*
* @param canvas
*/
private void drawNormalRing(Canvas canvas) {
Paint ringNormalPaint = new Paint(mPaint);
ringNormalPaint.setStyle(Paint.Style.STROKE);
ringNormalPaint.setStrokeWidth(mRingWidth);
ringNormalPaint.setColor(mRingNormalColor);
canvas.drawArc(mRectF, 270, 360, false, ringNormalPaint);
if (!isShowSelect) {
return;
}
ringNormalPaint.setColor(mMaxCircleColor);
for (int i = 0; i < mSelect; i++) {
canvas.drawArc(mRectF, 270 + (i * mRingAngleWidth + (i) * mSelectAngle), mSelectAngle, false, ringNormalPaint);
}
}
/**
* 显示几段
*
* @param i
*/
public void setSelect(int i) {
this.mSelectRing = i;
this.invalidate();
}
/**
* 断的总数
*
* @param i
*/
public void setSelectCount(int i) {
this.mSelect = i;
}
/**
* 是否显示断
*
* @param b
*/
public void setShowSelect(boolean b) {
this.isShowSelect = b;
}
public void setColor(int[] color) {
this.color = color;
}
}
这些就是代码的结构:
接下来 我们要看它是如何展示给用户的 。 这是布局中的样式 。
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<com.example.myapplication.ui.SuperCircleView
android:id="@+id/superview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:min_circle_radio="300"
app:ring_width="30" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="60dp"
android:text="体验度"
android:textColor="#CFD5DE"
android:textSize="18sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#506946"
android:textSize="80sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="%"
android:textSize="28sp" />
</LinearLayout>
</FrameLayout>
<com.example.myapplication.ui.SuperCircleView
android:id="@+id/superview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:is_show_select="true"
app:min_circle_radio="20"
app:ring_color_select="1"
app:ring_width="200"
app:select="5" />
</LinearLayout>
最后一步就是在Main中调用
package com.example.myapplication.activity;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.myapplication.R;
import com.example.myapplication.base.BaseActivity;
import com.example.myapplication.ui.SuperCircleView;
public class SixActivity extends BaseActivity {
SuperCircleView mSuperCircleView;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_six);
textView = (TextView) findViewById(R.id.tv);
mSuperCircleView = (SuperCircleView) findViewById(R.id.superview);
mSuperCircleView.setShowSelect(false);
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);
valueAnimator.setTarget(textView);
valueAnimator.setDuration(2000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int i = Integer.valueOf(String.valueOf(animation.getAnimatedValue()));
textView.setText(i + "");
mSuperCircleView.setSelect((int) (360 * (i / 100f)));
}
});
valueAnimator.start();
}
}
哦耶!咱们的功能就到此结束 。看看我们的劳动成果吧 !