Android开发自定义view-------SectorView 自定义扇形View


背景图是圆形的 一圈标记为24份(因为一开始设计的是时间方面的)
setPaintColor 设置view的背景
setTime方法输入开始与结束时间 然后 start 就会有一个一秒钟的滑动动画



import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.lidroid.xutils.util.LogUtils;
import com.yulook.yu.R;

/**
* Created by Administrator on 2016/3/1.
*/
public class SectorView extends View {


public SectorView(Context context) {
super(context);
}

public SectorView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SectorView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}


private int color1=getResources().getColor(R.color.white);//外圈
private int color2=getResources().getColor(R.color.white);//中间
private int color3=getResources().getColor(R.color.white);//内圈
private int color4=getResources().getColor(R.color.white);//背景


private int color5=getResources().getColor(R.color.white);//线
private int color6=getResources().getColor(R.color.white);//扇形




public void setPaintColor(int color1,int color2,int color3,int color4,int color5,int color6){
this.color1=color1;
this.color2=color2;
this.color3=color3;
this.color4=color4;
this.color5=color5;
this.color6=color6;
}

private int with=200,height=200;
private int proportion=24;//等分比例

public void setWith(int with){
this.with=with;
this.height=with;
}


private double startTime=24;
private double stopTime=24;
private int angle=0;//旋转角度

private double startX=getRoundX(with/2,with/2,with/2-12,startTime);
private double startY=getRoundY(with/2,with/2,with/2-12,startTime);

private double stopX=0;
private double stopY=0;

public void setTime(int startTime,int stopTime){
this.startTime=(double) startTime;
this.stopTime=(double) stopTime;

startX = getRoundX(with/2,with/2,with/2-12,startTime);
startY = getRoundY(with/2,with/2,with/2-12,startTime);

stopX = getRoundX(with/2,with/2,with/2-12,stopTime);
stopY = getRoundY(with/2,with/2,with/2-12,stopTime);

}


/**
* x0 y0 圆点坐标
* r 半径
* time 计算角度
* */
private double getRoundX(double x0,double y0,double r,double time){
double z_angle = time * 360/24-90;
double x = x0 + r * Math.cos(z_angle * 3.14/180);
LogUtils.i("====x:"+x+"角度:"+z_angle);
return x;
}


private double getRoundY(double x0,double y0,double r,double time){
double z_angle = time * 360/24-90;
double y = y0 + r * Math.sin(z_angle * 3.14 / 180);
LogUtils.i("====y:"+y);
return y;
}


int startAngle=0;
/**
* 设置旋转起始角度
* */
public void setStartAngle(int startAngle){
this.startAngle=startAngle;
}

/**开始角度*/
private int getStartangle(int time){
int start_Angle = 360 / 24 * time - 90;
return start_Angle;
}


/**
* 计算冲突时间
*
* @param start1 档期开始时间
* @param stop1 档期结束时间
* @param start2 任务开始时间
* @param stop2 任务结束时间
* @param type 标记类型 !=0 即可
*
*/
private int type=0;
private int start1=0;
private int start2=0;
private int stop2=0;
private int stop1=0;

