为活动中的特定视图定义某些进入和退出转换

时间:2016-03-01 13:38:44

标签: android animation material-design

我正在使用this文档添加活动转换。我知道我可以为整个活动定义进入和退出过渡,例如explode 但是,如果我想以不同的方式为输入活动的某些视图设置动画,该怎么办?例如,如果我在布局的上半部分有一个视图而在布局的底部有一个视图,我可能想在顶视图上使用向下幻灯片过渡,在底视图上使用向上幻灯片动画。这可以设置吗?

我可以考虑使用scene animationsdocumented here)并从空视图的两侧引入元素,但我的布局很复杂,我经常迭代它,所以跟踪两个不同的布局并不是很理想。

谢谢!

2 个答案:

答案 0 :(得分:2)

终于搞定了!我们的想法是在xml中创建自定义转换并设置目标:

<?xml version="1.0" encoding="utf-8"?>
<transitionSet
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:transitionOrdering="together">
    <slide
        android:slideEdge="top">
        <targets>
            <target android:targetId="@id/toolbar"/>
        </targets>
    </slide>
    <slide
        android:slideEdge="bottom">
        <targets>
            <target android:targetId="@id/text"/>
        </targets>
    </slide>
</transitionSet>

答案 1 :(得分:-1)

要将动画应用于所需的视图,例如按钮,textview,imageview等,请检查此http://www.androidhive.info/2013/06/android-working-with-xml-animations/

为动画创建四个XML文件: /res/anim/anim_alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/linear_interpolator">
   <alpha
       android:fromAlpha="1.0"
       android:toAlpha="0.1"
       android:duration="500"
       android:repeatCount="1"
       android:repeatMode="reverse" />
</set>

/res/anim/anim_rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/linear_interpolator">
   <rotate
       android:fromDegrees="0"
       android:toDegrees="360"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="500"
       android:startOffset="0"
       android:repeatCount="1"
       android:repeatMode="reverse" />
</set>

/res/anim/anim_scale.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/linear_interpolator">
   <scale
       android:fromXScale="1.0"
       android:toXScale="3.0"
       android:fromYScale="1.0"
       android:toYScale="3.0"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="500"
       android:repeatCount="1"
       android:repeatMode="reverse" />
</set>

/res/anim/anim_translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/linear_interpolator">
   <translate
       android:fromXDelta="0"
       android:toXDelta="100%p"
       android:duration="500"
       android:repeatCount="1"
       android:repeatMode="reverse"/>
</set>

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/hello" />

   <Button
       android:id="@+id/translate"
       android:layout_width="200dp"
       android:layout_height="wrap_content"
       android:layout_margin="10dp"
       android:text="Translate" />
   <Button
       android:id="@+id/alpha"
       android:layout_width="200dp"
       android:layout_height="wrap_content"
       android:layout_margin="10dp"
       android:text="Alpha" />
   <Button
       android:id="@+id/scale"
       android:layout_width="200dp"
       android:layout_height="wrap_content"
       android:layout_margin="10dp"
       android:text="Scale" />
   <Button
       android:id="@+id/rotate"
       android:layout_width="200dp"
       android:layout_height="wrap_content"
       android:layout_margin="10dp"
       android:text="Rotate" />

</LinearLayout>

主要活动:

package com.exercise.AndroidAnimButtons;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class AndroidAnimButtonsActivity extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       final Animation animTranslate = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
       final Animation animAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
       final Animation animScale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
       final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);

       Button btnTranslate = (Button)findViewById(R.id.translate);
       Button btnAlpha = (Button)findViewById(R.id.alpha);
       Button btnScale = (Button)findViewById(R.id.scale);
       Button btnRotate = (Button)findViewById(R.id.rotate);

       btnTranslate.setOnClickListener(new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   arg0.startAnimation(animTranslate);
  }});

       btnAlpha.setOnClickListener(new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   arg0.startAnimation(animAlpha);
  }});

       btnScale.setOnClickListener(new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   arg0.startAnimation(animScale);
  }});

       btnRotate.setOnClickListener(new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   arg0.startAnimation(animRotate);
  }});
   }
}