现在的位置: 首页 > 综合 > 正文

Android ProgressBar 自定义样式(五),仿真QQ pad版加载(位置不居中)

2013年01月07日 ⁄ 综合 ⁄ 共 1624字 ⁄ 字号 评论关闭

废话不多说,先上图,有图有真相:

xml布局文件如下:

<com.freesonfish.progress_bar.MyProgressBar
            android:id="@+id/my_progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:gravity="center" />

因为自定义view,所以给定一个高度:100

其中MyProgressBar定义为:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

public class MyProgressBar extends View {

	private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	private float mRotate;
	private Matrix mMatrix = new Matrix();
	private Shader mShader;
	private float x = 50.0f, y = 50.0f;

	public MyProgressBar(Context context, AttributeSet attr) {
		super(context, attr);
		initPaint();
	}

	public MyProgressBar(Context context) {
		super(context);
		initPaint();
	}

	private void initPaint() {
		mShader = new SweepGradient(x, y, new int[] { 0x66378300, 0xFF378300 }, new float[] { 0.7f, 0.7f });
		mPaint.setShader(mShader);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(10);
		mPaint.setAntiAlias(true);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Paint paint = mPaint;
		mMatrix.setRotate(mRotate, x, y);
		mShader.setLocalMatrix(mMatrix);
		mRotate += 5;
		if (mRotate >= 360) {
			mRotate = 0;
		}
		invalidate();
		canvas.drawCircle(x, y, 40, paint);
		BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(R.drawable.ic_login_qq);
		Bitmap bitmap = drawable.getBitmap();
		canvas.drawBitmap(drawable.getBitmap(), (100 - bitmap.getWidth()) / 2.0f, (100 - bitmap.getHeight()) / 2.0f, paint);

	}
}

其中一些原理和控制转速等等,自己起摸索吧,哈哈。

这样做有一个缺点就是bitmap一直都是画,幸亏图片足够小,要不要就爆了。下一篇预告:居中的情况

抱歉!评论已关闭.