安卓动画阿尔法

我看到动画了:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:fromAlpha="0.2"
android:toAlpha="1.0"
android:duration="500"/>
</set>

ImageView:

<ImageView
android:id="@+id/listViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/settings"
android:alpha="0.2"/>

及密码:

final Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
final ImageView iv = (ImageView) findViewById(R.id.listViewIcon);
anim .setFillAfter(true);
iv.startAnimation(anim);

所以一开始我有 ImageView和 alpha 0.2,最后我想有 ImageView和 alpha 1。但它不是这样工作的-当动画开始时,会添加更多的 alpha,动画完成时使用 alpha 0.2

我必须改变动画我的形象从 0.21

我已经检查了不同的设置-我设置 android:alpha="1.0"fromAlpa="1.0"toAlpha="0.2"它的工作原理与我预期的一样-从阿尔法 10.2。看起来 ImageView的 alpha 乘以动画中的 alpha..。

119962 次浏览

Try this

AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
animation1.setDuration(1000);
animation1.setStartOffset(5000);
animation1.setFillAfter(true);
iv.startAnimation(animation1);
<ImageView
android:id="@+id/listViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/settings"/>

Remove android:alpha=0.2 from XML-> ImageView.

Hm...

The thing is wrong, and possibly in the proper operation of the animations in the Android API.

The fact is that when you set in your code alpha value of 0.2f is based on the settings in the xml file for android it means that :

0.2f = 0.2f of 0.2f (20% from 100%) ie from 0.2f / 5 = 0.04f
1f = 0.2f

So your animation in fact works from 0.04f to 0.2f

flag setFillAfter(true) certainly works, but you need to understand that at the end of your animation ImageView will have the alpha value 0.2f instead of one, because you specify 0.2f as marginally acceptable value in the animation (a kind of maximum alpha channel).

So if you want to have the desired result shall carry all your logic to your code and manipulate animations in code instead of determining in xml.

You should understand that your animations directly depends of two things:

  • LayoutParams of Animated View
  • Animation parameters.

Animation parameters manipulate your LayoutParams in setFillAfter\setFillBefore methods.

Setting alpha to 1 before starting the animation worked for me:

AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
animation1.setDuration(500);
iv.setAlpha(1f);
iv.startAnimation(animation1);

At least on my tests, there's no flickering because of setting alpha before starting the animation. It just works fine.

The "setStartOffset" should be smaller, else animation starts at view alpha 0.xf and waits for start offset before animating to 1f. Hope the following code helps.

AlphaAnimation animation1 = new AlphaAnimation(0.1f, 1f);


animation1.setDuration(1000);
animation1.setStartOffset(50);


animation1.setFillAfter(true);


view.setVisibility(View.VISIBLE);


view.startAnimation(animation1);

Might be a little late, but found a lovely solution in the android docs.

//In transition: (alpha from 0 to 0.5)
view.setAlpha(0f);
view.setVisibility(View.VISIBLE);
view.animate()
.alpha(0.5f)
.setDuration(400)
.setListener(null);


//Out transition: (alpha from 0.5 to 0)
view.setAlpha(0.5f)
view.animate()
.alpha(0f)
.setDuration(400)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
});

Kotlin Version

Simply use ViewPropertyAnimator like this:

iv.alpha = 0.2f
iv.animate().apply {
interpolator = LinearInterpolator()
duration = 500
alpha(1f)
startDelay = 1000
start()
}
View.setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
v.alpha = 0f
v.invalidate()
}
MotionEvent.ACTION_UP -> {
v.alpha = 1f
v.invalidate()




}
}
false
}

This my extension, this is an example of change image with FadIn and FadOut :

fun ImageView.setImageDrawableWithAnimation(@DrawableRes() resId: Int, duration: Long = 300) {
if (drawable != null) {
animate()
.alpha(0f)
.setDuration(duration)
.withEndAction {
setImageResource(resId)
animate()
.alpha(1f)
.setDuration(duration)
}


} else if (drawable == null) {
setAlpha(0f)
setImageResource(resId)
animate()
.alpha(1f)
.setDuration(duration)
}
}
fun View.toggleAlpha(isShow: Boolean, delay: Long = 200, invisibleMode: Int = View.GONE) {
if (isShow) animateAlpha(View.VISIBLE, delay) else animateAlpha(invisibleMode, delay)
}


fun View.animateAlpha(visibility: Int, delay: Long = 200) {
if (visibility == View.VISIBLE) {
setVisibility(View.VISIBLE)
}


val alpha = when (visibility) {
View.GONE, View.INVISIBLE -> 0f
View.VISIBLE -> 1f
else -> 1f
}


animate().apply {
duration = delay
alpha(alpha)
withEndAction {
setVisibility(visibility)
}
}
}