在一个activity中,顶部和底部的两个控件以动画的形式相对出现,类似上下交错的效果。首先自定义两个布局,继承LinearLayout;其次在Activity需要加载布局文件中引用自定义布局,并且在该布局添加其他需要使用的组件,比如编辑框,文本框等。在自定义的布局中定义一个方法,从上往下移动和从下网上移动。使用TranslateAnimation类,实现组件移动。这样我们定义的布局就具有了动画的特征,那么在Activity加载Activiy对应的XML文件时,首先要实例化自定义的布局文件,在呈现的时候,动画效果也就随之出现了。
请看代码;
三个java文件,一个是自定义顶部布局的java文件,一个是自定义底部布局的java文件,他们都有一个动画的方法;另一个是Activity。
顶部自定义布局:
package com.king.helloword;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TopActivty extends LinearLayout{
private Context context;
public TopActivty(Context context) {
super(context);
topAnimation();
// TODO Auto-generated constructor stub
}
public TopActivty(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
//addViewIn();
setOrientation(LinearLayout.VERTICAL);
topAnimation();
}
private void topAnimation() {
TranslateAnimation ta = new TranslateAnimation(0, 0, -200, 0);
ta.setDuration(3000);
this.setAnimation(ta);
}
}
底部自定义布局:
package com.king.helloword;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
public class BottomActivity extends LinearLayout{
private Context context;
public BottomActivity(Context context) {
super(context);
this.context=context;
bottomAnimation();
// TODO Auto-generated constructor stub
}
public BottomActivity(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
setOrientation(LinearLayout.VERTICAL);
bottomAnimation();
// TODO Auto-generated constructor stub
}
private void bottomAnimation() {
Display d = ((Activity)context).getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
int height = d.getHeight();
TranslateAnimation ta = new TranslateAnimation(0, 0, 350, 0);
ta.setDuration(3000);
this.setAnimation(ta);
}
}
activity:
package com.king.helloword;
import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.Window;
public class DefinedActivtiyAnimation extends Activity{
private int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.defined_animation);
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.king.helloword.TopActivty
android:id="@+id/top_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:background="#4ff" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="100dp"
android:layout_marginTop="26dp"
android:text="TextView" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="74dp"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
</com.king.helloword.TopActivty>
<com.king.helloword.BottomActivity
android:id="@+id/bottom_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:background="#4ff" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/top_view"
android:layout_marginLeft="61dp"
android:layout_marginTop="62dp"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="36dp"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:text="Button" />
</com.king.helloword.BottomActivity>
</RelativeLayout>