Android TypedArray使用方法

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

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


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

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyProgressBar">
        <attr name="textSize" format="integer" />
        <attr name="textColor" format="color" />
    </declare-styleable>

</resources>
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);
	}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.sdw.typedarraydemo.MyProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="@string/hello_world"
        app:textColor="#FFFF0000"
        app:textSize="32" />

    <Button
        android:id="@+id/btn_80"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="80%" />

    <Button
        android:id="@+id/btn_30"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="30%" />

</LinearLayout>
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",在运行一下看下结果



我们看到下面的两个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);
	}
我们来看一下运行结果


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





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据结构和算法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值