自定义view圆环进度条
来源:互联网 发布:2017网络直播平台排名 编辑:程序博客网 时间:2024/05/16 10:37
//MyPbActivity
package com.baway.a1509a_view;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MyPbActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_pb);
}
}
//widget文件MyPb
package com.baway.a1509a_view.widget;
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;
import com.baway.a1509a_view.R;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by peng on 2017/10/31.
*/
public class MyPb extends View {
private float cx;
private float cy;
private float radius;
private Paint paint;
private int sweepAngle;
private int color;
public MyPb(Context context) {
this(context, null);
}
public MyPb(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//获取自定义的属性
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyPb);
//获取颜色
color = a.getColor(R.styleable.MyPb_circle_color, Color.BLACK);
radius = a.getDimension(R.styleable.MyPb_circle_radius, 20);
cx = a.getDimension(R.styleable.MyPb_circle_x, 100);
cy = a.getDimension(R.styleable.MyPb_circle_y, 100);
//需要回收
a.recycle();
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (sweepAngle > 360) {
return;
}
sweepAngle += 1;
//刷新View
//这个方法只能在主线程刷新UI
// invalidate();
//这个方法,也是刷新UI,并且可以在子线程刷新
postInvalidate();
}
}
, 1000, 20);
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(color);
paint.setStrokeWidth(5);
canvas.drawCircle(cx, cy, radius, paint);
paint.setColor(Color.RED);
RectF rectF = new RectF(cx - radius, cy - radius, cx + radius, cy + radius);
canvas.drawArc(rectF, -90, sweepAngle, false, paint);
//绘制文字
int progress = (int) (sweepAngle / 360f * 100);
paint.setStrokeWidth(0);
paint.setColor(Color.BLACK);
paint.setTextSize(15);
canvas.drawText(progress + "%", cx - 5, cy, paint);
}
}
//activity_my_pb.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"
tools:context="com.baway.a1509a_view.MyPbActivity">
<com.baway.a1509a_view.widget.MyPb
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circle_color="#0000ff"
app:circle_radius="70dp"
app:circle_x="200dp"
app:circle_y="200dp"/>
</LinearLayout>
* Created by peng on 2017/10/31.
*/
public class MyPb extends View {
private float cx;
private float cy;
private float radius;
private Paint paint;
private int sweepAngle;
private int color;
public MyPb(Context context) {
this(context, null);
}
public MyPb(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//获取自定义的属性
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyPb);
//获取颜色
color = a.getColor(R.styleable.MyPb_circle_color, Color.BLACK);
radius = a.getDimension(R.styleable.MyPb_circle_radius, 20);
cx = a.getDimension(R.styleable.MyPb_circle_x, 100);
cy = a.getDimension(R.styleable.MyPb_circle_y, 100);
//需要回收
a.recycle();
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (sweepAngle > 360) {
return;
}
sweepAngle += 1;
//刷新View
//这个方法只能在主线程刷新UI
// invalidate();
//这个方法,也是刷新UI,并且可以在子线程刷新
postInvalidate();
}
}
, 1000, 20);
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(color);
paint.setStrokeWidth(5);
canvas.drawCircle(cx, cy, radius, paint);
paint.setColor(Color.RED);
RectF rectF = new RectF(cx - radius, cy - radius, cx + radius, cy + radius);
canvas.drawArc(rectF, -90, sweepAngle, false, paint);
//绘制文字
int progress = (int) (sweepAngle / 360f * 100);
paint.setStrokeWidth(0);
paint.setColor(Color.BLACK);
paint.setTextSize(15);
canvas.drawText(progress + "%", cx - 5, cy, paint);
}
}
//activity_my_pb.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"
tools:context="com.baway.a1509a_view.MyPbActivity">
<com.baway.a1509a_view.widget.MyPb
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circle_color="#0000ff"
app:circle_radius="70dp"
app:circle_x="200dp"
app:circle_y="200dp"/>
</LinearLayout>
阅读全文
0 0
- 自定义View圆环进度条
- 自定义view圆环进度条
- 自定义view圆环进度条
- 自定义View之圆环进度条
- Android自定义view 圆环进度条
- Android自定义View圆环带文字进度条
- [AndroidUI]自定义view(三):实现圆环进度条
- Android 自定义圆形进度条(圆环刻度)View
- 自定义View————圆环和圆环进度条
- 自定义view (三) 用view圆环进度条
- 自定义view(1)---自定义圆环,仿qq健康进度条
- Android自定义View:圆环带数字百分比的进度条
- Android自定义view动态绘制百分比圆环进度条
- Android自定义view 画图——圆环进度条
- 自定义圆环进度条
- 自定义圆环进度条
- 自定义PointerProgressBar--圆环进度条
- 自定义圆环进度条
- 响应式开发
- 在windows系统中安装Sulley
- MVP其中有接口(x.http网络请求)
- android NDK
- 事务隔离级别
- 自定义view圆环进度条
- shm_open函数实例及说明
- 虚拟机Centos7配置网络
- HDOJ 1228 A + B(水题)
- 每次运行appium时需要安装unlock,setting,inputmanager三个apk文件的解决方法
- iOS推送javaPNS源码解析二,消息体类
- 经典IT书留存
- java面试宝典
- 接口回调实现登陆注册界面