Java Android 动画 Alpha
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20628973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Animation Alpha
提问by wioskamala
I've got animation:
我有动画:
<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>
and ImageView
:
和ImageView
:
<ImageView
android:id="@+id/listViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/settings"
android:alpha="0.2"/>
and code:
和代码:
final Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
final ImageView iv = (ImageView) findViewById(R.id.listViewIcon);
anim .setFillAfter(true);
iv.startAnimation(anim);
So at the beginning I have ImageView
with alpha 0.2
and at the end I want to have ImageView
with alpha 1
. But it doesn't work like that - when animation starts more alpha is added and animation finish with alpha 0.2
所以一开始我有ImageView
alpha 0.2
,最后我想有ImageView
alpha 1
。但它不是那样工作的 - 当动画开始时添加更多的 alpha 并且动画以 alpha 结束0.2
What do I have to change to animate my image from 0.2
up to 1
?
我有什么改变为从动画我的形象0.2
最多1
?
I've checked with different settings - I set android:alpha="1.0"
, fromAlpa="1.0"
, toAlpha="0.2"
it works like I expected - from alpha 1
to 0.2
. It looks like alpha from ImageView
is multiplied by alpha from animation...
我检查了不同的设置 - 我设置了android:alpha="1.0"
, fromAlpa="1.0"
,toAlpha="0.2"
它的工作原理和我预期的一样 - 从 alpha1
到0.2
. 看起来 alpha fromImageView
乘以动画中的 alpha ...
回答by Vaibhav Agarwal
Try this
尝试这个
AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
animation1.setDuration(1000);
animation1.setStartOffset(5000);
animation1.setFillAfter(true);
iv.startAnimation(animation1);
回答by Pavlik Zavarski
<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.
android:alpha=0.2
从 XML-> ImageView 中删除。
回答by Sergey Shustikov
Hm...
嗯...
The thing is wrong, and possibly in the proper operation of the animations in the Android API.
事情是错误的,可能是在 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 alpha 值时,基于 android xml 文件中的设置,这意味着:
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
所以你的动画实际上从 0.04f 到 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.2finstead of one, because you specify 0.2f as marginally acceptable value in the animation (a kind of maximum alpha channel).
标志setFillAfter(true)
当然有效,但您需要了解在动画结束时ImageView
将具有 alpha 值0.2f而不是 1,因为您将 0.2f 指定为动画中可接受的边缘值(一种最大 alpha 通道)。
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.
因此,如果您想获得所需的结果,请将您的所有逻辑都带入您的代码并在代码中操作动画,而不是在 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\setFillBeforemethods.
动画参数在setFillAfter\setFillBefore方法中操作您的 LayoutParams 。
回答by Iván Pérez
Setting alpha to 1
before starting the animation worked for me:
1
在开始动画之前将 alpha 设置为对我有用:
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.
至少在我的测试中,没有闪烁,因为在开始动画之前设置了 alpha。它工作正常。
回答by Chandrima Bhattacharjee
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.
“ setStartOffset
” 应该更小,否则动画从视图 alpha 开始0.xf
并在动画到1f
. 希望以下代码有所帮助。
AlphaAnimation animation1 = new AlphaAnimation(0.1f, 1f);
animation1.setDuration(1000);
animation1.setStartOffset(50);
animation1.setFillAfter(true);
view.setVisibility(View.VISIBLE);
view.startAnimation(animation1);
回答by Julián M.
Might be a little late, but found a lovely solutionin the android docs.
可能有点晚了,但在 android 文档中找到了一个可爱的解决方案。
//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);
}
});
回答by aminography
? Kotlin Version
? Kotlin Version
Simply use ViewPropertyAnimator
like this:
只需ViewPropertyAnimator
像这样使用:
iv.alpha = 0.2f
iv.animate().apply {
interpolator = LinearInterpolator()
duration = 500
alpha(1f)
startDelay = 1000
start()
}
回答by G?rkem KARA
View.setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
v.alpha = 0f
v.invalidate()
}
MotionEvent.ACTION_UP -> {
v.alpha = 1f
v.invalidate()
}
}
false
}