【发布时间】:2018-01-06 18:49:57
【问题描述】:
所以在布局中我有三个按钮,对于控制流,我使用一个开关来确定单击每个按钮时会发生什么行为。
我的问题是,当单击每个按钮转到下一个活动时,我希望淡入淡出过渡取代默认过渡。
我认为我的代码已关闭,但是当我运行模拟器时,我没有看到过渡变化?我是 Android 新手,我不知道我做错了什么,我为按钮布局附加了我的 Java 代码和 XML,以及我的输入/输出转换 XML。谢谢!!
package com.example.jonathan.om11;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by Jonathan on 12/28/17.
*/
public class Tab1 extends Fragment implements View.OnClickListener {
Button toEnv1Page;
Button toEnv2Page;
Button toEnv3Page;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
toEnv1Page = (Button) rootView.findViewById(R.id.envBtn1);
toEnv1Page.setOnClickListener(this);
toEnv2Page = (Button) rootView.findViewById(R.id.envBtn2);
toEnv2Page.setOnClickListener(this);
toEnv3Page = (Button) rootView.findViewById(R.id.envBtn3);
toEnv3Page.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.envBtn1:
Intent i = new Intent("android.intent.action.Env1Page");
startActivity(i);
android.support.v4.app.FragmentTransaction fAnimation = this.getActivity().getSupportFragmentManager().beginTransaction();
fAnimation.setCustomAnimations(android.R.anim.fade_in, R.anim.fade_out);
fAnimation.commit();
break;
case R.id.envBtn2:
Intent ii = new Intent("android.intent.action.Env2Page");
startActivity(ii);
android.support.v4.app.FragmentTransaction f2Animation = this.getActivity().getSupportFragmentManager().beginTransaction();
f2Animation.setCustomAnimations(android.R.anim.fade_in, R.anim.fade_out);
f2Animation.commit();
break;
case R.id.envBtn3:
Intent iii = new Intent("android.intent.action.Env3Page");
startActivity(iii);
android.support.v4.app.FragmentTransaction f3Animation = this.getActivity().getSupportFragmentManager().beginTransaction();
f3Animation.setCustomAnimations(android.R.anim.fade_in, R.anim.fade_out);
f3Animation.commit();
break;
default:
break;
}
}
}
布局 XML:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tabsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jonathan.om11.MainTabbedActivity$PlaceholderFragment">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/envBtn1"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/cardview_light_background"
android:text="Rainforest"
android:textSize="40sp" />
<Button
android:id="@+id/envBtn2"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/cardview_light_background"
android:text="Desert"
android:textSize="40sp" />
<Button
android:id="@+id/envBtn3"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/cardview_light_background"
android:text="Fireplace"
android:textSize="40sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
淡入动画 XML:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0">
</set>
淡出动画 XML:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:fillAfter="true"
android:duration="500"
>
</set>
【问题讨论】:
标签: java android android-fragments android-animation android-transitions