Sub*_*bby 6 animation android view rect
我创建了一个扩展的类,View我将在一个布局中引用它,以便在屏幕上绘制.
这个类只是表示一个Rectangle,我希望矩形的长度一直减小到0.
构造函数:
public CardAnimationNew(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(getResources().getColor(R.color.card_grey_underline));
}
Run Code Online (Sandbox Code Playgroud)
onMeasure:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mRectangle == null) {
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
mRectangle = new Rect(0, 0, mWidth, mHeight);
animateRectangle();
}
}
Run Code Online (Sandbox Code Playgroud)
animateRectangle:
private void animateRectangle() {
mObjectAnimator = ObjectAnimator.ofFloat(mRectangle, "width", 5);
mObjectAnimator.setDuration(1000);
mObjectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
invalidate();
}
});
mObjectAnimator.start();
}
Run Code Online (Sandbox Code Playgroud)
onDraw有:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(mRectangle, mPaint);
}
Run Code Online (Sandbox Code Playgroud)
该问题是,Rect是不是动画的.它保持相同的长度.我哪里错了?
根据文档,该属性需要在应用动画制作器的类中使用setter。
此名称将用于派生一个setter函数,该函数将被调用以设置动画值。
关键是Rect没有setWidth()或setHeight()。
此外矩形不具有宽度和高度属性,顺便能够绘制它的四个坐标(top,left,right,bottom),并达到你需要的效果。(只需将width作为Math.abs(left - right)和height作为Math.abs(top - bottom))。
为了使它可以在Rect上工作(或者像下面的示例中那样,在RectF上工作-如果您需要在Rect上使用,只需使用int setter而不是float即可),则需要通过添加所需属性的setter来对其进行子类化:
private class AnimatableRectF extends RectF{
public AnimatableRectF() {
super();
}
public AnimatableRectF(float left, float top, float right, float bottom) {
super(left, top, right, bottom);
}
public AnimatableRectF(RectF r) {
super(r);
}
public AnimatableRectF(Rect r) {
super(r);
}
public void setTop(float top){
this.top = top;
}
public void setBottom(float bottom){
this.bottom = bottom;
}
public void setRight(float right){
this.right = right;
}
public void setLeft(float left){
this.left = left;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以按常规方式对其进行动画处理……在此示例中,对一个名为mCurrentRect的AnimatableRectF进行动画处理以执行翻译:
float translateX = 50.0f;
float translateY = 50.0f;
ObjectAnimator animateLeft = ObjectAnimator.ofFloat(mCurrentRect, "left", mCurrentRect.left, mCurrentRect.left+translateX);
ObjectAnimator animateRight = ObjectAnimator.ofFloat(mCurrentRect, "right", mCurrentRect.right, mCurrentRect.right+translateX);
ObjectAnimator animateTop = ObjectAnimator.ofFloat(mCurrentRect, "top", mCurrentRect.top, mCurrentRect.top+translateY);
ObjectAnimator animateBottom = ObjectAnimator.ofFloat(mCurrentRect, "bottom", mCurrentRect.bottom, mCurrentRect.bottom+translateY);
animateBottom.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
postInvalidate();
}
});
AnimatorSet rectAnimation = new AnimatorSet();
rectAnimation.playTogether(animateLeft, animateRight, animateTop, animateBottom);
rectAnimation.setDuration(1000).start();
Run Code Online (Sandbox Code Playgroud)
我知道这不是解决问题的复制/粘贴解决方案,但我希望这段代码可以帮助您了解如何解决问题!
| 归档时间: |
|
| 查看次数: |
3790 次 |
| 最近记录: |