半年之前自己学习时写的一个小例子,贴上来作为笔记
package com.test.defview2;
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.util.AttributeSet;
import android.view.View;
public class MyView extends View {
private Bitmap bitmap = null;
private int imagescaletype = 0;
private String text = "";
private int textsize = 0;
private Paint mPaint = null;
Rect rTextRect = null;
Rect r = null;
int mWidth = 0;
int mHeight = 0;
Rect newR = null;
public MyView(Context context) {
this(context,null);
}
public MyView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, defStyleAttr, 0);
int count = ta.getIndexCount();
for(int i = 0; i< count;i++)
{
int k = ta.getIndex(i);
switch(k){
case R.styleable.MyView_text:
text = ta.getString(k);
break;
case R.styleable.MyView_textsize:
textsize = ta.getInt(k, 3);
break;
case R.styleable.MyView_image:
bitmap = BitmapFactory.decodeResource(context.getResources(), ta.getResourceId(k, 0));
break;
case R.styleable.MyView_imagescaletype:
imagescaletype = ta.getInt(k, 0);
break;
default:
break;
}
}
ta.recycle();
r = new Rect();
newR = new Rect();
mPaint = new Paint();
mPaint.setTextSize(textsize);
rTextRect = new Rect();
mPaint.getTextBounds(text, 0, text.length(), rTextRect);
}
//计算的是整个自定义view的尺寸
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthmode = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);//初步得出的此自定义view的宽
int widthDText = 0;
int widthDBmp = 0;
switch(widthmode)
{
case MeasureSpec.AT_MOST://wrap_content
widthDText = rTextRect.width()+getPaddingLeft()+getPaddingRight();
widthDBmp = bitmap.getWidth()+getPaddingLeft()+getPaddingRight();
int calWidth = Math.max(widthDText, widthDBmp);
mWidth = Math.min(calWidth, width);
break;
case MeasureSpec.EXACTLY://match_parent or exactly value
mWidth = width;
break;
default:
break;
}
int heightmode = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
switch(heightmode)
{
case MeasureSpec.AT_MOST://wrap_content
int caculateHeight = rTextRect.height()+bitmap.getWidth()+getPaddingTop()+getPaddingBottom();
mHeight = Math.min(height, caculateHeight);
break;
case MeasureSpec.EXACTLY://match_parent or exactly value
mHeight = height;
break;
default:
break;
}
setMeasuredDimension(mWidth, mHeight);
}
// @Override
// protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
// mPaint.setColor(Color.YELLOW);
// mPaint.setStrokeWidth(4);
// mPaint.setStyle(Style.STROKE);
// //外框
// canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);
//
//
// r.left = mWidth-getPaddingLeft();
// r.top = mHeight-getPaddingTop();
// r.right = mWidth-getPaddingRight();
// r.bottom = mHeight-getPaddingBottom();
// //文字
// mPaint.setStyle(Style.FILL);
// mPaint.setColor(0xffff0000);
// canvas.drawText(text, mWidth/2-rTextRect.width()/2, mHeight-getPaddingBottom(), mPaint);
//
// //图片
// r.bottom = r.bottom - rTextRect.height();
// if(imagescaletype == 0)//fillxy
// {
// canvas.drawBitmap(bitmap, null, r, mPaint);;
// } else if(imagescaletype == 1)//center
// {
// r.left = mWidth/2 - bitmap.getWidth()/2;
// r.top = (mHeight-rTextRect.height())/2 - bitmap.getHeight()/2;
// r.right = mWidth/2 + bitmap.getWidth()/2;
// r.bottom = (mHeight-rTextRect.height())/2 + bitmap.getHeight()/2;
// canvas.drawBitmap(bitmap, null, r, mPaint);;
// }
//
// }
@Override
public void onDraw(Canvas canvas){
mPaint.setColor(Color.YELLOW);
mPaint.setStrokeWidth(4);
mPaint.setStyle(Style.STROKE);
//外框
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);
r.left = mWidth-getPaddingLeft();
r.top = mHeight-getPaddingTop();
r.right = mWidth-getPaddingRight();
r.bottom = mHeight-getPaddingBottom();
//文字
mPaint.setStyle(Style.FILL);
mPaint.setColor(0xffff0000);
canvas.drawText(text, mWidth/2-rTextRect.width()/2, mHeight-bitmap.getWidth()-getPaddingBottom(), mPaint);
//图片
r.top = r.top - rTextRect.height();
if(imagescaletype == 0)//fillxy
{
canvas.drawBitmap(bitmap, null, r, mPaint);;
} else if(imagescaletype == 1)//center
{
r.left = mWidth/2 - bitmap.getWidth()/2;
r.top = (mHeight-getPaddingTop()-getPaddingBottom()-rTextRect.height())/2 +rTextRect.height()+getPaddingTop();
r.right = mWidth/2 + bitmap.getWidth()/2;
r.bottom = mHeight-getPaddingBottom();
canvas.drawBitmap(bitmap, null, r, mPaint);;
}
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ns="http://schemas.android.com/apk/res/com.test.defview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:orientation="vertical">
<com.test.defview2.MyView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ns:text="nihao"
ns:textsize="30"
ns:image="@drawable/ic_launcher"
ns:imagescaletype="center"
/>
<com.test.defview2.MyView
android:layout_width="match_parent"
android:layout_height="wrap_content"
ns:text="nihaozhongguo"
ns:textsize="50"
ns:image="@drawable/fasd"
ns:imagescaletype="center"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
android:scaleType="center"
android:src="@drawable/fasd"/>
</LinearLayout>
属性文件values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="text" format="string"/>
<attr name="textsize" format="integer"/>
<attr name="image" format="reference"/>
<attr name="imagescaletype" >
<enum name="fillxy" value="0"/>
<enum name="center" value="1"/>
</attr>
</declare-styleable>
</resources>