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>
相关文章推荐
- Android NDK 开发 第一步的几个坑
- Android上玩玩Hook?
- Android 开发常用代码片段
- android支付之银联支付
- Android 对话框【Dialog】去除白色边框代码
- Android开发常用工具类
- Android--动画Animation
- Android四个基本组件(2)之Service 服务与Content Provider内容提供商
- NativeActivity通过jni调用java层的Android API
- Android studio集合
- android 随机数Random
- Android屏幕适配全攻略(最权威的官方适配指导)
- Android实现带动画的下拉刷新RecyclerView
- Android 手势滑动,多点触摸放大缩小图片
- android入门---Activity组件.活动(一)
- Context
- 【每日一点】android VideoView
- 即时聊天IM之三 XMPP协议客户端库的和Android端框架概述
- android布局的问题----有些布局始终置底
- Android 使用Websocket