自定义Android LoadingDialog
fmms
13年前
看惯了系统自带的Dialog的同学们可以试着尝试自己做一个LoadingDialog。 <br /> 效果图: <br /> <img style="width:676px;height:476px;" alt="自定义Android LoadingDialog" src="https://simg.open-open.com/show/896127da20298aab84e0495a6f14cfa1.png" /> <br /> <br /> 代码如下: <br /> <span style="line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;"><strong>CustomerLoading.java<pre class="brush:java; toolbar: true; auto-links: false;">package 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"); } }</pre></strong></span> <span style="line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;">这个类应该是这个知识点的重点,重画View图像,达到转动效果.<br /> <strong>LoadingView.java: <pre class="brush:java; toolbar: true; auto-links: false;">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); }; }; }</pre></strong></span> <span style="line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;">这个类主要是实现一个ProgressDialog效果的进度条框. <br /> <strong>ViewHandler.java: <pre class="brush:java; toolbar: true; auto-links: false;">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; } }</pre></strong></span>