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

android 自定义 View

2015-10-26 13:56 651 查看
首先创建attrs.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"
android:layout_height="match_parent">

<!--在布局中声明我们的自定义View
eclispe 下 需要使用app的package:xmlns:custom="http://schemas.android.com/apk/res/自定义包"
android studio 下:xmlns:custom="http://schemas.android.com/apk/res-auto"-->

<com.zhang.hongchao.myview.view.CustomTitleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:mTitleText=""
android:padding="10dp"
custom:mTitleTextColor="#FF0000"
android:layout_centerInParent="true"
custom:mTitleTextSize="40dp"
/>

</RelativeLayout>


创建类

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.zhang.hongchao.myview.R;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
* Created by hongchao on 2015/10/22.
* 1. 自定义View的属性
* 2. 在View的构造方法中获得我们自定义的属性
* 3. 重写onMesure
* 4. 重写onDraw
*/
public class CustomTitleView extends View {
private String mTitleText;//文本
private int mTitleTextColor;//文本颜色
private int mTitleTextSize;//文本大小

/**绘制时控制文本的绘制的范围*/
private Rect mBound;
private Paint mPaint;

public CustomTitleView(Context context) {
this(context,null);
}

public CustomTitleView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}

public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
/**获取定义的属性*/
TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView,
defStyleAttr,0
);
int n = array.getIndexCount();
for (int i=0;i<n;i++){
int attr = array.getIndex(i);
switch (attr){
case R.styleable.CustomTitleView_mTitleText:
mTitleText = array.getString(attr);
break;
case R.styleable.CustomTitleView_mTitleTextColor:
mTitleTextColor = array.getColor(attr, Color.BLACK);
break;
case R.styleable.CustomTitleView_mTitleTextSize:
mTitleTextSize = array.getDimensionPixelSize(attr,
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,16,
getResources().getDisplayMetrics()
));
break;
}
}
array.recycle();

/**绘制文本的宽和高*/
mPaint = new Paint();
mPaint.setTextSize(mTitleTextSize);
mBound = new Rect();
mPaint.getTextBounds(mTitleText,0,mTitleText.length(),mBound);
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTitleText = "zhanghongchao";
postInvalidate();
}
});
}

@Override
protected void onDraw(Canvas canvas) {
mPaint.setColor(Color.YELLOW);
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);

mPaint.setColor(mTitleTextColor);
canvas.drawText(mTitleText,getWidth()/2-mBound.width()/2,
getHeight()/2+mBound.height()/2,mPaint);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = 0;
int height = 0;

/**设置宽度*/
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);
switch (specMode){
case MeasureSpec.EXACTLY:
width = getPaddingLeft()+getPaddingRight()+specSize;
break;
case MeasureSpec.AT_MOST:
width = getPaddingLeft()+getPaddingRight()+mBound.width();
break;
}

/**设置高度*/
specMode = MeasureSpec.getMode(heightMeasureSpec);
specSize = MeasureSpec.getSize(heightMeasureSpec);
/**
* EXACTLY:一般是设置了明确的值或者是MATCH_PARENT
* AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
* UNSPECIFIED:表示子布局想要多大就多大,很少使用
*/
switch (specMode){
case MeasureSpec.EXACTLY:
height = getPaddingTop()+getPaddingBottom()+specSize;
break;
case MeasureSpec.AT_MOST:
height = getPaddingTop()+getPaddingBottom()+mBound.height();
break;
}
setMeasuredDimension(width,height);
}
}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"
android:layout_height="match_parent">

<!--在布局中声明我们的自定义View
eclispe 下 需要使用app的package:xmlns:custom="http://schemas.android.com/apk/res/自定义包"
android studio 下:xmlns:custom="http://schemas.android.com/apk/res-auto"-->

<com.zhang.hongchao.myview.view.CustomTitleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:mTitleText=""
android:padding="10dp"
custom:mTitleTextColor="#FF0000"
android:layout_centerInParent="true"
custom:mTitleTextSize="40dp"
/>

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