com.example.wechat.widget.FlippingImageView派生自ImageView,主要实现在登录时候的图片旋转动画:
package com.example.wechat.widget;主要函数是startAnimation、clearRotateAnimation来实现开始播放动画以及清除动画。
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
public class FlippingImageView extends ImageView {
private RotateAnimation mAnimation;
private boolean mIsHasAnimation;
public FlippingImageView(Context context) {
super(context);
}
public FlippingImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FlippingImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private void setRotateAnimation() {
if (mIsHasAnimation == false && getWidth() > 0 && getVisibility() == View.VISIBLE) {
mIsHasAnimation = true;
mAnimation = new RotateAnimation(getWidth() / 2.0F, getHeight() / 2.0F, RotateAnimation.Mode.Y);
mAnimation.setDuration(1000L);
mAnimation.setInterpolator(new LinearInterpolator());
mAnimation.setRepeatCount(-1);
mAnimation.setRepeatMode(Animation.RESTART);
setAnimation(mAnimation);
}
}
private void clearRotateAnimation() {
if (mIsHasAnimation) {
mIsHasAnimation = false;
setAnimation(null);
mAnimation = null;
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setRotateAnimation();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
clearRotateAnimation();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (w > 0) {
setRotateAnimation();
}
}
public void startAnimation() {
if (mIsHasAnimation) {
super.startAnimation(mAnimation);
}
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == View.INVISIBLE || visibility == View.GONE) {
clearRotateAnimation();
} else {
setRotateAnimation();
}
}
}
其他重载函数均根据实际情况来显示显示或清除动画。
具体使用方法:
mFivIcon = (FlippingImageView) findViewById(R.id.loadingdialog_fiv_icon);其中动画效果类:
mFivIcon.startAnimation();
package com.example.wechat.widget;具体实现原理不再细究,会使用即可。
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class RotateAnimation extends Animation {
private Camera mCamera;
private float mCenterX;
private float mCenterY;
private Mode mMode;
public RotateAnimation(float centerX, float centerY, Mode mode) {
mCenterX = centerX;
mCenterY = centerY;
mMode = mode;
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float deg = 0.0F + 360.0F * interpolatedTime;
Matrix matrix = t.getMatrix();
mCamera.save();
if (mMode == Mode.X)
mCamera.rotateX(deg);
if (mMode == Mode.Y)
mCamera.rotateY(deg);
if (mMode == Mode.Z)
mCamera.rotateZ(deg);
mCamera.getMatrix(matrix);
mCamera.restore();
matrix.preTranslate(-mCenterX, -mCenterY);
matrix.postTranslate(mCenterX, mCenterY);
}
public enum Mode {
X, Y, Z;
}
}