Android在设置时添加简单的动画(view.Gone) [英] Android adding simple animations while setvisibility(view.Gone)

查看:175
本文介绍了Android在设置时添加简单的动画(view.Gone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个简单的布局。我已经完成了没有动画的设计,但现在我想在textview点击事件时添加动画,我不知道如何使用它。
我的xml设计看起来好不好?
如有任何建议,我们将不胜感激。



我的XML

 <?xml version =1.0encoding =UTF-8?> 
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =fill_parent
android:layout_height =fill_parent
android:longClickable =false
android:orientation =vertical
android:weightSum =16>

< LinearLayout
android:layout_width =fill_parent
android:layout_height =0dp
android:orientation =vertical
android: background =#00DDA0
android:layout_weight =3>
< / LinearLayout>
< TextView
android:id =@ + id / Information1
android:layout_width =match_parent
android:layout_height =1dp
android: text =Child Information
android:background =#0390BE
android:layout_weight =0.75
android:textColor =#FFFFFF
android:layout_gravity =中心| fill_horizo​​ntal/>

< LinearLayout
android:id =@ + id / layout1
android:layout_width =fill_parent
android:layout_height =0dp
android:layout_weight =8.5
android:background =#BBBBBB
android:orientation =vertical>

< TextView
android:id =@ + id / textView1
android:layout_width =match_parent
android:layout_height =match_parent
android:text =TextView/>
< / LinearLayout>

< TextView
android:id =@ + id / Information2
android:layout_width =match_parent
android:layout_height =0dp
android:text =父信息
android:background =#0390BE
android:layout_weight =0.75
android:textColor =#FFFFFF
android :layout_gravity = 中心| fill_horizo​​ntal/>
< LinearLayout
android:id =@ + id / layout2
android:layout_width =fill_parent
android:layout_height =0dp
android: orientation =vertical
android:background =#BBBBBB
android:layout_weight =8.5>
< TextView
android:id =@ + id / textView2
android:layout_width =match_parent
android:layout_height =match_parent
android: text =TextView/>
< / LinearLayout>
< TextView
android:id =@ + id / Information3
android:layout_width =match_parent
android:layout_height =0dp
android: text =兄弟姐妹
android:background =#0390BE
android:layout_weight =0.75
android:textColor =#FFFFFF
android:layout_gravity =center | fill_horizo​​ntal/>
< LinearLayout
android:id =@ + id / layout3
android:layout_width =fill_parent
android:layout_height =0dp
android: orientation =vertical
android:background =#BBBBBB
android:layout_weight =8.5>
< TextView
android:id =@ + id / textView3
android:layout_width =match_parent
android:layout_height =match_parent
android: text =TextView/>
< / LinearLayout>
< TextView
android:id =@ + id / Information4
android:layout_width =match_parent
android:layout_height =0dp
android: text =教师信息
android:background =#0390BE
android:layout_weight =0.75
android:textColor =#FFFFFF
android:layout_gravity =中心| fill_horizo​​ntal/>
< LinearLayout
android:id =@ + id / layout4
android:layout_width =fill_parent
android:layout_height =0dp
android: orientation =vertical
android:background =#BBBBBB
android:layout_weight =8.5>
< TextView
android:id =@ + id / textView4
android:layout_width =match_parent
android:layout_height =match_parent
android: text =TextView/>
< / LinearLayout>
< TextView
android:id =@ + id / Information5
android:layout_width =match_parent
android:layout_height =0dp
android: text =等级信息
android:background =#0390BE
android:layout_weight =0.75
android:textColor =#FFFFFF
android:layout_gravity =中心| fill_horizo​​ntal/>
< LinearLayout
android:id =@ + id / layout5
android:layout_width =fill_parent
android:layout_height =0dp
android: orientation =vertical
android:background =#BBBBBB
android:layout_weight =8.5>
< TextView
android:id =@ + id / textView5
android:layout_width =match_parent
android:layout_height =match_parent
android: text =TextView/>
< / LinearLayout>
< TextView
android:id =@ + id / Information6
android:layout_width =match_parent
android:layout_height =0dp
android: text =健康信息
android:background =#0390BE
android:layout_weight =0.75
android:textColor =#FFFFFF
android:layout_gravity =中心| fill_horizo​​ntal/>
< LinearLayout
android:id =@ + id / layout6
android:layout_width =fill_parent
android:layout_height =0dp
android: orientation =vertical
android:background =#BBBBBB
android:layout_weight =8.5>
< TextView
android:id =@ + id / textView5
android:layout_width =match_parent
android:layout_height =match_parent
android: text =TextView
android:layout_weight =8.5/>
< / LinearLayout>

< / LinearLayout>

我的java

 公共类Certify_Info扩展活动{

私有静态TextView tv2,tv3,tv5,tv6,tv4,tv1;
private static LinearLayout l1,l2,l3,l4,l5,l6;
@Override
protected void onCreate(Bundle savedInstanceState){
// TODO自动生成的方法stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_certify__info);

tv1 =(TextView)findViewById(R.id.Information1);
tv2 =(TextView)findViewById(R.id.Information2);
tv3 =(TextView)findViewById(R.id.Information3);
tv4 =(TextView)findViewById(R.id.Information4);
tv5 =(TextView)findViewById(R.id.Information5);
tv6 =(TextView)findViewById(R.id.Information6);

l1 =(LinearLayout)findViewById(R.id.layout1);
l2 =(LinearLayout)findViewById(R.id.layout2);
l3 =(LinearLayout)findViewById(R.id.layout3);
l4 =(LinearLayout)findViewById(R.id.layout4);
l5 =(LinearLayout)findViewById(R.id.layout5);
l6 =(LinearLayout)findViewById(R.id.layout6);

l2.setVisibility(View.GONE);
l3.setVisibility(View.GONE);
l4.setVisibility(View.GONE);
l5.setVisibility(View.GONE);
l6.setVisibility(View.GONE);

tv1.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v){
// TODO自动生成的方法stub
l2.setVisibility(View.GONE);
l3.setVisibility(View.GONE);
l4.setVisibility(View.GONE);
l5.setVisibility(View。 GONE);
l6.setVisibility(View.GONE);
l1.setVisibility(View.VISIBLE);
}
});
tv2.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v){
// TODO自动生成的方法stub
l1.setVisibility(View.GONE);
l3.setVisibility(View.GONE);
l4.setVisibility(View.GONE);
l5.setVisibility(View.GONE);
l6.setVisibility(View.GONE);
l2.setVisibility(View.VISIBLE);
}
});
tv3.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v){
// TODO自动生成方法stub
l1.setVisibility(View.GONE);
l2.setVisibility(View.GONE);
l4.setVisibility(View.GONE);
l5.setVisibility(View.GONE);
l6.setVisibility(View.GONE);
l3.setVisibility(View.VISIBLE);

}
});
tv4.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v){
// TODO自动生成的方法stub
l1.setVisibility(View.GONE);
l2.setVisibility(View.GONE);
l3.setVisibility(View.GONE);
l4.setVisibility(View.GONE);
l5.setVisibility(View.GONE);
l6.setVisibility(View.GONE);
l4.setVisibility(View.VISIBLE);
}
});
tv5.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v){
// TODO自动生成的方法stub
l1.setVisibility(View.GONE);
l2.setVisibility(View.GONE);
l3.setVisibility(View.GONE);
l4.setVisibility(View.GONE);
l6.setVisibility(View.GONE);
l5.setVisibility(View.VISIBLE);
}
});
tv6.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v){
// TODO自动生成的方法stub
l1.setVisibility(View.GONE);
l2.setVisibility(View.GONE);
l3.setVisibility(View.GONE);
l4.setVisibility(View.GONE);
l5.setVisibility(View.GONE);
l6.setVisibility(View.VISIBLE);
}
});

}
}


