下面列出了怎么用android.view.animation.AlphaAnimation的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testResetViewStatus() {
ActivityController<NavigationSourceUtility.TestActivity> controller = Robolectric.buildActivity(NavigationSourceUtility.TestActivity.class).create().start().resume();
NavigationSourceUtility.TestActivity testActivity = controller.get();
View view = new View(testActivity);
view.setTranslationX(1);
view.setTranslationY(1);
view.setScaleX(2.0f);
view.setScaleY(2.0f);
view.setRotation(1.0f);
view.setRotationX(1.0f);
view.setRotationY(1.0f);
view.setAlpha(0.5f);
view.startAnimation(new AlphaAnimation(0.0f, 1.0f));
AnimatorUtility.resetViewStatus(view);
assertEquals(0.0f, view.getTranslationX(), 0.0f);
assertEquals(0.0f, view.getTranslationY(), 0.0f);
assertEquals(1.0f, view.getScaleX(), 0.0f);
assertEquals(1.0f, view.getScaleY(), 0.0f);
assertEquals(0.0f, view.getRotation(), 0.0f);
assertEquals(0.0f, view.getRotationX(), 0.0f);
assertEquals(0.0f, view.getRotationY(), 0.0f);
assertEquals(1.0f, view.getAlpha(), 0.0f);
assertNull(view.getAnimation());
}
public void showLogo() {
_isLogoVisible = true;
runOnUiThread(new Runnable() {
@Override
public void run() {
Animation fadeIn = new AlphaAnimation(0, 0.8f);
fadeIn.setInterpolator(new AccelerateInterpolator()); //and this
fadeIn.setStartOffset(0);
fadeIn.setDuration(500);
//
AnimationSet animation = new AnimationSet(false); //change to false
animation.addAnimation(fadeIn);
animation.setFillAfter(true);
RelativeLayout ivLogo = (RelativeLayout) findViewById(R.id.logo);
ivLogo.startAnimation(animation);
}
});
}
/**
* 设置透明度
* @param viewId
* @param value
* @return
*/
@SuppressLint("NewApi")
public ViewHolder setAlpha(int viewId, float value)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
getView(viewId).setAlpha(value);
} else
{
// Pre-honeycomb hack to set Alpha value
AlphaAnimation alpha = new AlphaAnimation(value, value);
alpha.setDuration(0);
alpha.setFillAfter(true);
getView(viewId).startAnimation(alpha);
}
return this;
}
private AnimationSet getEntryAnimation(int inAnimationDuration) {
//In
AnimationSet mInAnimationSet = new AnimationSet(false);
TranslateAnimation mSlideInAnimation = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_SELF, -1.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f);
mSlideInAnimation.setFillAfter(true);
AlphaAnimation mFadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
mFadeInAnimation.setFillAfter(true);
mInAnimationSet.addAnimation(mSlideInAnimation);
mInAnimationSet.addAnimation(mFadeInAnimation);
mInAnimationSet.setDuration(inAnimationDuration);
return mInAnimationSet;
}
private void initAnimations() {
Animation inScaleAnimation = new ScaleAnimation( 1.2f, 1, 1.2f, 1,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Animation inAlphaAnimation = new AlphaAnimation(0.0f, 1f);
AnimationSet inAnimationSet = new AnimationSet(true);
inAnimationSet.setDuration(ANIMATION_DURATION);
inAnimationSet.addAnimation(inScaleAnimation);
inAnimationSet.addAnimation(inAlphaAnimation);
mInAnimation = inAnimationSet;
Animation outScaleAnimation = new ScaleAnimation(1, 1.4f, 1, 1.4f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Animation outAlphaAnimation = new AlphaAnimation(1f, 0f);
AnimationSet outAnimationSet = new AnimationSet(true);
outAnimationSet.setDuration(ANIMATION_DURATION);
outAnimationSet.addAnimation(outScaleAnimation);
outAnimationSet.addAnimation(outAlphaAnimation);
mOutAnimation = outAnimationSet;
}
public static void setAdapterInsertAnimation(final View aCard, int row, int height) {
final int ANIMATION_DURATION = 650;
final int BASE_DELAY = 50;
TranslateAnimation translationAnimation = new TranslateAnimation(0,0, height,0);
AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
final AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(translationAnimation);
animationSet.addAnimation(alphaAnimation);
animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
animationSet.setFillAfter(true);
animationSet.setFillBefore(true);
animationSet.setDuration(ANIMATION_DURATION + row * BASE_DELAY);
aCard.setAnimation(animationSet);
}
@Override
public Animation createOptionDisappearAnimation(OptionButton optionButton, int index) {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation= new TranslateAnimation(
0
,getYMenuButton().getX() - optionButton.getX()
,0
,getYMenuButton().getY() - optionButton.getY()
);
translateAnimation.setDuration(getOptionSD_AnimationDuration());
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(getOptionSD_AnimationDuration());
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(alphaAnimation);
//设置动画延时
animationSet.setStartOffset(60*(getOptionPositionCount() - index));
return animationSet;
}
public void hideLogo() {
_isLogoVisible = false;
runOnUiThread(new Runnable() {
@Override
public void run() {
Animation fadeOut = new AlphaAnimation(0.8f, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(0);
fadeOut.setDuration(500);
//
AnimationSet animation = new AnimationSet(false); //change to false
animation.addAnimation(fadeOut);
animation.setFillAfter(true);
RelativeLayout ivLogo = (RelativeLayout) findViewById(R.id.logo);
ivLogo.startAnimation(animation);
}
});
}
private void setOutAnimation(float[] direction, Interpolator interpolator, long duration) {
Animation push = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, direction[0], Animation.RELATIVE_TO_PARENT, direction[1],
Animation.RELATIVE_TO_PARENT, direction[2], Animation.RELATIVE_TO_PARENT, direction[3]
);
push.setInterpolator(interpolator);
push.setDuration(duration);
Animation fade = new AlphaAnimation(1, 0);
fade.setDuration(duration);
fade.setInterpolator(interpolator);
AnimationSet set = new AnimationSet(false);
set.addAnimation(push);
set.addAnimation(fade);
outAnimation = set;
}
/**
* Animates {@link ImageView} with "fade-in" effect
*
* @param imageView {@link ImageView} which display image in
* @param durationMillis The length of the animation in milliseconds
*/
public static void animate(View imageView, int durationMillis) {
if (imageView != null) {
AlphaAnimation fadeImage = new AlphaAnimation(0, 1);
fadeImage.setDuration(durationMillis);
fadeImage.setInterpolator(new DecelerateInterpolator());
imageView.startAnimation(fadeImage);
}
}
private void fadeOnAck(@Order.Status String status, View view) {
clearAnimation(view);
if (status.equals(ACKNOWLEDGED)) {
AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 0.9f);
alphaAnimation.setDuration(700);
alphaAnimation.setRepeatMode(Animation.REVERSE);
alphaAnimation.setRepeatCount(Animation.INFINITE);
view.setAnimation(alphaAnimation);
alphaAnimation.start();
// color animation
/* ObjectAnimator slide = ObjectAnimator.ofObject(view,
"backgroundColor",
new ArgbEvaluator(),
ContextCompat.getColor(App.getContext(), R.color.white),
ContextCompat.getColor(App.getContext(), R.color.md_deep_orange_50));*/
/* ObjectAnimator slide = ObjectAnimator.ofFloat(view,
"alpha", 0.3f, 0.9f);
slide.setInterpolator(new AccelerateDecelerateInterpolator());
slide.setDuration(700);
slide.setRepeatCount(ValueAnimator.INFINITE);
slide.setRepeatMode(ValueAnimator.REVERSE);
slide.start();*/
}
}
public void onSensorData(final Sensor sensor) {
if (animatingSensors.contains(sensor.getType()) || holdingBackNextFlash) {
return;
}
holdingBackNextFlash = true;
postDelayed(new Runnable() {
@Override
public void run() {
holdingBackNextFlash = false;
}
}, FlashDuration / 2);
post(new Runnable() {
@Override
public void run() {
Integer id = ViewIds.get(sensor.getType());
if (id == null) {
return;
}
View v = findViewById(id);
if (v == null) {
return;
}
animatingSensors.add(sensor.getType());
AlphaAnimation fadeIn = new AlphaAnimation(FadeAlpha, FlashAlpha);
fadeIn.setDuration(FlashDuration);
AlphaAnimation fadeOut = new AlphaAnimation(FlashAlpha, FadeAlpha);
fadeOut.setDuration(FlashDuration);
fadeOut.setStartOffset(fadeIn.getDuration() + FlashDuration / 2);
AnimationSet set = new AnimationSet(false);
set.addAnimation(fadeIn);
set.addAnimation(fadeOut);
set.setAnimationListener(new FadeAnimListener(sensor.getType()));
set.setFillAfter(true);
v.startAnimation(set);
}
});
}
/**
* 透明度 Alpha
*/
public static Animation getAlphaAnimation(float fromAlpha, float toAlpha,
long durationMillis) {
AlphaAnimation alpha = new AlphaAnimation(fromAlpha, toAlpha);
alpha.setDuration(durationMillis);
alpha.setFillAfter(true);
return alpha;
}
private void setAlpha(View view, float alpha, long durationMillis) {
if (Build.VERSION.SDK_INT < 11) {
final AlphaAnimation animation = new AlphaAnimation(alpha, alpha);
animation.setDuration(durationMillis);
animation.setFillAfter(true);
view.startAnimation(animation);
} else {
view.setAlpha(alpha);
}
}
/**
@brief Hide the wrapper.
*/
public void hide()
{
mUI_LoadingProgress.stop();
AlphaAnimation anim = new AlphaAnimation(1.0f, 0.0f);
anim.setDuration(1000);
anim.setRepeatCount(0);
this.startAnimation(anim);
this.setVisibility(View.INVISIBLE);
}
/**
* Animates {@link ImageView} with "fade-in" effect
*
* @param imageView {@link ImageView} which display image in
* @param durationMillis The length of the animation in milliseconds
*/
public static void animate(View imageView, int durationMillis) {
if (imageView != null) {
AlphaAnimation fadeImage = new AlphaAnimation(0, 1);
fadeImage.setDuration(durationMillis);
fadeImage.setInterpolator(new DecelerateInterpolator());
imageView.startAnimation(fadeImage);
}
}
public static AlphaAnimation getAlphaAnimation(float start, float end, long durationMillis) {
AlphaAnimation alphaAnimation = new AlphaAnimation(start, end);
alphaAnimation.setDuration(durationMillis);
alphaAnimation.setFillEnabled(true);
alphaAnimation.setFillAfter(true);
return alphaAnimation;
}
/**
* Create push down animation for entering.
*
* @return Animation
*/
public static Animation pushDownIn() {
AnimationSet animationSet = new AnimationSet(true);
animationSet.setFillAfter(true);
animationSet.addAnimation(new TranslateAnimation(0, 0, -100, 0));
animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
return animationSet;
}
@Override
protected Animation initExitAnimation() {
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0F, 0.0F);
alphaAnimation.setDuration(300L);
alphaAnimation.setInterpolator(new AccelerateInterpolator());
return alphaAnimation;
}
static Animation makeOpenCloseAnimation(Context context, float startScale,
float endScale, float startAlpha, float endAlpha) {
AnimationSet set = new AnimationSet(false);
ScaleAnimation scale = new ScaleAnimation(startScale, endScale, startScale, endScale,
Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
scale.setInterpolator(DECELERATE_QUINT);
scale.setDuration(ANIM_DUR);
set.addAnimation(scale);
AlphaAnimation alpha = new AlphaAnimation(startAlpha, endAlpha);
alpha.setInterpolator(DECELERATE_CUBIC);
alpha.setDuration(ANIM_DUR);
set.addAnimation(alpha);
return set;
}
public static AnimationSet getOutAnimation(Context context) {
AnimationSet out = new AnimationSet(context, null);
AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
alpha.setDuration(150);
ScaleAnimation scale = new ScaleAnimation(1.0f, 0.6f, 1.0f, 0.6f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scale.setDuration(150);
out.addAnimation(alpha);
out.addAnimation(scale);
return out;
}
public void show() {
this.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
this.setClickable(false);
((ViewGroup) ((Activity) getContext()).getWindow().getDecorView()).addView(this);
AlphaAnimation startAnimation = new AlphaAnimation(0.0f, 1.0f);
startAnimation.setDuration(APPEARING_ANIMATION_DURATION);
startAnimation.setFillAfter(true);
this.startAnimation(startAnimation);
mIsShowing = true;
}
/**
* <p>
* Initialize the progress bar's default values:
* </p>
* <ul>
* <li>progress = 0</li>
* <li>max = 100</li>
* <li>animation duration = 4000 ms</li>
* <li>indeterminate = false</li>
* <li>behavior = repeat</li>
* </ul>
*/
private void initProgressBar() {
mMax = 100;
mProgress = 0;
mSecondaryProgress = 0;
mIndeterminate = false;
mOnlyIndeterminate = false;
mDuration = 4000;
mBehavior = AlphaAnimation.RESTART;
mMinWidth = 24;
mMaxWidth = 48;
mMinHeight = 24;
mMaxHeight = 48;
}
private void init(Context context, AttributeSet attrs) {
if(attrs == null) {
initializeWithDefaults();
} else {
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.AnimateImageView);
if(attributes == null) {
initializeWithDefaults();
}
else {
mShouldAnimateChanges = attributes.getBoolean(R.styleable.AnimateImageView_animate_changes, false);
int inAnimationResId = attributes.getResourceId(R.styleable.AnimateImageView_in_animation, 0);
int outAnimationResId = attributes.getResourceId(R.styleable.AnimateImageView_out_animation, 0);
attributes.recycle();
if(inAnimationResId == 0) {
mInAnimation = new AlphaAnimation(0.0f, 1.0f);
mInAnimation.setDuration(400);
mInAnimation.setFillAfter(true);
} else {
mInAnimation = AnimationUtils.loadAnimation(context, inAnimationResId);
}
if(outAnimationResId == 0) {
mOutAnimation = new AlphaAnimation(1.0f, 0.0f);
mOutAnimation.setDuration(150);
mOutAnimation.setFillAfter(true);
} else {
mOutAnimation = AnimationUtils.loadAnimation(context, outAnimationResId);
}
mInAnimation.setAnimationListener(this);
mOutAnimation.setAnimationListener(this);
}
}
}
private void initAnim() {
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
ScaleAnimation scaleAnim = new ScaleAnimation(1,2,1,2, Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
mAnimationSet = new AnimationSet(true);
mAnimationSet.addAnimation(alphaAnimation);
mAnimationSet.addAnimation(scaleAnim);
mAnimationSet.setDuration(DURATION);
}
public void animateSmallMicAlpha() {
alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(500);
alphaAnimation.setRepeatMode(Animation.REVERSE);
alphaAnimation.setRepeatCount(Animation.INFINITE);
smallBlinkingMic.startAnimation(alphaAnimation);
}
private void init(Context context, View target, int tabIndex)
{
this.context = context;
this.target = target;
this.targetTabIndex = tabIndex;
// apply defaults
badgePosition = DEFAULT_POSITION;
badgeMarginH = dipToPixels(DEFAULT_MARGIN_DIP);
badgeMarginV = badgeMarginH;
badgeColor = DEFAULT_BADGE_COLOR;
setTypeface(Typeface.DEFAULT_BOLD);
int paddingPixels = dipToPixels(DEFAULT_LR_PADDING_DIP);
setPadding(paddingPixels, 0, paddingPixels, 0);
setTextColor(DEFAULT_TEXT_COLOR);
fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator());
fadeIn.setDuration(200);
fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setDuration(200);
isShown = false;
if (this.target != null)
{
applyTo(this.target);
}
else
{
show();
}
}
@Override
public EasyLVHolder setAlpha(int viewId, float value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getView(viewId).setAlpha(value);
} else {
AlphaAnimation alpha = new AlphaAnimation(value, value);
alpha.setDuration(0);
alpha.setFillAfter(true);
getView(viewId).startAnimation(alpha);
}
return this;
}
/**
* Animates {@link ImageView} with "fade-in" effect
*
* @param imageView {@link ImageView} which display image in
* @param durationMillis The length of the animation in milliseconds
*/
public static void animate(View imageView, int durationMillis) {
if (imageView != null) {
AlphaAnimation fadeImage = new AlphaAnimation(0, 1);
fadeImage.setDuration(durationMillis);
fadeImage.setInterpolator(new DecelerateInterpolator());
imageView.startAnimation(fadeImage);
}
}
private void setup(@NonNull View bubble) {
float startAlpha = toVisible ? 0 : 1;
float endAlpha = toVisible ? 1 : 0;
AlphaAnimation alphaAnimation = new AlphaAnimation(startAlpha, endAlpha);
alphaAnimation.setDuration(DURATION);
addAnimation(alphaAnimation);
setAnimationListener(new BubbleVisibilityAnimationListener(bubble, toVisible));
//Works around the issue of the animation never starting because the view is GONE
if (bubble.getVisibility() == View.GONE) {
bubble.setVisibility(View.INVISIBLE);
}
}