Cec*_*oCQ 10 java layout android fade
作为对象,我将重现两个布局之间的淡入淡出效果.现在我遇到这种情况:
LinearLayout l;
LinearLayout l2;
Run Code Online (Sandbox Code Playgroud)
要在它们之间切换,我已经习惯了
l.setVisibility(View.GONE);
l2.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)
我希望在这个转录之间添加淡入淡出效果,我该怎么办呢?
Ixx*_*Ixx 22
这是一个在两个布局之间交叉淡入淡出的工作解决方案:
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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
其中l1和l2是2个随机示例布局.诀窍是将它们放在XML中,使它们彼此重叠(例如在RelativeLayout中)与visible/gone,为动画添加侦听器以切换完成时的可见性,并设置必须淡入到可见之前的视图.动画开始,否则动画将不可见.
我把按钮与监听器一起切换布局本身的动画,因为我需要以这种方式实现它,但是点击监听器当然可以在其他地方(如果它只是一个它应该与一些标志结合使用或检查知道如何切换).
这些是动画文件.它们必须存储在文件夹res/anim中:
fade_in.xml
<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
Run Code Online (Sandbox Code Playgroud)
fade_out.xml
<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromAlpha="1.0"
android:toAlpha="0" />
Run Code Online (Sandbox Code Playgroud)
更新:
您可以使用Android API(android.R.fade_in)中的默认fade_in,而不是使用R.anim.fade_in :
final Animation fadeIn = AnimationUtils.loadAnimation(CrossFadeActivity.this, android.R.anim.fade_in);
Run Code Online (Sandbox Code Playgroud)
使用android.R.anim.fade_in,您不需要创建文件res/anim/fade_in.xml.
Android在android.R.anim上有一些包含一些有用动画的软件包:http://developer.android.com/reference/android/R.anim.html
小智 4
使用 R.anim.fade_out 和 .R.anim.fade_in 您可以创建一个动画来执行此操作。我自己对此了解不多,但这里有一个关于 android 动画的教程:Animation Tutorial
PS 这个教程不是我的,因此我不会获得荣誉。
编辑:
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);
Run Code Online (Sandbox Code Playgroud)
淡出动画
public static Animation runFadeOutAnimationOn(Activity ctx, View target) {
Animation animation = AnimationUtils.loadAnimation(ctx,android.R.anim.fade_out);
target.startAnimation(animation);
return animation;
}
Run Code Online (Sandbox Code Playgroud)
我猜你可以尝试这样的事情,我复制粘贴了教程中的动画,我不知道它到底是做什么的,因为我没有 Android 开发经验。另一个例子可以是示例 2
| 归档时间: |
|
| 查看次数: |
17866 次 |
| 最近记录: |