解决方案

你可以做两件事来添加动画,首先你可以让android动画布局改变你。这样每次你在布局中改变一些东西,如改变视图可见性或视图位置时,android会自动创建淡入淡出/过渡动画。使用该套装

  android:animateLayoutChanges =true




您的第二个选项是手动添加动画。为此,我建议您使用Android 3.0(Honeycomb)中引入的新动画API。我可举几个例子:



这淡出查看

  view.animate()。alpha(0.0f); 

这会将其淡入:

  view.animate()α(1.0F)。 

这会将 View 向下移动高度:

  view.animate()。translationY(view.getHeight()); 

这会将查看返回到其起始位置在它移动到其他地方之后:

  view.animate()。translationY(0); 

您还可以使用 setDuration()来设置动画的持续时间。例如,这会在2秒的时间内淡出查看

  view.animate()α(0.0F).setDuration(2000); 

您可以根据需要组合任意数量的动画,例如这会淡出查看并在0.3秒内同时向下移动:

  view .animate()
.translationY(view.getHeight())
.alpha(0.0f)
.setDuration(300);

您还可以为动画分配监听器并对各种事件做出反应。就像动画开始时,结束或重复等一样。通过使用抽象类 AnimatorListenerAdapter ,您不必实现 AnimatorListener <的所有回调/ code>马上但只有你需要的那些。这使代码更具可读性。例如,以下代码淡出视图在0.3秒(300毫秒)的时间内将其向下移动高度,当动画完成时,其可见性设置为 View.GONE

  view.animate()
