【问题标题】:Animation Scale Arc by Angle动画按角度缩放弧
【发布时间】:2018-04-04 18:23:19
【问题描述】:

我的任务是绘制圆图。
我有 4 条弧线。
在每条弧线上,我有 2 个值来制作 View Animate。

这张图片

会告诉你更多细节。

谁能建议我应该为每个值使用的动画?
以及如何使其按特定角度进行动画处理?

编辑:我上传 .gif 文件以获得更多详细信息。

【问题讨论】:

  • 如果我写错了或者我的问题不清楚,请推荐,以便我下次改进。不要只是投反对票,我不知道我错了什么:(
  • 这就是我面临的问题。我不知道应该使用哪种方式来绘制可以轻松控制缩放动画的弧线。其实我只是一个大三学生,对此我一无所知,我1周前才开始接触ObjectAnimator。所以我需要一些关键字可以解决问题。
  • 好吧,我错了,也许我应该使用“我的任务是绘制圆图”
  • 谢谢,我会研究一下Cabvas。

标签: android animation layout


【解决方案1】:

您可以制作自定义视图和自定义动画。覆盖自定义动画中的applyTransformation() 方法以应用转换。

public class ArcView extends View {

    private final Paint mPaint;
    private final RectF mRect;
    private float arcAngle;

    public ArcView(Context context, AttributeSet attrs) {

        super(context, attrs);

        // Set Angle to 0 initially or whatever you want
        arcAngle = 0;

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(4);
        mPaint.setColor(Color.WHITE);
        mRect = new RectF(2, 2, 78, 78);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(mRect, 90, arcAngle, false, mPaint);
    }

    public float getArcAngle() {
        return arcAngle;
    }

    public void setArcAngle(float arcAngle) {
        this.arcAngle = arcAngle;
    }
}

这是你的动画类的样子

public class ArcAngleAnimation extends Animation {

    private ArcView arcView;

    private float oldAngle;
    private float newAngle;

    public ArcAngleAnimation(ArcView arcView, int newAngle) {
        this.oldAngle = arcView.getArcAngle();
        this.newAngle = newAngle;
        this.arcView = arcView;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation transformation) {
        float angle = oldAngle + ((newAngle - oldAngle) * interpolatedTime);

        arcView.setArcAngle(angle);
        arcView.requestLayout();
    }

    public void setNewAngle(int newAngle){
        this.newAngle = newAngle;
    }

    public void setOldAngle(float oldAngle) {
        this.oldAngle = oldAngle;
    }
}

现在您需要做的就是应用动画并在您的活动或片段中运行它

ArcView view = (ArcView) findViewById(R.id.arc_view);
ArcAngleAnimation arcAnimation;
arcAnimation = new ArcAngleAnimation(view, 30);
arcAnimation.setFillAfter(true);
arcAnimation.setDuration(200);
view.startAnimation(arcAnimation);

【讨论】:

  • 非常感谢。但是 arcAnimation 中的 leftAnim 是什么 = new ArcAngleAnimation(leftAnim, 30); ?
  • 我的错,应该是 view 声明的 ArcView 对象。编辑了答案。
  • 也许我错了,但它不起作用。布局为
    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.thiendn.tempproject.ArcView android:id="@+id/arc_view" android:src="@drawable/test_smile" android:layout_gravity="center" android:layout_width="50dp" android:layout_height="50dp" android:background="@color/colorAccent"/> </LinearLayout>
猜你喜欢
  • 2023-03-22
  • 2012-11-30
  • 2017-03-09
  • 1970-01-01
  • 2018-12-26
  • 2018-02-05
  • 1970-01-01
  • 2017-04-06
  • 2012-12-02
相关资源
最近更新 更多