Java 布局之间的淡入效果
作为对象,我将在两个布局之间重现淡入淡出效果。 现在我有了这种情况:Java 布局之间的淡入效果,java,android,layout,fade,Java,Android,Layout,Fade,作为对象,我将在两个布局之间重现淡入淡出效果。 现在我有了这种情况: LinearLayout l; LinearLayout l2; 在它们之间切换我用过的 l.setVisibility(View.GONE); l2.setVisibility(View.VISIBLE); 我想在这个过渡之间添加淡入淡出效果,我该怎么做?使用R.anim.fade\u out和R.anim.fade\u in,您可以创建一个这样做的动画。我自己对此不太了解,但这里有一个关于android动画的教程: 另
LinearLayout l;
LinearLayout l2;
在它们之间切换我用过的
l.setVisibility(View.GONE);
l2.setVisibility(View.VISIBLE);
我想在这个过渡之间添加淡入淡出效果,我该怎么做?使用R.anim.fade\u out和R.anim.fade\u in,您可以创建一个这样做的动画。我自己对此不太了解,但这里有一个关于android动画的教程: 另外,这篇教程不是我的,所以我没有学分 编辑:
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(100);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
l.setLayoutAnimation(controller);
淡出动画
public static Animation runFadeOutAnimationOn(Activity ctx, View target) {
Animation animation = AnimationUtils.loadAnimation(ctx,android.R.anim.fade_out);
target.startAnimation(animation);
return animation;
}
我猜你可以试试这样的东西,我复制粘贴了教程中的动画,我不知道它到底做什么,因为我没有Android开发经验。另一个例子可能是这里是一个在两个布局之间交叉淡入淡出的工作解决方案:
public class CrossFadeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crossfade);
final View l1 = findViewById(R.id.l1);
final View l2 = findViewById(R.id.l2);
final Animation fadeOut = AnimationUtils.loadAnimation(CrossFadeActivity.this, R.anim.fade_out);
final Animation fadeIn = AnimationUtils.loadAnimation(CrossFadeActivity.this, R.anim.fade_in);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
fadeOut.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
l1.setVisibility(View.GONE);
}
});
l1.startAnimation(fadeOut);
l2.setVisibility(View.VISIBLE);
l2.startAnimation(fadeIn);
}
});
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
fadeOut.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
l2.setVisibility(View.GONE);
}
});
l2.startAnimation(fadeOut);
l1.setVisibility(View.VISIBLE);
l1.startAnimation(fadeIn);
}
});
}
}
crossfade.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/l1"
android:layout_width="fill_parent"
android:layout_height="300dip"
android:orientation="vertical"
android:background="@drawable/someimage"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<RelativeLayout
android:id="@+id/l2"
android:layout_width="fill_parent"
android:layout_height="300dip"
android:orientation="vertical"
android:background="@drawable/someimage2"
android:visibility="gone"
>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</RelativeLayout>
使用android.R.anim.fade_-in,您将不需要创建res/anim/fade_-in.xml文件
Android在Android.R.anim上有一个包含一些有用动画的软件包:,因为您可以使用Android 3.0
android:animateLayoutChanges="true"
在xml布局中,运行时对此特定布局内容的任何更改都将设置动画。例如,在您的情况下,需要为l和l2的父布局的父级设置此属性,例如
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="me.kalem.android.RegistrationActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:padding="20dp">
<LinearLayout
android:id="@+id/l1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible">
........
</LinearLayout>
<LinearLayout
android:id="@+id/l2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone">
........
</LinearLayout>
</LinearLayout>
........
........
现在,在运行时隐藏l1和显示l2将被设置动画。我想使用android.R.anim.fade\u in和fade\u out,我已经阅读了一些关于方法重写PendingTransition的内容。但我不明白如何将动画设置为线性布局。你能帮我吗?本教程介绍了anim.fade_in和fade_out,如果你向下滚动一下,你可以看到示例代码!我想这应该可以让你走了。你也可以使用
.bringToFront()
方法来反转相关布局的z顺序,以防用户需要与前景交互。这个答案只涉及动画方面。
final Animation fadeIn = AnimationUtils.loadAnimation(CrossFadeActivity.this, android.R.anim.fade_in);
android:animateLayoutChanges="true"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="me.kalem.android.RegistrationActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:padding="20dp">
<LinearLayout
android:id="@+id/l1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible">
........
</LinearLayout>
<LinearLayout
android:id="@+id/l2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone">
........
</LinearLayout>
</LinearLayout>