您的位置:首页 > 移动开发 > Android开发

android自定义View 之仿QQ消息头像

2014-12-27 10:02 423 查看
在以前开发产品中需要自己 开发 一个 IM功能 所以 头像+消息提示 本来 用图片拼的 现在 我 用自定义View 写了 一个,刚接触 不是很熟悉 ,不足 之处请大家多多指教话不多说上代码

package com.zhl.ziview;

import android.content.Context;
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.Paint.Style;

import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MyViews extends View {

	private Paint mPaint;// 画笔
	private Rect mRect;// 控件占用的大小
	private int mTextSize;// 字体大小
	private int mTextColor;// 字体颜色
	private String mText;// 字体

	public MyViews(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public MyViews(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public MyViews(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		TypedArray mTyarray = context.obtainStyledAttributes(attrs,
				R.styleable.MyView);
		int n = mTyarray.getIndexCount();
		for (int i = 0; i < n; i++) {

			int count = mTyarray.getIndex(i);
			switch (count) {
			case R.styleable.MyView_text:
				mText = mTyarray.getString(count);
				break;
			case R.styleable.MyView_textColor:
				// mPaint.setColor(mTyarray.getColor(R.styleable.MyView_textColor,
				// 0XFF00FF00));
				mTextColor = mTyarray.getColor(count, Color.BLUE);
				break;
			case R.styleable.MyView_textSize:
				// mPaint.setTextSize(mTyarray.getDimension(
				// R.styleable.MyView_textColor, 30));
				mTextSize = mTyarray.getDimensionPixelSize(count,
						R.dimen.text_size);
				break;
			default:
				break;

			}
			mTyarray.recycle();
			mPaint = new Paint();// 初始化画笔
			mRect = new Rect();// 初始化绘制区域
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		int withmode = MeasureSpec.getMode(widthMeasureSpec);
		int withsize = MeasureSpec.getSize(widthMeasureSpec);
		int heightmode = MeasureSpec.getMode(heightMeasureSpec);
		int heightsize = MeasureSpec.getSize(heightMeasureSpec);
		int with, height;// 最终大小

		if (withmode == MeasureSpec.EXACTLY) {
			with = withsize;

		} else {
			// 获得控件自适应的大小
			mPaint.setTextSize(mTextSize);
			mPaint.getTextBounds(mText, 0, mText.length(), mRect);
			float textWidth = mRect.width();
			int width2 = (int) (getPaddingLeft() + textWidth + getPaddingRight());
			with = width2;
		}
		if (heightmode == MeasureSpec.EXACTLY) {
			height = heightsize;
		} else {
			mPaint.setTextSize(mTextSize);
			mPaint.getTextBounds(mText, 0, mText.length(), mRect);
			float textHeight = mRect.height();
			int height2 = (int) (getPaddingTop() + textHeight + getPaddingBottom());
			height = height2;
		}
		setMeasuredDimension(with, height);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub

		// mPaint.setColor(Color.RED);
		// mPaint.setStyle(Style.FILL);
		// canvas.drawCircle(50, 50, 50, mPaint);

		//

		Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher);
		Drawable drawable = new BitmapDrawable(mBitmap);// 转换为drawable
		// canvas.drawBitmap(mBitmap, getWidth() / 2 - (100), getHeight() / 2,
		// mPaint); 这是绘制一个图片

		this.setBackgroundDrawable(drawable);

		canvas.translate(getWidth() - (getWidth() / 4), getHeight() / 2
				- getHeight() / 3);// 下面绘制的控件的位置
		// 偏右20dp

		// canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(),
		// mPaint);
		// mPaint.setColor(Color.RED);
		// mPaint.setStyle(Style.FILL);
		// mPaint.setAlpha(0);
		// mPaint.setStrokeJoin(Paint.Join.ROUND);
		// mPaint.setStrokeCap(Paint.Cap.ROUND);
		// mPaint.setAntiAlias(true);
		// mPaint.setStyle(Style.STROKE);
		// mPaint.setStrokeWidth(3);
		mPaint.setColor(Color.RED);
		canvas.drawCircle(0, 0, 20, mPaint);// 绘制红色的圆

		// 绘制红色圆中展示的文字
		mPaint.setTextSize(mTextSize);
		mPaint.setColor(mTextColor);

		mPaint.getTextBounds(mText, 0, mText.length(), mRect);

		canvas.drawText(mText, -mRect.width() / 2, 0, mPaint);

	}

	/*
	 * 设置新消息的数量
	 */
	public void setText(int textCount) {
		if (textCount != 0) {
			this.mText = String.valueOf(textCount);

			invalidate();
		}

	}

	/**
	 * 当消息 呗查看后 为空
	 */

	public void setGone() {
		if (mPaint != null) {

			mPaint.setColor(Color.TRANSPARENT);
			invalidate();

		}

	}

	/**
	 * 设置背景图片 可能是从网络上加载的图片
	 */
	public void setBitmap(Bitmap bitMap) {
		if (bitMap != null) {
			Drawable drawable = new BitmapDrawable(bitMap);// bitmap转换为drawable

			this.setBackgroundDrawable(drawable);
			invalidate();
		}
	}

}
下面是我们自定义的属性

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

    <declare-styleable name="MyView">
        <attr name="text" format="string" />
        <attr name="textColor" format="color"></attr>
        <attr name="textSize" format="dimension"></attr>
    </declare-styleable>

</resources>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: