Android custom Animation for an ArcShape

2019-01-22 15:24发布

问题:

First let me explain my goal. I am trying to make an Animation that changes the properties of an ArcShape. An ArcShape's constructor takes two fields: startAngle and sweepAngle. I want to animate the sweepAngle so that it appears on screen as a continuously shrinking circle.

You can picture this animation by imagining PacMan. Imagine his mouth is closed. This animation would be akin to him opening his upper jaw more and more until there was no more PacMan.

Now... I have a couple of issues with implementing this. First, once an ArcShape is created, there are no built in methods of changing it's sweepAngle. This brings me to my first question: Is there any way to override ArcShape and implement some setSweepAngle method? Or will I have to create a new ArcShape for each sweepAngle I wish to display?

Now on to the second issue... Assuming I found a solution to the first issue, how could I create this Animation? This is the gist of what I have now:

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?
  }
}

回答1:

I have found a solution. I have a class that extends View We'll call this Pacman I nested my custom Animation within this Pacman class. This allowed me to access the member variables of the Pacman class.

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.
    }
  }
}

Now when the custom animation updates the container classes mCurrAngle, onDraw is automatically called, which draws the appropriate ArcShape.



回答2:

I think you might be better off extending Drawable and override the draw() function to modify the sweep angle on each call and draw the corresponding arc. Draw is typically called every time the object is updated, which will mean that you'll have to make a new ArcShape every time it's drawn Animation is more for performing transformations on Views and other UI components.

Something like:

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;  
}

Hope this gets you on the right track.