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 ...
随机推荐
- python基础提高演练(名片管理系统的开发)
综合应用 —— 名片管理系统 目标 综合应用已经学习过的知识点: 变量 流程控制 函数 模块 开发 名片管理系统 系统需求 1. 程序启动,显示名片管理系统欢迎界面,并显示功能菜单 ********* ...
- 剑指Offer 39. 平衡二叉树 (二叉树)
题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 题目地址 https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222 ...
- 剑指Offer 50. 数组中重复的数字 (数组)
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- Linux 驱动——从宏观上掌握基本框架
一.一个简单的驱动程序实例 led_drv.c 驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include ...
- tomcat升级 遇到的坑
今天说说tomcat升级后出的问题 以前的版本是8.0.30的 因用安全漏洞 需要升级tomcat 为8.5.28的版本 升级后jvm的配置 等等都和一起一样,过了几天发现,我们的错误日志和处理影响转 ...
- 关于multi-index
[转载请注明出处]http://www.cnblogs.com/mashiqi 2017/02/22 将$D^{\alpha}$和$\partial^{\alpha}$区别对待.$D^{\alpha} ...
- Oracle使用笔记(三)
在使用oracle过程中,总会遇到用户锁定,密码失效等问题,对于这些问题,总结了以下经验: 一.用户被锁定原因及解锁 对于用户被锁定,有以下几种原因: 1.密码过期: Oracle数据库的用户密码有效 ...
- 14.python-CS编程
一.客户端/服务器架构1.C/S架构:(1)硬件C/S架构(打印机)(2)软件C/S架构(web服务)2.生活中的C/S架构:饭店是S端,所有食客是C端3.C/S架构与socket的关系:socke就 ...
- 为WebService添加身份验证的两种方法
方法一:SoapHeader 辅助类:MySoapHeader //SoapHeader 添加引用 using System.Web.Services.Protocols; #region 配置登录标 ...
- visio画图有感
昨天在和一个同事看流程图,在我还在考虑图的含义时他说这个图太乱了,如果要团队成员看也会很费劲,并找出觉得画的好的案例. 对比两个图我发现了一个最大的差别是好的图形状都是水平或垂直对齐的,连接线也都是水 ...