17、com.example.wechat.widget.FlippingImageView图片旋转动画

本文介绍了一个名为 FlippingImageView 的自定义视图类,该类继承自 ImageView 并实现了在登录页面进行图片旋转动画的功能。通过设置 RotateAnimation,用户可以在可见的 ImageView 上实现流畅的动画效果。文章详细解释了关键函数 startAnimation 和 clearRotateAnimation 的作用,并提供了实际应用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

com.example.wechat.widget.FlippingImageView派生自ImageView,主要实现在登录时候的图片旋转动画:

package com.example.wechat.widget;

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();
}
}
}
主要函数是startAnimation、clearRotateAnimation来实现开始播放动画以及清除动画。
其他重载函数均根据实际情况来显示显示或清除动画。

具体使用方法:
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;
}
}
具体实现原理不再细究,会使用即可。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

asmcvc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值