。 translationY(view.getHeight())
.alpha(0.0f)
.setDuration(300)
.setListener(new AnimatorListenerAdapter(){
@Override
public void onAnimationEnd(Animator animation){
super.onAnimationEnd(animation);
view.setVisibility(View.GONE);
}
});


I have designed a simple layout.I have finished the design without animation, but now I want to add animations when textview click event and I don't know how to use it. Did my xml design looks good or not? Any suggestions would be appreciated.

My 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:longClickable="false"
    android:orientation="vertical"
    android:weightSum="16" >

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:background="#00DDA0"
    android:layout_weight="3" >
</LinearLayout>
 <TextView
        android:id="@+id/Information1"
        android:layout_width="match_parent"
        android:layout_height="1dp" 
        android:text="Child Information" 
        android:background="#0390BE"
        android:layout_weight="0.75"
        android:textColor="#FFFFFF"
        android:layout_gravity="center|fill_horizontal"/>

 <LinearLayout
     android:id="@+id/layout1"
     android:layout_width="fill_parent"
     android:layout_height="0dp"
     android:layout_weight="8.5"
     android:background="#BBBBBB"
     android:orientation="vertical" >

     <TextView
         android:id="@+id/textView1"
         android:layout_width="match_parent"
         android:layout_height="match_parent"        
         android:text="TextView" />
 </LinearLayout>

  <TextView
        android:id="@+id/Information2"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:text="Parent Information" 
        android:background="#0390BE"
        android:layout_weight="0.75"
        android:textColor="#FFFFFF"
        android:layout_gravity="center|fill_horizontal"/>
  <LinearLayout 
          android:id="@+id/layout2"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:background="#BBBBBB"
    android:layout_weight="8.5" >
     <TextView
         android:id="@+id/textView2"
         android:layout_width="match_parent"
         android:layout_height="match_parent"        
         android:text="TextView" />
      </LinearLayout>
   <TextView
        android:id="@+id/Information3"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:text="Siblings" 
        android:background="#0390BE"
        android:layout_weight="0.75"
        android:textColor="#FFFFFF"
        android:layout_gravity="center|fill_horizontal"/>
   <LinearLayout 
          android:id="@+id/layout3"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:background="#BBBBBB"
    android:layout_weight="8.5" >
     <TextView
         android:id="@+id/textView3"
         android:layout_width="match_parent"
         android:layout_height="match_parent"        
         android:text="TextView" />
      </LinearLayout>
    <TextView
        android:id="@+id/Information4"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:text="Teacher Information" 
        android:background="#0390BE"
        android:layout_weight="0.75"
        android:textColor="#FFFFFF"
        android:layout_gravity="center|fill_horizontal"/>
    <LinearLayout 
          android:id="@+id/layout4"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:background="#BBBBBB"
    android:layout_weight="8.5" >
     <TextView
         android:id="@+id/textView4"
         android:layout_width="match_parent"
         android:layout_height="match_parent"        
         android:text="TextView" />
      </LinearLayout>
     <TextView
        android:id="@+id/Information5"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:text="Grade Information" 
        android:background="#0390BE"
        android:layout_weight="0.75"
        android:textColor="#FFFFFF"
        android:layout_gravity="center|fill_horizontal"/>
     <LinearLayout 
          android:id="@+id/layout5"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:background="#BBBBBB"
    android:layout_weight="8.5" >
     <TextView
         android:id="@+id/textView5"
         android:layout_width="match_parent"
         android:layout_height="match_parent"        
         android:text="TextView" />
      </LinearLayout>
      <TextView
        android:id="@+id/Information6"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:text="Health Information" 
        android:background="#0390BE"
        android:layout_weight="0.75"
        android:textColor="#FFFFFF"
        android:layout_gravity="center|fill_horizontal"/>
      <LinearLayout 
          android:id="@+id/layout6"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:background="#BBBBBB"
    android:layout_weight="8.5" >
    <TextView
         android:id="@+id/textView5"
         android:layout_width="match_parent"
         android:layout_height="match_parent"        
         android:text="TextView" 
         android:layout_weight="8.5" />
      </LinearLayout>

</LinearLayout>

My java

public class Certify_Info extends Activity {

