当前位置: 首页 > news >正文

深圳知名网站建设平台高清做网站插图

深圳知名网站建设平台,高清做网站插图,网站开发程序员岗位职责,寻找大连网站建设转载请标明出处#xff1a;http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View#xff0c;可能都是比较恐惧的#xff0c;但是这又是高手进阶的必经之路#xff0c;所有准备在自定义View上面花一些功夫#x… 转载请标明出处http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View可能都是比较恐惧的但是这又是高手进阶的必经之路所有准备在自定义View上面花一些功夫多写一些文章。先总结下自定义View的步骤 1、自定义View的属性 2、在View的构造方法中获得我们自定义的属性 [ 3、重写onMesure ] 4、重写onDraw 我把3用[]标出了所以说3不一定是必须的当然了大部分情况下还是需要重写的。 1、自定义View的属性首先在res/values/  下建立一个attrs.xml 在里面定义我们的属性和声明我们的整个样式。 ?xml version1.0 encodingutf-8? resourcesattr nametitleText formatstring /attr nametitleTextColor formatcolor /attr nametitleTextSize formatdimension /declare-styleable nameCustomTitleViewattr nametitleText /attr nametitleTextColor /attr nametitleTextSize //declare-styleable/resources我们定义了字体字体颜色字体大小3个属性format是值该属性的取值类型: 一共有string,color,demension,integer,enum,reference,float,boolean,fraction,flag;不清楚的可以google一把。 然后在布局中声明我们的自定义View RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsxmlns:customhttp://schemas.android.com/apk/res/com.example.customview01android:layout_widthmatch_parentandroid:layout_heightmatch_parent com.example.customview01.view.CustomTitleViewandroid:layout_width200dpandroid:layout_height100dpcustom:titleText3712custom:titleTextColor#ff0000custom:titleTextSize40sp //RelativeLayout 一定要引入 xmlns:customhttp://schemas.android.com/apk/res/com.example.customview01我们的命名空间后面的包路径指的是项目的package 2、在View的构造方法中获得我们的自定义的样式 /*** 文本*/private String mTitleText;/*** 文本的颜色*/private int mTitleTextColor;/*** 文本的大小*/private int mTitleTextSize;/*** 绘制时控制文本绘制的范围*/private Rect mBound;private Paint mPaint;public CustomTitleView(Context context, AttributeSet attrs){this(context, attrs, 0);}public CustomTitleView(Context context){this(context, null);}/*** 获得我自定义的样式属性* * param context* param attrs* param defStyle*/public CustomTitleView(Context context, AttributeSet attrs, int defStyle){super(context, attrs, defStyle);/*** 获得我们所定义的自定义样式属性*/TypedArray a context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0);int n a.getIndexCount();for (int i 0; i n; i){int attr a.getIndex(i);switch (attr){case R.styleable.CustomTitleView_titleText:mTitleText a.getString(attr);break;case R.styleable.CustomTitleView_titleTextColor:// 默认颜色设置为黑色mTitleTextColor a.getColor(attr, Color.BLACK);break;case R.styleable.CustomTitleView_titleTextSize:// 默认设置为16spTypeValue也可以把sp转化为pxmTitleTextSize a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));break;}}a.recycle();/*** 获得绘制文本的宽和高*/mPaint new Paint();mPaint.setTextSize(mTitleTextSize);// mPaint.setColor(mTitleTextColor);mBound new Rect();mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);} 我们重写了3个构造方法默认的布局文件调用的是两个参数的构造方法所以记得让所有的构造调用我们的三个参数的构造我们在三个参数的构造中获得自定义属性。 3、我们重写onDrawonMesure调用系统提供的 Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){super.onMeasure(widthMeasureSpec, heightMeasureSpec);}Overrideprotected 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);}此时的效果是 是不是觉得还不错基本已经实现了自定义View。但是此时如果我们把布局文件的宽和高写成wrap_content会发现效果并不是我们的预期 系统帮我们测量的高度和宽度都是MATCH_PARNET当我们设置明确的宽度和高度时系统帮我们测量的结果就是我们设置的结果当我们设置为WRAP_CONTENT,或者MATCH_PARENT系统帮我们测量的结果就是MATCH_PARENT的长度。 所以当设置了WRAP_CONTENT时我们需要自己进行测量即重写onMesure方法” 重写之前先了解MeasureSpec的specMode,一共三种类型 EXACTLY一般是设置了明确的值或者是MATCH_PARENT AT_MOST表示子布局限制在一个最大值内一般为WARP_CONTENT UNSPECIFIED表示子布局想要多大就多大很少使用 下面是我们重写onMeasure代码 Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){int widthMode MeasureSpec.getMode(widthMeasureSpec);int widthSize MeasureSpec.getSize(widthMeasureSpec);int heightMode MeasureSpec.getMode(heightMeasureSpec);int heightSize MeasureSpec.getSize(heightMeasureSpec);int width;int height ;if (widthMode MeasureSpec.EXACTLY){width widthSize;} else{mPaint.setTextSize(mTitleTextSize);mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds);float textWidth mBounds.width();int desired (int) (getPaddingLeft() textWidth getPaddingRight());width desired;}if (heightMode MeasureSpec.EXACTLY){height heightSize;} else{mPaint.setTextSize(mTitleTextSize);mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds);float textHeight mBounds.height();int desired (int) (getPaddingTop() textHeight getPaddingBottom());height desired;}setMeasuredDimension(width, height);}现在我们修改下布局文件 RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsxmlns:customhttp://schemas.android.com/apk/res/com.example.customview01android:layout_widthmatch_parentandroid:layout_heightmatch_parent com.example.customview01.view.CustomTitleViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentcustom:titleText3712android:padding10dpcustom:titleTextColor#ff0000android:layout_centerInParenttruecustom:titleTextSize40sp //RelativeLayout 现在的效果是 完全复合我们的预期现在我们可以对高度、宽度进行随便的设置了基本可以满足我们的需求。 当然了这样下来我们这个自定义View与TextView相比岂不是没什么优势所有我们觉得给自定义View添加一个事件 在构造中添加 this.setOnClickListener(new OnClickListener(){Overridepublic void onClick(View v){mTitleText randomText();postInvalidate();}});private String randomText(){Random random new Random();SetInteger set new HashSetInteger();while (set.size() 4){int randomInt random.nextInt(10);set.add(randomInt);}StringBuffer sb new StringBuffer();for (Integer i : set){sb.append( i);}return sb.toString();} 下面再来运行 我们添加了一个点击事件每次让它随机生成一个4位的随机数有兴趣的可以在onDraw中添加一点噪点然后改写为验证码是不是感觉很不错。 好了各位学习的打酱油的留个言顶个呗~ 源码点击此处下载 版权声明本文为博主原创文章未经博主允许不得转载。 转载于:https://www.cnblogs.com/dingxiaoyue/p/4924976.html
http://www.lebaoying.cn/news/20783.html

