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