关于在View进行动画的同时进行裁剪

vincent-pan 发布于 2012/10/27 09:41
阅读 674
收藏 0

问题是这样的,我现在有2个动画效果

package com.mytest;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;


public class MyImageView extends ImageView {


private Rect mClipRect;


public MyImageView(Context context) {
super(context);
}


public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}


@Override
protected void onDraw(Canvas canvas) {
if (mClipRect != null) {
int save = canvas.save();
canvas.clipRect(mClipRect);
super.onDraw(canvas);
canvas.restoreToCount(save);
} else {
super.onDraw(canvas);
}
}


public void setClipRect(int left, int top, int right, int bottom) {
if (mClipRect == null) {
mClipRect = new Rect();
}
mClipRect.set(left, top, right, bottom);
}


}

=============================================================
package com.mytest;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.Transformation;


public class MytestActivity extends Activity implements OnClickListener {


private MyImageView mImageView;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImageView = (MyImageView) findViewById(R.id.myview2);
mImageView.setOnClickListener(this);
}


@Override
public void onClick(View v) {

ScaleAnimation zoomAnimation = new ScaleAnimation(1.0f, 1.0f, 1.0f,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1.0f);
zoomAnimation.setDuration(2000);


ScaleAnimation clipAnimation = new ScaleAnimation(1.0f, 1.0f, 1.0f,
2.0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1.0f) {
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
mImageView.setClipRect(0,
(int) (interpolatedTime * mImageView.getHeight() / 2),
mImageView.getWidth(), mImageView.getHeight());
super.applyTransformation(interpolatedTime, t);
}
};
clipAnimation.setDuration(2000);
clipAnimation.setStartOffset(2000);


AnimationSet set = new AnimationSet(false);
set.addAnimation(zoomAnimation);
set.addAnimation(clipAnimation);
set.setFillAfter(true);
v.startAnimation(set);
}


}


很明显,我是在第一个动画中压缩Y轴,第二个动画中还原的同时进行裁剪,但是在裁剪的过程中,上边界会出现上下波动的问题。求解决方式

加载中
返回顶部
顶部