Android 在画布上绘制圆形模拟动画
我需要画一个边距为10像素的空圆圈。我遇到的问题是,我需要在2秒钟内模拟圆的绘制,然后开始在圆的顶部绘制另一个具有另一种颜色的圆。我正在使用一个自定义视图,我尝试将我的逻辑实现到onDraw方法中,并每隔50毫秒使视图无效。问题是我画不出圆圈……我只画了些蹩脚的数字。有人知道我如何在不使用canvas.drawCircle方法的情况下绘制圆吗?因为该方法在没有动画的情况下直接绘制圆 我当前的代码Android 在画布上绘制圆形模拟动画,android,animation,android-canvas,Android,Animation,Android Canvas,我需要画一个边距为10像素的空圆圈。我遇到的问题是,我需要在2秒钟内模拟圆的绘制,然后开始在圆的顶部绘制另一个具有另一种颜色的圆。我正在使用一个自定义视图,我尝试将我的逻辑实现到onDraw方法中,并每隔50毫秒使视图无效。问题是我画不出圆圈……我只画了些蹩脚的数字。有人知道我如何在不使用canvas.drawCircle方法的情况下绘制圆吗?因为该方法在没有动画的情况下直接绘制圆 我当前的代码 public class CustomAnimationView extends View{
public class CustomAnimationView extends View{
private Canvas canvas;
private int count = 0;
private Paint paint;
private int mLeft;
private int mRight;
private int mBottom;
private int mTop;
public CustomAnimationView(Context context) {
super(context);
}
public CustomAnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAnimationView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setAttributes(attrs);
}
private void setAttributes(AttributeSet attrs) {
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.canvas = canvas;
if(paint == null){
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(10);
paint.setColor(Color.BLACK);
}
if(count<150){
drawFirstQuarter(count);
}
count++;
}
public void drawFirstQuarter(int count){
RectF oval = new RectF(mLeft, mTop, mRight, mBottom);
canvas.drawArc(oval, 90, 30, true, paint);
}
public void setRect(int top, int bottom, int left, int right){
mBottom = bottom;
mTop = top;
mLeft = left;
mRight = right;
}
公共类CustomAnimationView扩展视图{
私人帆布;
私有整数计数=0;
私人油漆;
私营企业;
私人国际航空公司;
私人英特姆博托姆酒店;
私人int mTop;
公共自定义动画视图(上下文){
超级(上下文);
}
公共CustomAnimationView(上下文、属性集属性){
超级(上下文,attrs);
}
公共CustomAnimationView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
设置属性(attrs);
}
私有void集合属性(AttributeSet attrs){
}
受保护的void onDraw(画布){
super.onDraw(帆布);
this.canvas=画布;
if(paint==null){
油漆=新油漆();
paint.setAntiAlias(真);
油漆.设置样式(样式.笔划);
油漆。设置行程宽度(10);
油漆。设置颜色(颜色。黑色);
}
如果(计数谢谢。我已经解决了。
下面是一个代码示例
public class CustomAnimationView extends View{
private Canvas canvas;
private int mCount = 0;
private Paint paint1;
private Paint paint2;
private RectF oval1;
private Context context;
private int mColorCount = 0;
public CustomAnimationView(Context context) {
super(context);
this.context = context;
}
public CustomAnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public CustomAnimationView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
setAttributes(attrs);
}
private void setAttributes(AttributeSet attrs) {
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.canvas = canvas;
if(paint1 == null){
paint1 = new Paint();
paint1.setAntiAlias(true);
paint1.setStyle(Style.STROKE);
paint1.setStrokeWidth(10);
}
if(paint2 == null){
paint2 = new Paint();
paint2.setAntiAlias(true);
paint2.setStyle(Style.STROKE);
paint2.setStrokeWidth(10);
}
if(mCount % 360 == 0 ){
mColorCount++;
}
if(mColorCount % 2 == 0){
paint1.setColor(context.getResources().getColor(R.color.white));
paint2.setColor(context.getResources().getColor(R.color.black));
}else{
paint2.setColor(context.getResources().getColor(R.color.white));
paint1.setColor(context.getResources().getColor(R.color.black));
}
if(oval1 == null)
oval1 = new RectF(5,5,canvas.getWidth()-5, canvas.getHeight()-5);
drawFirstQuarter(mCount, oval1);
}
public void drawFirstQuarter(int count, RectF oval){
canvas.drawArc(oval, 90, 360, false, paint2);
canvas.drawArc(oval, 90, count, false, paint1);
if(mCount == 330)
mCount = 0;
else
mCount += 30;
}
}您可以发布当前自定义视图的代码吗?