android开门动画效果

来源:互联网 发布:php授权破解 编辑:程序博客网 时间:2024/04/19 19:08
  1. public class AntActivity extends Activity {  
  2.   
  3.     private Context context;  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         this.context = this;  
  9.         /***  
  10.          * 特别说明这两行代码,当加载一个布局文件的时候请使用以下方法导入。  
  11.          * 如导入main.xml这个布局文件的时候用以下方法。  
  12.          * 不要使用 以下代码  如  
  13.          *  LinearLayout ll = (LinearLayout) this.findViewById(R.layout.main);  
  14.          *  因为main这个布局不是main.xml 文件下的一个布局,所以如果使用上一行代码的话 会报空指针的  
  15.          *  简单说,如果导入的是一个布局文件 那么使用LayoutInflater  如果加载的是一个布局文件中的子布局,使用findviewbyid()  
  16.          */  
  17.          
  18.         LayoutInflater inflater = LayoutInflater.from(this);  
  19.         LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.main,null);  
  20.           
  21.         /*****************************************************************************************/  
  22.           
  23.           
  24.         Animation animation = new TranslateAnimation(0, -getWindowManager().getDefaultDisplay().getWidth(), 0, 0);//向左移动的动画效果  
  25.         animation.setDuration(5000);//设置动画过程时间  
  26.         animation.setStartOffset(1000);//设置动画延时时间//一秒后开  
  27.         Animation animation1 = new TranslateAnimation(0,  
  28.                 (float) (getWindowManager().getDefaultDisplay().getWidth()),0 ,0);//向右移动的动画效果  
  29.         animation1.setStartOffset(1000);  
  30.         animation1.setDuration(5000);  
  31.          
  32.           
  33.           
  34.         final ImageView tt = new ImageView(this);  
  35.         tt.setLayoutParams(new LayoutParams(getWindowManager().getDefaultDisplay().getWidth()/2,  
  36.                 getWindowManager().getDefaultDisplay().getHeight()));//设置改ImageView 的大小,宽为屏幕的一半,高为屏幕的高度  
  37.         //这样做的好处可以对不同屏幕分辨率进行适配  
  38.           
  39.         tt.setBackgroundResource(R.drawable.tt_1);//设置背景  
  40.           
  41.         final ImageView tt1 = new ImageView(this);  
  42.         tt1.setLayoutParams(new LayoutParams(getWindowManager().getDefaultDisplay().getWidth()/2,  
  43.                 getWindowManager().getDefaultDisplay().getHeight()));  
  44.         tt1.setBackgroundResource(R.drawable.tt_2);  
  45.          
  46.         tt1.setAnimation(animation1);//设置动画效果  
  47.         tt.setAnimation(animation);//设置动画效果  
  48.           
  49.           
  50.         animation.setAnimationListener(new Animation.AnimationListener() {//设置动画监听  
  51.             @Override  
  52.             public void onAnimationStart(Animation animation) {//动画状态开始  
  53.                 // TODO Auto-generated method stub  
  54.                 Toast.makeText(context, "I'm begin", 0).show();//日志提示开始了  
  55.                 //如果想开们效果中出现锯齿效果,在动画开始时,在本位置,将ImageView 的背景置换一下就可以了  
  56.             }  
  57.               
  58.             @Override  
  59.             public void onAnimationRepeat(Animation animation) {  
  60.                 // TODO Auto-generated method stub  
  61.                   
  62.             }  
  63.               
  64.             @Override  
  65.             public void onAnimationEnd(Animation animation) {//动画状态结束  
  66.                 // TODO Auto-generated method stub  
  67.                 tt.setVisibility(View.GONE);//设置改控件隐藏,当然你也可以在结束这个位置放置其他处理。  
  68.             }  
  69.         });  
  70.           
  71.         /**  
  72.          * 与上一个监听一样  
  73.          */  
  74.         animation1.setAnimationListener(new Animation.AnimationListener() {  
  75.               
  76.             @Override  
  77.             public void onAnimationStart(Animation animation) {  
  78.                   
  79.             }  
  80.             @Override  
  81.             public void onAnimationRepeat(Animation animation) {  
  82.                   
  83.             }  
  84.             @Override  
  85.             public void onAnimationEnd(Animation animation) {  
  86.                 tt1.setVisibility(View.GONE);  
  87.             }  
  88.         });  
  89.         ll.addView(tt);  
  90.         ll.addView(tt1);  
  91.           
  92.         this.setContentView(ll);//设置显示的布局内容  
  93.           
  94.     }  
  95. }  
布局文件
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:text="@string/hello"  
  11.         android:visibility="gone" />  
  12.   
  13. </LinearLayout> 


原创粉丝点击