[英]Animate an imageview out of the activity and bring a fragment into the activity from the bottom: Android
我有一个主要活动,它的图像视图从顶部滑入。
我想以2秒的延迟淡出此imageview。 然后立即有一个片段从底部滑入主要活动。
如何延迟淡出imageview?
如何在xml或新的Java类中定义片段?
以及如何为片段制作动画?
MainActivity.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>
res / anim中的uptodown.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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.