【问题标题】:Animate an imageview out of the activity and bring a fragment into the activity from the bottom: Android将图像视图从活动中动画化,并从底部将片段带入活动:Android
【发布时间】:2018-08-25 09:49:46
【问题描述】:

我有一个主要活动,它有一个从顶部滑入的图像视图。

我想以 2 秒的延迟淡出这个 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>

【问题讨论】:

    标签: java android android-fragments android-animation android-transitions


    【解决方案1】:

    在您的 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      相关资源
      最近更新 更多