最近的任务中要求点击按钮弹出一个对话框,在对话框中播放动画,点击对话框中的取消按钮,对话框就消失。具体代码如下:
1.工程目录文件:
2.在主布局里就放了2个按钮,主布局比较简单,就不详细说了,dialog的布局用自定义的,dialog布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialogbackground"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_gravity="center"
android:background="@anim/blast"/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#009ACD"
android:text="取消" />
</LinearLayout>
线性布局中我们设置了背景为dialogbackground,它是dialog样式的文件,放在新建的drawable文件里,主要设置dialog的形状和颜色,具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:topLeftRadius="20px"
android:topRightRadius="5px"
android:bottomLeftRadius="20px"
android:bottomRightRadius="5px"/>
<stroke android:width="3px" android:color="#00FFFF"/>
<gradient android:startColor="#000000"
android:centerColor="#000000"
android:endColor="#000000"/>
</shape>
3.在res下面新建一个anim文件,在这里面写上动画:
然后将我们每一帧的图片放到drawable-mdpi文件中:
4.将dialo封装,具体代码如下:
package com.example.leaningclass;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
public class PariseDialog extends AlertDialog implements android.view.View.OnClickListener{
Button negative;
ImageView image;
public PariseDialog(Context context) {
super(context);
}
protected void onCreate(android.os.Bundle savedInstanceState) {
// View view=in.inflate(R.layout.parisedialog, null);
setContentView(R.layout.parisedialog);
image=(ImageView) findViewById(R.id.imageview);
negative=(Button) findViewById(R.id.bt1);
// setView(view);
setCanceledOnTouchOutside(false);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
final AnimationDrawable anim=(AnimationDrawable)image.getBackground();
anim.start();//启动动画播放
negative.setOnClickListener(this);
};
@Override
public void onClick(View v) {
dismiss();
}
@Override
public void show() {
// TODO Auto-generated method stub
super.show();
}
}
5.在主Activity中显示我们的dialog,看看我们的MainActivity的代码:
package com.example.leaningclass;
import android.R.anim;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Switch;
public class MainActivity extends Activity implements OnClickListener {
private Button pariseButton ,robButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pariseButton=(Button)findViewById(R.id.btpraise);
robButton=(Button)findViewById(R.id.btrob);
pariseButton.setOnClickListener(this);
robButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button bt = (Button) v;
switch(bt.getId()){
case R.id.btpraise:
PariseDialog pariseDialog = new PariseDialog(MainActivity.this);
pariseDialog.show();
break;
case R.id.btrob:
robDialog robDialog = new robDialog(MainActivity.this);
robDialog.show();
}
}
}