Android TypedArray使用方法

尊重原创,转载请标明出处    http://blog.csdn.net/abcdef314159

上一篇简单分析了一下TypedArray的源码,这一篇主要讲述一下怎么使用,把一些代码先贴出来一下,不知道怎么制作gif格式,下面这个是录屏之后转化的,不是很清晰,实现的页面如下,

Android TypedArray使用方法_第1张图片

这里只定义了两个属性,其他的都差不多




    
        
        
    

package com.sdw.typedarraydemo;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

public class MyProgressBar extends View {
	private String showProgress = "";
	private Paint mBitmapPaint;
	private TextPaint mTextPaint;
	private Bitmap line_progress_bar;
	private Bitmap triangle_progressbar;
	private Rect mRectLine;
	private Rect triangleRectLine;
	private Rect textBounds;
	private int textMagingBottom;
	private int textSize;
	private int textColor;

	public MyProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context, attrs);
	}

	public MyProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context, attrs);
	}

	private void initView(Context context, AttributeSet attrs) {
		if (isInEditMode())
			return;
		TypedArray typedArray = context.obtainStyledAttributes(attrs,
				R.styleable.MyProgressBar);

		textSize = typedArray
				.getInteger(R.styleable.MyProgressBar_textSize, 24);
		textColor = typedArray.getColor(R.styleable.MyProgressBar_textColor,
				Color.WHITE);
		typedArray.recycle();

		textMagingBottom = 6;
		mBitmapPaint = new Paint();
		mBitmapPaint.setColor(Color.WHITE);
		mBitmapPaint.setAntiAlias(true);

		mTextPaint = new TextPaint();
		mTextPaint.setColor(textColor);
		mTextPaint.setAntiAlias(true);
		mTextPaint.setTextSize(textSize);
		line_progress_bar = BitmapFactory.decodeResource(getResources(),
				R.drawable.img_line_progress_bar);
		triangle_progressbar = BitmapFactory.decodeResource(getResources(),
				R.drawable.img_progress_bar);

		textBounds = new Rect();
		triangleRectLine = new Rect();
		mRectLine = new Rect();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawBitmap(line_progress_bar, null, mRectLine, mBitmapPaint);
		canvas.drawBitmap(triangle_progressbar, null, triangleRectLine,
				mBitmapPaint);
		int textWidth = (int) mTextPaint.measureText(showProgress);
		canvas.drawText(showProgress, textBounds.left - textWidth / 2,
				textBounds.height(), mTextPaint);
	}

	public void setProgress(int progress) {
		final int total = getScreenSize(getContext()).x * progress / 100;
		textBounds = new Rect();
		showProgress = progress + "%";
		mTextPaint.getTextBounds(showProgress, 0, showProgress.length(),
				textBounds);
		triangleRectLine = new Rect();
		mRectLine = new Rect();

		new Handler() {
			int progressAnimation = 0;

			public void handleMessage(Message msg) {
				if (progressAnimation < total) {
					progressAnimation += 10;
					textBounds.set(progressAnimation, textBounds.top,
							textBounds.width() + progressAnimation,
							textBounds.bottom);
					triangleRectLine.set(
							progressAnimation - triangle_progressbar.getWidth()
									/ 2,
							textBounds.height() + textMagingBottom,
							triangle_progressbar.getWidth() + progressAnimation
									- triangle_progressbar.getWidth() / 2,
							textBounds.height()
									+ triangle_progressbar.getHeight()
									+ textMagingBottom);
					mRectLine.set(
							mRectLine.left,
							triangle_progressbar.getHeight()
									+ textBounds.height()
									- line_progress_bar.getHeight()
									+ textMagingBottom,
							progressAnimation,
							triangle_progressbar.getHeight()
									+ textBounds.height() + textMagingBottom);
					invalidate();
					sendEmptyMessageDelayed(0, 10);
				}
			}
		}.sendEmptyMessage(0);
	}

	public Point getScreenSize(Context mContext) {
		Resources resources = mContext.getResources();
		DisplayMetrics dm = resources.getDisplayMetrics();
		return new Point(dm.widthPixels, dm.heightPixels);
	}
}


    

    
package com.sdw.typedarraydemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	private MyProgressBar progress;
	private Button btn_80;
	private Button btn_30;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		progress = (MyProgressBar) findViewById(R.id.progress);
		btn_80 = (Button) findViewById(R.id.btn_80);
		btn_30 = (Button) findViewById(R.id.btn_30);
		btn_80.setOnClickListener(this);
		btn_30.setOnClickListener(this);
		progress.setProgress(50);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_80:
			progress.setProgress(80);
			break;

		case R.id.btn_30:
			progress.setProgress(30);

			break;
		default:
			break;
		}
	}

}
这就是以上代码,实现的是一个类似的ProgressBar,根据setProgress(int progress)来设置进度,动态填充,也可以通过修改来实现动态更新,我们看到在Xml文件中有这样一行代码android:layout_height="50dp",现在把他改为android:layout_height="wrap_content",在运行一下看下结果

Android TypedArray使用方法_第2张图片

我们看到下面的两个button没有了,因为在上一篇的《 Android TypedArray源码详解》中最后讲到如果继承View的时候最好覆写他的onMeasure方法,如果不覆写,他的wrap_content和match_parent结果是一样的,充满整个屏幕,所以下面的两个button就看不到了,下面我来覆写一下onMeasure方法

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int textHight = textBounds == null ? textSize : textBounds.height();
		int hight = textHight + textMagingBottom
				+ triangle_progressbar.getHeight();
		setMeasuredDimension(width, hight);
	}
我们来看一下运行结果

Android TypedArray使用方法_第3张图片

下面的两个button已经出来了。





你可能感兴趣的:(Android)