您的位置:首页 > 移动开发 > Android开发

android开门动画效果

2012-11-19 10:49 281 查看
public class AntActivity extends Activity {

private Context context;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

this.context = this;

/***

* 特别说明这两行代码,当加载一个布局文件的时候请使用以下方法导入。

* 如导入main.xml这个布局文件的时候用以下方法。

* 不要使用 以下代码 如

* LinearLayout ll = (LinearLayout) this.findViewById(R.layout.main);

* 因为main这个布局不是main.xml 文件下的一个布局,所以如果使用上一行代码的话 会报空指针的

* 简单说,如果导入的是一个布局文件 那么使用LayoutInflater 如果加载的是一个布局文件中的子布局,使用findviewbyid()

*/

LayoutInflater inflater = LayoutInflater.from(this);

LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.main,null);

/*****************************************************************************************/

Animation animation = new TranslateAnimation(0, -getWindowManager().getDefaultDisplay().getWidth(), 0, 0);//向左移动的动画效果

animation.setDuration(5000);//设置动画过程时间

animation.setStartOffset(1000);//设置动画延时时间//一秒后开

Animation animation1 = new TranslateAnimation(0,

(float) (getWindowManager().getDefaultDisplay().getWidth()),0 ,0);//向右移动的动画效果

animation1.setStartOffset(1000);

animation1.setDuration(5000);

final ImageView tt = new ImageView(this);

tt.setLayoutParams(new LayoutParams(getWindowManager().getDefaultDisplay().getWidth()/2,

getWindowManager().getDefaultDisplay().getHeight()));//设置改ImageView 的大小,宽为屏幕的一半,高为屏幕的高度

//这样做的好处可以对不同屏幕分辨率进行适配

tt.setBackgroundResource(R.drawable.tt_1);//设置背景

final ImageView tt1 = new ImageView(this);

tt1.setLayoutParams(new LayoutParams(getWindowManager().getDefaultDisplay().getWidth()/2,

getWindowManager().getDefaultDisplay().getHeight()));

tt1.setBackgroundResource(R.drawable.tt_2);

tt1.setAnimation(animation1);//设置动画效果

tt.setAnimation(animation);//设置动画效果

animation.setAnimationListener(new Animation.AnimationListener() {//设置动画监听

@Override

public void onAnimationStart(Animation animation) {//动画状态开始

// TODO Auto-generated method stub

Toast.makeText(context, "I'm begin", 0).show();//日志提示开始了

//如果想开们效果中出现锯齿效果,在动画开始时,在本位置,将ImageView 的背景置换一下就可以了

}

@Override

public void onAnimationRepeat(Animation animation) {

// TODO Auto-generated method stub

}

@Override

public void onAnimationEnd(Animation animation) {//动画状态结束

// TODO Auto-generated method stub

tt.setVisibility(View.GONE);//设置改控件隐藏,当然你也可以在结束这个位置放置其他处理。

}

});

/**

* 与上一个监听一样

*/

animation1.setAnimationListener(new Animation.AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {

}

@Override

public void onAnimationRepeat(Animation animation) {

}

@Override

public void onAnimationEnd(Animation animation) {

tt1.setVisibility(View.GONE);

}

});

ll.addView(tt);

ll.addView(tt1);

this.setContentView(ll);//设置显示的布局内容

}

}

布局文件

<?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="horizontal" >

<TextView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="@string/hello"

android:visibility="gone" />

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: