简单的“加载中”动画效果

来源:互联网 发布:好的兼职软件 编辑:程序博客网 时间:2024/05/17 23:00

在日常开发过程中,处理一些延迟操作的时候,经常需要显示一个“加载中”对话框来来表示程序正在请求或正在处理。那么如何写一个最简单的动画效果呢?

这里我写了个demo:

MainActivity:

public class MainActivity extends Activity {    //ProgressDialog用于显示加载对话框用    private ProgressDialog progressDialog = null;    //显示结果用    private EditText editText1;    //消息处理    private Handler handler=new Handler(){        public void handleMessage(android.os.Message msg) {            switch (msg.what) {            case 1:                //显示加载后的内容                editText1.setText("成功了");                //加载对话框关闭                progressDialog.dismiss();                break;             default:                break;            }        };    };         @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                 editText1=(EditText) findViewById(R.id.editText1);             }         public void click(View v){        //当按钮点击的时候,此加载中的对话框显示        progressDialog= ProgressDialog.show(MainActivity.this, "请稍等...", "获取数据中...", true);        //开始联网        getNet();    }     private void getNet() {        new Thread(){            @Override            public void run() {                super.run();                SystemClock.sleep(2000);                //发消息                handler.sendEmptyMessage(1);            }        }.start();    }}


布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >     <Button        android:id="@+id/button1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_marginTop="15dp"        android:text="Button"        android:onClick="click" />     <EditText        android:id="@+id/editText1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/button1"        android:ems="10" >    </EditText> </RelativeLayout>


运行效果:

wKioL1ZvesuT1nJFAABwt-DE4Wk368.jpg

本文出自 “移动平台开发” 博客,请务必保留此出处http://liuxudong1001.blog.51cto.com/10877072/1723105