【问题标题】:Android custom Animation for an ArcShapeArcShape 的 Android 自定义动画
【发布时间】:2011-05-20 00:08:45
【问题描述】:

首先让我解释一下我的目标。我正在尝试制作一个 Animation 来更改 ArcShape 的属性。 ArcShape's 构造函数有两个字段:startAnglesweepAngle。我想为sweepAngle 设置动画,使其在屏幕上显示为一个不断缩小的圆圈。

你可以通过想象吃豆人来想象这个动画。想象他的嘴是闭着的。这个动画类似于他越来越张开上颚,直到不再有吃豆人。

现在...我在实现这一点时遇到了一些问题。首先,一旦创建了ArcShape,就没有内置方法可以更改它的sweepAngle。这让我想到了我的第一个问题:有没有办法覆盖 ArcShape 并实现一些 setSweepAngle 方法?还是我必须为我希望显示的每个 sweepAngle 创建一个 new ArcShape

现在开始第二个问题...假设我找到了第一个问题的解决方案,我该如何创建这个Animation?这是我现在所拥有的要点:

public class OpenPacman extends Animation {
  public OpenPacman(float startAngle, float sweepAngle) {
    mStartAngle = startAngle;
    mSweepAngle = sweepAngle;
  }

  @Override
  protected void applyTransformation(float interpolatedTime, Transformation t) {
    /* This represents the current sweepAngle */
    float currAngle = mStartAngle + ((mSweepAngle - mStartAngle) * interpolatedTime);

    //Now I need to update the ArcShape's sweepAngle to currAngle. But HOW?
  }
}

【问题讨论】:

    标签: java android animation


    【解决方案1】:

    我找到了解决办法。我有一个扩展View 的类,我们称之为Pacman 我将自定义Animation 嵌套在这个Pacman 类中。这使我可以访问Pacman 类的member variables

    public class Pacman extends View {
      float mSweepAngle;
      ...
      //include constructors
      //override onMeasure
      ...
    
      /* Here we override onDraw */
      @Override
      protected void onDraw(final Canvas canvas) {
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        RectF oval = new RectF(canvas.getClipBounds());
        canvas.drawArc(oval, 0, mCurrAngle, true, p);
      }
    
      /* Here we define our nested custom animation */
      public class OpenPacman extends Animation {
        float mStartAngle;
        float mSweepAngle;
    
        public OpenPacman (int startAngle, int sweepAngle, long duration) {
          mStartAngle = startAngle;
          mSweepAngle = sweepAngle;
          setDuration(duration);
          setInterpolator(new LinearInterpolator());
        }
    
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
          float currAngle = mStartAngle + ((mSweepAngle - mStartAngle) * interpolatedTime);
          Pacman.this.mCurrAngle = -currAngle; //negative for counterclockwise animation.
        }
      }
    }
    

    现在,当自定义动画更新容器类mCurrAngle 时,onDraw 会自动调用,从而绘制相应的ArcShape

    【讨论】:

    • 我必须添加“invalidate();”到 applyTransformation 方法,因此视图将以新角度重新绘制。
    • 我按照说明操作,但我的自定义视图没有出现在 UI 上。你能发布你的完整代码吗?
    • 我知道,永远的老帖子....但我不明白这如何使弧线动画化。它只是更新当前角度,然后您可以重绘它,但它不是动画的。那么......你是如何制作这个动画的呢?
    • 不要使用这个过时的“解决方案”。它不起作用。即使你浪费时间画一条线,它也不会动画。见medium.com/@dbottillo/animate-android-custom-view-a94473412054
    【解决方案2】:

    我认为您最好扩展 Drawable 并覆盖 draw() 函数以修改每次调用的扫描角度并绘制相应的弧线。每次更新对象时通常都会调用 Draw,这意味着每次绘制时都必须创建一个新的 ArcShape 动画更多用于对 View 和其他 UI 组件执行转换。

    类似:

    public class OpenPacman  
    {
    
    public OpenPacman(float startAngle, float sweepAngle) {  
        this.mStartAngle = startAngle;  
        this.mSweepAngle = sweepAngle;  
        this.wakaWaka = new ArcShape(this.startAngle, this.mSweepAngle);  
    } 
    
    public void draw(Canvas c){  
      //Do drawing stuff here with the canvas
    }
    
    //Your other functions for calculating angle, etc and making the ArcShape changes
    //Can call these from inside draw function so that when the view that contains your
    //object calls draw, it updates correctly.
    
    public float mStartAngle;  
    public float msweepAngle;  
    private ArcShape wakaWaka;  
    }
    

    希望这能让你走上正轨。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多