public void setConflictTime(int start1,int stop1,int start2,int stop2,int type){
this.type=type;
this.start1=start1;
this.stop2=stop2;
this.start2=start2;
this.stop1=stop1;
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(with, height);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
try {
Paint p = new Paint();
p.setStrokeWidth((float) 2.0);
p.setAntiAlias(true);// 设置画笔的锯齿效果

p.setColor(color1);
canvas.drawCircle(with/2, height/2, with/2, p);

p.setColor(color2);
canvas.drawCircle(with/2, height/2,with/2-5, p);


p.setColor(color3);
canvas.drawCircle(with/2, height/2, with/2-11, p);


p.setColor(color4);
canvas.drawCircle(with/2, height/2, with/2-12, p);


if(0!=type){

int z_angle = 360 / 24 * (stop2 - start1);

p.setColor(Color.parseColor("#F8F8F8"));
RectF oval2 = new RectF(12, 12, with-12, with-12);
canvas.drawArc(oval2, getStartangle(start1), z_angle, true, p);


p.setColor(Color.parseColor("#E8E8E8"));
double startX = getRoundX(with/2,with/2,with/2-12,start1);
double startY = getRoundY(with/2,with/2,with/2-12,start1);

double stopX = getRoundX(with/2,with/2,with/2-12,stop2);
double stopY = getRoundY(with/2,with/2,with/2-12,stop2);
canvas.drawLine(with/2, height/2, (float) startX, (float)startY, p);//开始竖线
canvas.drawLine(with/2, height/2, (float)stopX, (float)stopY, p);//结束竖线

//
int x_angle = 360 / 24 * (stop1 - start2);

p.setColor(color6);
RectF oval1 = new RectF(12, 12, with-12, with-12);
canvas.drawArc(oval1, getStartangle(start2), x_angle, true, p);


p.setColor(color5);
double startX1 = getRoundX(with/2,with/2,with/2-12,start2);
double startY1 = getRoundY(with/2,with/2,with/2-12,start2);

double stopX2 = getRoundX(with/2,with/2,with/2-12,stop1);
double stopY2 = getRoundY(with/2,with/2,with/2-12,stop1);
canvas.drawLine(with/2, height/2, (float) startX1, (float)startY1, p);//开始竖线
canvas.drawLine(with/2, height/2, (float)stopX2, (float)stopY2, p);//结束竖线


}else{
p.setColor(color6);
RectF oval2 = new RectF(12, 12, with-12, with-12);
canvas.drawArc(oval2, getStartangle((int)startTime), angle, true, p);

p.setColor(color5);
double stopX = getRoundX(with/2,with/2,with/2-12,stopTime);
double stopY = getRoundY(with/2,with/2,with/2-12,stopTime);
canvas.drawLine(with/2, height/2, (float) stopX, (float)stopY, p);//结束竖线
canvas.drawLine(with/2, height/2, (float) startX, (float)startY, p);//开始竖线
}

}catch (Exception p){
p.printStackTrace();
}
}


/**扇形动画*/
public class task extends AsyncTask<Void, Float, Void>{

private Handler handler;
private double a=0;//旋转度数
private double b=0;

public task(Handler handler){
this.handler=handler;
if(startTime!=24){
double num = stopTime - startTime;
a=360*num/24;
}else {
a=360/24*stopTime;
}
b=stopTime;
}


@Override
protected Void doInBackground(Void... params) {
for(int i=1;i<=a;i++){
try {
angle=i;
stopTime=startTime+((double)24)/360*i;
handler.sendEmptyMessage(1);
Thread.sleep((long) (1000/a));
}catch (Exception p){
p.printStackTrace();
}
}
return null;
}
}

public void start(){
new task(handler).execute();
}

private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
invalidate();
}
};

}

转载于:https://www.cnblogs.com/yulook/p/5278836.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
疫情居家办公系统管理系统按照操作主体分为管理员和用户。管理员的功能包括办公设备管理、部门信息管理、字典管理、公告信息管理、请假信息管理、签到信息管理、留言管理、外出报备管理、薪资管理、用户管理、公司资料管理、管理员管理。用户的功能等。该系统采用了MySQL数据库,Java语言,Spring Boot框架等技术进行编程实现。 疫情居家办公系统管理系统可以提高疫情居家办公系统信息管理问题的解决效率,优化疫情居家办公系统信息处理流程,保证疫情居家办公系统信息数据的安全,它是一个非常可靠,非常安全的应用程序。 管理员权限操作的功能包括管理公告,管理疫情居家办公系统信息,包括外出报备管理,培训管理,签到管理,薪资管理等,可以管理公告。 外出报备管理界面,管理员在外出报备管理界面中可以对界面中显示,可以对外出报备信息的外出报备状态进行查看,可以添加新的外出报备信息等。签到管理界面,管理员在签到管理界面中查看签到种类信息,签到描述信息,新增签到信息等。公告管理界面,管理员在公告管理界面中新增公告,可以删除公告。公告类型管理界面,管理员在公告类型管理界面查看公告的工作状态,可以对公告的数据进行导出,可以添加新公告的信息,可以编辑公告信息,删除公告信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值