自定义View的多个情况
来源:互联网 发布:log4j 只输出sql 编辑:程序博客网 时间:2024/06/05 15:04
package com.bwie.attrsview;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.Path;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.Typeface;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;/** * Created by T_baby on 17/09/28. */public class myview extends View { private Paint paint; private String text; private int color, weight; private float size; private Rect rect; Context context; 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); paint = new Paint(); TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.mytext, defStyleAttr, 0); text = type.getString(R.styleable.mytext_mText); color = type.getColor(R.styleable.mytext_mTextColor, Color.RED); size = type.getDimension(R.styleable.mytext_mTextSize, 100); weight = type.getInteger(R.styleable.mytext_mTextweight, 10); type.recycle(); paint.setTextSize(size); paint.setColor(color); //获得文本的宽高 rect = new Rect(); paint.getTextBounds(text, 0, text.length(), rect); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.GRAY); //圆角边框 Paint paint2 = new Paint(); //设置圆角空心 paint2.setStyle(Paint.Style.STROKE); //圆环宽度 paint2.setStrokeWidth(10); //锯齿 paint2.setAntiAlias(true); //背景颜色 paint2.setColor(Color.WHITE); //圆角边框左上右下的坐标 RectF rec = new RectF(canvas.getWidth() / 2 - rect.width() / 2 - 20, canvas.getHeight() / 2 - rect.height(), canvas.getWidth() / 2 + rect.width() / 2 + 20, canvas.getHeight() / 2 + rect.height() * 3 / 2); //画圆角边框 canvas.drawRoundRect(rec, weight, weight, paint2); //内圆角边框 paint2.setColor(Color.YELLOW); //填充 paint2.setStyle(Paint.Style.FILL); //内圆角边框左上右下的坐标 RectF rec2 = new RectF(canvas.getWidth() / 2 - rect.width() / 2 - 17, canvas.getHeight() / 2 - rect.height() + 3, canvas.getWidth() / 2 + rect.width() / 2 + 17, canvas.getHeight() / 2 + rect.height() * 3 / 2 - 2); //画内圆角边框 canvas.drawRoundRect(rec2, weight, weight, paint2); //文字 canvas.drawText(text, canvas.getWidth() / 2 - rect.width() / 2, canvas.getHeight() / 2 + rect.height() / 2, paint); //矩形 canvas.drawRect(0, 0, 100, 100, paint2); //圆 canvas.drawCircle(200, 200, 100, paint2); //直线 canvas.drawLine(0,250,500,300,paint2); //弧线,扇形 paint2.setStyle(Paint.Style.STROKE);//设置空心 //范围 RectF oval1=new RectF(0,300,180,480); //范围,开始角度,划过角度,是否绘制边,画笔 canvas.drawArc(oval1, 0, 120, false, paint2);//弧形 //椭圆 paint2.setStyle(Paint.Style.FILL); RectF tuoyuan=new RectF(150,0,300,80); //新建一个椭圆外接矩形 canvas.drawOval(tuoyuan, paint2); //多边形 Path path = new Path(); path.moveTo(380, 200);// 此点为多边形的起点 path.lineTo(520, 350); path.lineTo(380, 350); path.close(); // 使这些点构成封闭的多边形 canvas.drawPath(path, paint2); paint2.setColor(Color.RED); //画点 canvas.drawPoint(60, 390, paint2);//画一个点 canvas.drawPoints(new float[]{60,400,65,400,70,400}, paint2); //贝塞尔曲线 Path path2=new Path(); path2.moveTo(100, 320);//设置Path的起点 path2.quadTo(150, 310, 170, 400); //设置贝塞尔曲线的控制点坐标和终点坐标 canvas.drawPath(path2, paint2);//画出贝塞尔曲线 //绘制图片 //画图片,就是贴图 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); canvas.drawBitmap(bitmap, 250,360, paint2); } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { color = Color.BLACK; int a = (int) (Math.random() * 10000); text = a + ""; invalidate(); //设置为粗体 paint.setFakeBoldText(true); //设置字体对齐 paint.setTextAlign(Paint.Align.LEFT); //文本缩放 paint.setTextScaleX(3.0f); //设置字体 paint.setTypeface(Typeface.DEFAULT_BOLD); } return super.onTouchEvent(event); }}
阅读全文
0 0
- 自定义View的多个情况
- Android解决自定义View获取不到焦点的情况
- 自定义View<1> 多个控件进行组合形成新的自定义控件
- 【Android自定义View 一】第一个自定义的控件
- springMVC处理表单重复提交,包含页面多个嵌套提交的情况,自定义token处理
- 自定义控件(12)---View的onMeasure默认、自定义情况(MeasureSpec)
- 自定义view画个圈圈
- 自定义View --画个钟表
- Android自定义View 做个简单的验证码控件
- 自定义控件2.第一个自定义view
- extends View的自定义view
- 多个if else 时的情况
- 自定义View(一)自定义View的分类
- 自定义view的自定义属性
- 自定义View的多节点实现
- 自定义的View
- 自定义View的使用
- 自定义view的收藏。
- Karaf教程第7部分- Camel JPA和JTA事务
- 数据库的多表查询
- OJ---敌兵布阵
- maven中整合ueditor步骤与常见问题(一)
- lua脚本学习2
- 自定义View的多个情况
- 【缓存】缓存中常见的4种问题分析以及解决方案
- 用Springdata Jpa给指定字段排序
- Karaf教程第8部分-分布式OSGi
- 【学习记录】maven安装与环境变量配置以及maven在eclipse中的安装
- javaWeb-jsp知识点详解
- 【深度学习读书笔记】第一章 引言
- 39二叉树的深度python
- 悟空qq通讯录加好友