    private static TextView tv2,tv3,tv5,tv6,tv4,tv1;
    private static LinearLayout l1,l2,l3,l4,l5,l6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_certify__info);

        tv1=(TextView) findViewById(R.id.Information1);
        tv2=(TextView) findViewById(R.id.Information2);
        tv3=(TextView) findViewById(R.id.Information3);
        tv4=(TextView) findViewById(R.id.Information4);
        tv5=(TextView) findViewById(R.id.Information5);
        tv6=(TextView) findViewById(R.id.Information6); 

        l1=(LinearLayout) findViewById(R.id.layout1);
        l2=(LinearLayout) findViewById(R.id.layout2);
        l3=(LinearLayout) findViewById(R.id.layout3);
        l4=(LinearLayout) findViewById(R.id.layout4);
        l5=(LinearLayout) findViewById(R.id.layout5);
        l6=(LinearLayout) findViewById(R.id.layout6); 

        l2.setVisibility(View.GONE);
        l3.setVisibility(View.GONE); 
        l4.setVisibility(View.GONE); 
        l5.setVisibility(View.GONE);
        l6.setVisibility(View.GONE);

        tv1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                l2.setVisibility(View.GONE);
                l3.setVisibility(View.GONE); 
                l4.setVisibility(View.GONE); 
                l5.setVisibility(View.GONE);
                l6.setVisibility(View.GONE);
                l1.setVisibility(View.VISIBLE);
            }
        });
        tv2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                l1.setVisibility(View.GONE);
                l3.setVisibility(View.GONE); 
                l4.setVisibility(View.GONE); 
                l5.setVisibility(View.GONE);
                l6.setVisibility(View.GONE);
                l2.setVisibility(View.VISIBLE);
            }
        });
        tv3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                l1.setVisibility(View.GONE);
                l2.setVisibility(View.GONE);
                l4.setVisibility(View.GONE); 
                l5.setVisibility(View.GONE);
                l6.setVisibility(View.GONE);
                l3.setVisibility(View.VISIBLE);

            }
        });
        tv4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                l1.setVisibility(View.GONE);
                l2.setVisibility(View.GONE);
                l3.setVisibility(View.GONE); 
                l4.setVisibility(View.GONE); 
                l5.setVisibility(View.GONE);
                l6.setVisibility(View.GONE);
                l4.setVisibility(View.VISIBLE); 
            }
        });
        tv5.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                l1.setVisibility(View.GONE);
                l2.setVisibility(View.GONE);
                l3.setVisibility(View.GONE); 
                l4.setVisibility(View.GONE); 
                l6.setVisibility(View.GONE);
                l5.setVisibility(View.VISIBLE); 
            }
        });
        tv6.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                l1.setVisibility(View.GONE);
                l2.setVisibility(View.GONE);
                l3.setVisibility(View.GONE); 
                l4.setVisibility(View.GONE); 
                l5.setVisibility(View.GONE);
                l6.setVisibility(View.VISIBLE);
            }
        });

    }
}

解决方案

You can do two things to add animations, first you can let android animate layout changes for you. That way every time you change something in the layout like changing view visibility or view positions android will automatically create fade/transition animations. To use that set

android:animateLayoutChanges="true"

on the root node in your layout.

Your second option would be to manually add animations. For this I suggest you use the new animation API introduced in Android 3.0 (Honeycomb). I can give you a few examples:

This fades out a View:

view.animate().alpha(0.0f);

This fades it back in:

view.animate().alpha(1.0f);

This moves a View down by its height:

view.animate().translationY(view.getHeight());

This returns the View to its starting position after it has been moved somewhere else:

view.animate().translationY(0);

You can also use setDuration() to set the duration of the animation. For example this fades out a View over a period of 2 seconds:

view.animate().alpha(0.0f).setDuration(2000);

And you can combine as many animations as you like, for example this fades out a View and moves it down at the same time over a period of 0.3 seconds:

view.animate()
        .translationY(view.getHeight())
        .alpha(0.0f)
        .setDuration(300);

And you can also assign a listener to the animation and react to all kinds of events. Like when the animation starts, when it ends or repeats etc. By using the abstract class AnimatorListenerAdapter you don't have to implement all callbacks of AnimatorListener at once but only those you need. This makes the code more readable. For example the following code fades out a View moves it down by its height over a period of 0.3 seconds (300 milliseconds) and when the animation is done its visibility is set to View.GONE.

view.animate()
        .translationY(view.getHeight())
        .alpha(0.0f)
        .setDuration(300)
        .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                view.setVisibility(View.GONE);
            }
        });

这篇关于Android在设置时添加简单的动画(view.Gone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