相关文章:

  • 站酷设计师网站网站页面统计代码是什么
  • 网站费用wordpress如何换内页模板
  • 网站建设html代码高端公司网站
  • 张掖建设局网站交易网站开发文档
  • 可做装饰推广的网站视频剪辑软件
  • 网站建设需要什么技术宿州网站建设费用
  • 网站推广服务器怎么选做网站需要什么
  • 商务网站建设规划百度运营平台
  • 建立网站教学的网站建设合同封皮
  • 校园二手物品交易网站怎么做南通小程序制作
  • 网站和网页域名怎么做网站
  • 电子商务网站建设教学总结响应式网站开发费用
  • 长沙大型网站设计公司.vip域名做网站
  • 产品网页的制作宁波seo搜索优化费用
  • 响应式大学网站大型网络游戏排行榜2021前十名
  • 建设文明网站包括哪些网站有中文域名
  • ps做网站效果图均安公司网站建设
  • 常用的设计师网站wordpress zh cn.po
  • wordpress 目录权限设置上海seo优化公司 kinglink
  • 开发一个网站的流程东莞做好网络推广
  • 佛山美容院网站建设网站建设好做吗
  • 织梦 两个网站网站动态背景欣赏
  • 做网站用什么电脑配置遨翔网站建设
  • 淘宝优惠券网站建设总代利趣网站开发商
  • 凡科网让经营更简单湖南企业竞价优化首选
  • 营销型网站价格网站的上一页怎么做的
  • 保定网站建设推广公司怎么样泉州网站制作哪个好薇
  • 重庆网络推广网站推广网站的域名分为哪些
  • 动态手机网站怎么做的太原网站建设培训学校
  • 六种常见的网站类型建设网站赚钱的方法