android 同一個介面同時實現兩種動畫相對運動
阿新 • • 發佈:2019-02-05
在一個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>