android - 如何在 canvas android 上绘制带动画的圆形轨迹箭头?

标签 android animation android-canvas

伙计们 我尝试实现弧头上的黑色箭头。箭头以弧形动画,并具有圆形轨迹。我已经根据不同的值用动画实现了红色弧线。 请参阅附件。 如何在红色圆弧上实现黑色箭头?因为如果我采用与红色弧形动画相同的方式,黑色箭头将打印不需要的轨迹。

提前致谢! 里昂

enter image description here

最佳答案

您只需要 Canvas。

这是例子。

这是你的绘图类:

public class CircleView   extends View
{
    public CircleView(Context context) {
        super(context);
        path = new Path();
        paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float angle = 270;

        float radius = canvas.getWidth()/3;
        float x = canvas.getWidth()/2;
        float y = canvas.getHeight()/2;
        final RectF oval = new RectF();

        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(25);

        oval.set(x - radius, y - radius, x + radius,y + radius);

        // Draw circle
        paint.setColor(Color.RED);
        canvas.drawArc(oval, 135, angle, false, paint);
        paint.setColor(Color.BLUE);
        canvas.drawArc(oval, angle, 405-angle, false, paint);

        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLACK);

        float l = 1.2f;
        float a = angle*(float)Math.PI/180;
        // Draw arrow
        path.moveTo(x+ (float)Math.cos(a) *radius, y + (float)Math.sin(a) * radius);
        path.lineTo(x+ (float)Math.cos(a+0.1) *radius*l, y + (float)Math.sin(a+0.1) * radius*l);
        path.lineTo(x+ (float)Math.cos(a-0.1) *radius*l, y + (float)Math.sin(a-0.1) * radius*l);
        path.lineTo(x+ (float)Math.cos(a) *radius, y + (float)Math.sin(a) * radius);
        canvas.drawPath(path, paint);
    }
    private Path path;
    private Paint paint;
}

这是它的 Activity :

public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new CircleView(this));
    }
}

对于 angle == 200 你会看到这样的图像:

angle == 200

IntelliJ Idea 的工作副本:https://github.com/weerf/android_circle

关于android - 如何在 canvas android 上绘制带动画的圆形轨迹箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29489123/

相关文章:

android - 当三次贝塞尔曲线的端点变化时找到新的控制点

kotlin - 我如何在 Android Jetpack Compose 中的 Canvas 上绘制单侧加厚描边?

java - Android-利用Canvas优化画线

javascript - jQuery UI Touch Punch - 页面缩放时移动设备上的 slider 不准确

android - 任务 ':permission_handler:compileDebugJavaWithJavac' 执行失败

javascript - .animate 滚动宽度

jquery - 改变图像CSS jquery的动画

android - 如何在 VideoView 上捕获 "Sorry, This video cannot be played"错误

java - android中的默认微调器样式

vue.js - 在 Vue.js 中如何使导航在向下滚动事件中淡入淡出并在滚动 pageYOffset = 0 处淡出