android 开发 View _4_ 我的简单自定义ViewDemo
效果图:

代码:
package com.example.lenovo.mydemo.myViewDemo; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View; import com.example.lenovo.mydemo.R; /**
* Created by lenovo on 2018/7/2.
*/ public class MyView_1 extends View {
private final String TAG = "MyView_1";
private float radius;
private int color;
private Paint mPaint; public MyView_1(Context context) {
super(context);
} public MyView_1(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
} public MyView_1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); } @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e(TAG, "onDraw_radius:"+radius);
/*
用多线段画矩形(也可以直接drawRect画矩形)
*/
//创建画笔\ 关于画笔可以也可以不创建多个画笔使用paint1.reset();来重置画笔
Paint paint1 = new Paint(); //给画笔添加颜色
paint1.setColor(Color.RED);
//设置画笔宽度
paint1.setStrokeWidth(10);
//设置画笔转角(两个线相交的点)风格
paint1.setStrokeJoin(Paint.Join.MITER);
//设置抗锯齿
paint1.setAntiAlias(true);
float[] f = new float[]{
0,0,//第一条线的起始点
getWidth(),0,//第一条线的终点
getWidth(),0,//第二条线的起始点
getWidth(),getHeight(),//第二条线的终点
getWidth(),getHeight(),//同上
0,getHeight(),
0,getHeight(),
0,0};
//在画布上画多线段,添加坐标和画笔
canvas.drawLines(f,paint1); /*
画圆
*/
//创建画笔
Paint paint2 = new Paint();
//设置画笔颜色
paint2.setColor(getResources().getColor(R.color.colorPrimary));
//设置画笔宽度
paint2.setStrokeWidth(10);
//抗齿距
paint2.setAntiAlias(true);
//画空心圆 如果不设置默认是实心圆 另外2个参数是 FILL 和 FILL_AND_STROKE 分别都实心圆
paint2.setStyle(Paint.Style.STROKE);
//画圆形 传入X坐标Y坐标(2个坐标合起来做中心点),然后是半径200加画笔
canvas.drawCircle(getWidth()/2,getHeight()/2,200,paint2);
/*
画文字
*/
Paint paint3 = new Paint();
/*
----------- 字体类型 -------------
* Typeface.DEFAULT //常规字体类型 * Typeface.DEFAULT_BOLD //黑体字体类型 * Typeface.MONOSPACE //等宽字体类型 * Typeface.SANS_SERIF //sans serif字体类型 * Typeface.SERIF //serif字体类型
------------ 字体风格 ---------------
* * Typeface.BOLD //粗体 * Typeface.BOLD_ITALIC //粗斜体 * Typeface.ITALIC //斜体 * Typeface.NORMAL //常规
*/
Typeface typeface = Typeface.create(Typeface.DEFAULT,Typeface.NORMAL);
//导入字体类型和风格
paint3.setTypeface(typeface);
//设置字体大小
paint3.setTextSize(50);
//设置线宽
paint3.setStrokeWidth(5);
//设置颜色
paint3.setColor(Color.RED);
//设置文字居中
paint3.setTextAlign(Paint.Align.CENTER);
canvas.drawText("你好",getWidth()/2,getHeight()/2,paint3); }
}
android 开发 View _4_ 我的简单自定义ViewDemo的更多相关文章
- android 开发 View _1_ View的子类们 和 视图坐标系图
目录: android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览 android 开发 View _3_ View的属性动画ValueAnimator a ...
- Android开发——View滑动的三种实现方式
0. 前言 Android开发中,我们常常需要View滑动实现一些绚丽的效果来优化用户体验.一般View的滑动可以用三种方式实现. 转载请注明出处:http://blog.csdn.net/seu ...
- 黑客破译android开发代码真就那么简单?
很多程序员辛辛苦苦开发出的android开发代码,很容易就被黑客翻译了. Google似乎也发现了这个问题,从SDK2.3开始我们可以看到在android-sdk-windows\tools\下面多了 ...
- Android开发——View滑动冲突解决方案
0. 前言 我们在Android开发--事件分发机制详解中深入学习了事件分发机制,为我们解决Android开发中的滑动冲突问题做了初步准备.针对滑动冲突这里给出两种解决方案:外部拦截法和内部拦截法 ...
- android 开发 View _14 MotionEvent和事件处理详解,与实践自定义滑动条View
转载https://blog.csdn.net/huaxun66/article/details/52352469 MotionEvent MotionEvent对象是与用户触摸相关的时间序列,该序列 ...
- android 开发 View _8_ 动态图片自定义View
转载地址:https://blog.csdn.net/mengks1987/article/details/77770922 先来看下效果: 是不是有一种熟悉感,其实这种效果使用序列帧动画也是可以实现 ...
- android 开发 View _16 自定义计步器View、自定义柱状图View
/** *@content:实现计步的环形View *@time:2018-7-30 *@build: */ public class CountStepsAnnularView extends Vi ...
- android 开发 View _7_ 动态自定义View
效果图: 代码: package com.example.lenovo.mydemo.myViewDemo; import android.content.Context; import androi ...
- android 开发 View _10_ Path之基本操作
转载地址:http://www.gcssloop.com/customview/Path_Basic/ 安卓自定义View进阶-Path之基本操作 在上一篇Canvas之图片文字中我们了解了如何使用C ...
随机推荐
- SQL-49 针对库中的所有表生成select count(*)对应的SQL语句
题目描述 针对库中的所有表生成select count(*)对应的SQL语句CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_dat ...
- 进程管理02 通过PEB枚举进程所有模块
0x01 结构探究 先在win7 x86下通过windbg来探究通过peb来得到进程模块的步骤: 命令!process 0 0 exeplorer.exe 先获取到explorer.exe的EPRO ...
- 关于attibutedText输出中文字符后的英文和数字进行分开解析的问题
上面的图应该很清楚 具体这个attibutedText 是做什么的就不说了 ,最初我查了资料发现有人和我一样的输出,把一个字符串的中英文分开打印出来是iOS关于UItextVIew和UIlabel的差 ...
- java基础(1)IntelliJ IDEA入门和数组操作 解决idea启动速度慢--配置JVM
一. IntelliJ IDEA入门 1 快捷键和技巧 智能补全代码,比如只写首字母按回车: psvm+Enter :public stactic void main(String[] args) s ...
- Vue: 用 key 管理可复用的元素
<div id="login"> <template v-if="loginType === 'username'"> <labe ...
- select添加option
本文介绍select添加option的两种方法 1.使用selectObject.add(option,before)方法,其中 option为要添加选项元素.必需是 option 或 optgrou ...
- Web.xml详解分析
一.首先了解项目加载的优先级 首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter. 最终得出的结 ...
- Visual Studio 2017 常用快捷键
1.窗口快捷键 Ctrl+W,W: 浏览器窗口 Ctrl+W,S: 解决方案管理器 (Solution) Ctrl+W,C: 类视图 (Class) Ctrl+W,E: 错误列表 (Error) Ct ...
- cannot find package "context"
导入 github.com/go-sql-driver/mysql 和 database/sql 时,报cannot find package "context"的错误因为go1 ...
- alert大法看执行流程(一次采坑)
页面的dom元素加载完了,给元素一次性添加事件. 收获:事件都是一次性给添加好的,不是点击一次,................................................... ...