我有一个主要活动,它有一个从顶部滑入的图像视图。
我想以 2 秒的延迟淡出此图像视图。 然后立即让一个片段从底部滑入主活动。
如何延迟淡出图像视图?
在 xml 或新的 java 类中,我在哪里定义片段?
如何为片段设置动画?
主要活动.java
public class MainActivity extends AppCompatActivity {
LinearLayout ml;
Animation uptodown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
ml = (LinearLayout) findViewById(R.id.ml);
uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
ml.setAnimation(uptodown);
}
activity_main.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/backgroundcolour">
<LinearLayout
android:id="@+id/ml"
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical">
<ImageView
<!-- my imageview -->
/>
</LinearLayout>
向上.xml在分辨率/动画中
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="800"
android:fromYDelta="-100%p"
android:fromXDelta="0%p"/>
</set>
在xml的末尾添加一个FrameLayout,如下所示:
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
并将您的片段添加到此框架布局中,如下所示:
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
android.support.v4.app.Fragment newFragment;
newFragment = new YourFragment();
transaction.replace(R.id.frame_layout, newFragment);
transaction.commit();
添加幻灯片动画,例如:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>
并将其添加到您的代码中:
FrameLayout frameLayout = findViewById(R.id.frame_layout);
Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_up);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
ml.setAnimation(fadeOut);
frameLayout.setAnimation(slide_up);
}
},2000);