布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:custom="http://schemas.android.com/apk/res-auto"
tools:context="com.example.zy.zhangyangyang1508d1009.MainActivity">
<Button
android:id="@+id/change"
android:text="改变外层圆环颜色"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="39dp"
android:layout_above="@+id/myCustomTitleView"
android:layout_centerHorizontal="true" />
<com.example.zymyview.CustomTitleView
android:id="@+id/myCustomTitleView"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="200dp"
android:layout_height="200dp"
custom:mSpeed="100"
custom:mFirstColor="#0040ff"
custom:mSecondColor="#40ff00"
custom:mCircleWidth="20px"
custom:textSize="30px"
/>
<Button
android:id="@+id/kaishi"
android:text="开始"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/chongzhi"
android:layout_alignBottom="@+id/chongzhi"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="51dp"
android:layout_marginStart="51dp" />
<Button
android:id="@+id/chongzhi"
android:text="重置"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="53dp"
android:layout_marginEnd="53dp"
android:layout_below="@+id/myCustomTitleView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="32dp" />
</RelativeLayout>
activity
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Myprogressround mypro;
private Button change;
private boolean flag = false;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0){
mypro.setmPro();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mypro = (Myprogressround) findViewById(R.id.mypro);
Button change= (Button) findViewById(R.id.change);
Button kaishi= (Button) findViewById(R.id.kaishi);
Button chongzhi= (Button) findViewById(R.id.chongzhi);
kaishi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
flag = false;
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... strings) {
for (int i=1; i<361; i++){
if (flag == true){
handler.sendEmptyMessage(0);
break;
}
SystemClock.sleep(10);
publishProgress(i);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
mypro.setProgress(values[0]);
}
}.execute();
}
});
chongzhi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
flag = true;
mypro.setmPro();
}
});
}
}
/// 自定义
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.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class Myprogressround extends View {
private Paint paint;
private int mProgress;
private int mCountProgress;
private float mradiuSize;
private float mringSize;
private float mtextSize;
private float mprogressColor;
public Myprogressround(Context context) {
super(context);
init();
}
public Myprogressround(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
getCustomAttr(context, attrs);
init();
}
private void getCustomAttr(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Myprogressround);
mradiuSize = array.getDimension(R.styleable.Myprogressround_radiuSize, 100);
mringSize = array.getDimension(R.styleable.Myprogressround_ringSize, 20);
mtextSize = array.getDimension(R.styleable.Myprogressround_textSize, 30);
mprogressColor = array.getColor(R.styleable.Myprogressround_progressColor, Color.YELLOW);
}
public Myprogressround(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
paint = new Paint();
paint.setAntiAlias(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width = 0;
int height = 0;
if(widthMode == MeasureSpec.AT_MOST){
width = (int) (mradiuSize * 2);
}else{
width = Math.max(widthSize, (int) (mradiuSize * 2));
}
if(heightMode == MeasureSpec.AT_MOST){
height = (int) (mradiuSize * 2);
}else{
height = Math.max(heightSize, (int) (mradiuSize * 2));
}
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
paint.setStrokeWidth(0);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,mradiuSize, paint);
canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,mradiuSize-mringSize,paint);
paint.setTextSize(mtextSize);
String text =mCountProgress + "%";
float textWidth = paint.measureText(text);
canvas.drawText(text, getMeasuredWidth()/2-textWidth/2, getMeasuredWidth()/2+mtextSize/2, paint);
RectF rectF = new RectF(getMeasuredWidth()/2 - mradiuSize + mringSize/2,getMeasuredHeight()/2 - mradiuSize + mringSize/2,getMeasuredWidth()/2 + mradiuSize - mringSize/2,getMeasuredHeight()/2 + mradiuSize - mringSize/2);
paint.setStrokeWidth(mringSize);
canvas.drawArc(rectF,0,mProgress,false,paint);
}
public void setProgress(int progress){
mProgress = progress;
mCountProgress = progress * 100 / 360;
invalidate();
}
public void setmPro(){
mProgress = 0;
mCountProgress = 0;
invalidate();
}
}
attrs文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Myprogressround">
<attr name="progressColor" format="color"></attr>
<attr name="textSize" format="dimension"></attr>
<attr name="ringSize" format="dimension"></attr>
<attr name="radiuSize" format="dimension"></attr>
</declare-styleable>
</resources>
自定义View 更新
最新推荐文章于 2021-05-27 05:34:52 发布