Android 自定义LoadingDialog
看惯了系统自带的Dialog的同学们可以试着尝试自己做一个LoadingDialog。
效果图:
代码如下:
CustomerLoading.javapackage com.eric; import android.app.Activity; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.TextView; public class CustomerLoading extends Activity { private LinearLayout mLoad; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewHandler.creteProgressDialog(this, "loading"); } }
这个类应该是这个知识点的重点,重画View图像,达到转动效果.
LoadingView.java: package com.eric; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.os.Handler; import android.util.AttributeSet; import android.view.View; public class LoadingView extends View { private Matrix mFgMatrix; private Bitmap mFgBitmap; public LoadingView(Context context) { super(context); } public LoadingView(Context context, AttributeSet attrs) { super(context, attrs); } public void setDrawableResId(int iconResId) { mFgMatrix = new Matrix(); mFgBitmap = BitmapFactory.decodeResource(getResources(), iconResId); myHandler.sendEmptyMessage(0); onMeasure(mFgBitmap.getWidth(), mFgBitmap.getHeight()); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(mFgBitmap.getWidth(), mFgBitmap.getHeight()); package com.eric; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.os.Handler; import android.util.AttributeSet; import android.view.View; public class LoadingView extends View { private Matrix mFgMatrix; private Bitmap mFgBitmap; public LoadingView(Context context) { super(context); } public LoadingView(Context context, AttributeSet attrs) { super(context, attrs); } public void setDrawableResId(int iconResId) { mFgMatrix = new Matrix(); mFgBitmap = BitmapFactory.decodeResource(getResources(), iconResId); myHandler.sendEmptyMessage(0); onMeasure(mFgBitmap.getWidth(), mFgBitmap.getHeight()); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(mFgBitmap.getWidth(), mFgBitmap.getHeight()); } @Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(mFgBitmap, mFgMatrix, null); } private Handler myHandler = new Handler() { public void handleMessage(android.os.Message msg) { mFgMatrix.postRotate(-10f, mFgBitmap.getWidth() / 2f, mFgBitmap .getHeight() / 2f); invalidate();//更新视图 myHandler.sendEmptyMessageDelayed(msg.what, 20); }; }; } } @Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(mFgBitmap, mFgMatrix, null); } private Handler myHandler = new Handler() { public void handleMessage(android.os.Message msg) { mFgMatrix.postRotate(-10f, mFgBitmap.getWidth() / 2f, mFgBitmap .getHeight() / 2f); invalidate();//更新视图 myHandler.sendEmptyMessageDelayed(msg.what, 20); }; }; }
这个类主要是实现一个ProgressDialog效果的进度条框.
ViewHandler.java: package com.eric; import android.app.ProgressDialog; import android.content.Context; import android.widget.LinearLayout; import android.widget.TextView; public class ViewHandler { public static ProgressDialog creteProgressDialog(Context context, String text) { final ProgressDialog dlg = new ProgressDialog(context); dlg.show(); dlg.setContentView(R.layout.loading); LinearLayout root = (LinearLayout) dlg .findViewById(R.id.progressDialog); root.setGravity(android.view.Gravity.CENTER); LoadingView mLoadView = new LoadingView(context); mLoadView.setDrawableResId(R.drawable.icon10); root.addView(mLoadView); TextView alert = new TextView(context); alert.setText(text); root.addView(alert); return dlg; } }