在android中为imageview平滑淡出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20782260/
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
Making a smooth fade out for imageview in android
提问by denza
I managed to make the imageView dissapear after 10 seconds(a tutorial image on my mainActivity). But i want to make a smooth fade out because like this it doesn`t look good, can anyone refer me to any good tutorial
我设法使 imageView 在 10 秒后消失(我的 mainActivity 上的教程图像)。但我想平滑淡出,因为这样看起来不太好,谁能给我推荐任何好的教程
img=(ImageView)findViewById(R.id.ImageTutorial);
if(getIntent()!=null)
{
Bundle extras = getIntent().getExtras();
String TutorialDemo=extras !=null? extras.getString("TutorialDemo"):"false";
if(TutorialDemo.equals("true"))
{
Runnable mRunnable;
Handler mHandler=new Handler();
mRunnable=new Runnable() {
@Override
public void run() {
img.setVisibility(View.GONE); //This will remove the View. and free s the space occupied by the View
}
};
mHandler.postDelayed(mRunnable,10*900);
}
else
{
img.setVisibility(View.GONE);
}
}
here is the image view xml
这是图像视图xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fullview"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical" >
.
.
.
.
<ImageView
android:contentDescription="tutorial"
android:id="@+id/ImageTutorial"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:background="@drawable/tutorial"
android:layout_marginTop="40dp"
android:gravity="center"
/>
</LinearLayout>
回答by Const
Replace img.setVisibility(View.GONE) in your code with a call to fadeOutAndHideImage(img) which is defined like this:
将代码中的 img.setVisibility(View.GONE) 替换为对fadeOutAndHideImage(img) 的调用,其定义如下:
private void fadeOutAndHideImage(final ImageView img)
{
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setDuration(1000);
fadeOut.setAnimationListener(new AnimationListener()
{
public void onAnimationEnd(Animation animation)
{
img.setVisibility(View.GONE);
}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationStart(Animation animation) {}
});
img.startAnimation(fadeOut);
}
It will apply the fade out animation first, then will hide the image view.
它将首先应用淡出动画,然后隐藏图像视图。
回答by Adhikari Bishwash
take hint from this code snippet. The code required goes like this-
从这个代码片段中获取提示。所需的代码是这样的——
Animation fadeOut = new AlphaAnimation(1, 0); // the 1, 0 here notifies that we want the opacity to go from opaque (1) to transparent (0)
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setStartOffset(500); // Start fading out after 500 milli seconds
fadeOut.setDuration(1000); // Fadeout duration should be 1000 milli seconds
Now set this to an element say an image view -
现在将其设置为一个元素,比如图像视图 -
myImageView.setAnimation(fadeOut);