画布之ShapeDrawable
package com.loaderman.customviewdemo; import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.ShapeDrawable;
import android.util.AttributeSet;
import android.view.View; public class ShapeView extends View {
private ShapeDrawable mShapeDrawable;
public ShapeView(Context context) {
super(context);
init();
} public ShapeView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public ShapeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init(){
setLayerType(LAYER_TYPE_SOFTWARE,null);
// //RectShape
// mShapeDrawable = new ShapeDrawable(new RectShape());
// mShapeDrawable.setBounds(new Rect(50,50,200,100));
// mShapeDrawable.getPaint().setColor(Color.YELLOW); // //OvalShape
// mShapeDrawable = new ShapeDrawable(new OvalShape());
// mShapeDrawable.setBounds(new Rect(50,50,200,100));
// mShapeDrawable.getPaint().setColor(Color.YELLOW); // //ArcShape 15968145727
// mShapeDrawable = new ShapeDrawable(new ArcShape(0,300));
// mShapeDrawable.setBounds(new Rect(50,50,200,100));
// mShapeDrawable.getPaint().setColor(Color.YELLOW); // //RoundRectShape
// float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };
// RectF inset = new RectF(6, 6, 6,6);
// float[] innerR = new float[] { 50, 12, 0, 0, 12, 50, 0, 0 };
// mShapeDrawable = new ShapeDrawable(new RoundRectShape(outerR,inset,innerR));
// mShapeDrawable.setBounds(new Rect(50,50,200,100));
// mShapeDrawable.getPaint().setColor(Color.BLACK); // //PathShape
// Path path = new Path();
// path.moveTo(0,0);
// path.lineTo(100,0);
// path.lineTo(100,100);
// path.lineTo(0,100);
// // 封闭前面点所绘制的路径
// path.close();
// mShapeDrawable = new ShapeDrawable(new PathShape(path,100,200));
// mShapeDrawable.setBounds(new Rect(0,0,250,150));
// mShapeDrawable.getPaint().setColor(Color.YELLOW); //ReginShape
//构造两个矩形
Rect rect1 = new Rect(50,0,90,150);
Rect rect2 = new Rect(0,50,250,100);
//构造两个Region
Region region = new Region(rect1);
Region region2= new Region(rect2);
//取两个区域的交集
region.op(region2, Region.Op.XOR);
mShapeDrawable = new ShapeDrawable(new RegionShape(region));
mShapeDrawable.setBounds(new Rect(0,0,200,100));
mShapeDrawable.getPaint().setColor(Color.YELLOW);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mShapeDrawable.draw(canvas);
}
}
package com.loaderman.customviewdemo; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View; public class TelescopeView extends View {
private Bitmap bitmap;
private ShapeDrawable drawable;
// 放大镜的半径 private static final int RADIUS = 80;
// 放大倍数 private static final int FACTOR = 3;
private final Matrix matrix = new Matrix(); public TelescopeView(Context context) {
super(context);
init();
} public TelescopeView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public TelescopeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init() {
setLayerType(LAYER_TYPE_SOFTWARE, null);
} @Override
public boolean onTouchEvent(MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
// 这个位置表示的是,画shader的起始位置
matrix.setTranslate(RADIUS - x * FACTOR, RADIUS - y * FACTOR);
drawable.getPaint().getShader().setLocalMatrix(matrix); // bounds,就是那个圆的外切矩形
drawable.setBounds(x - RADIUS, y - RADIUS, x + RADIUS, y + RADIUS);
invalidate();
return true;
} @Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bitmap == null) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.scenery);
bitmap = Bitmap.createScaledBitmap(bmp, getWidth(), getHeight(), false); BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(bitmap,
bitmap.getWidth() * FACTOR, bitmap.getHeight() * FACTOR, true),
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setShader(shader);
drawable.setBounds(0, 0, RADIUS * 2, RADIUS * 2);
} canvas.drawBitmap(bitmap, 0, 0, null);
drawable.draw(canvas); } }
效果:

画布之ShapeDrawable的更多相关文章
- Graphics简单汇总
1.主页面布局文件 activity_main.xml(仅仅有2个button按钮) <?xml version="1.0" encoding="utf-8&quo ...
- 安卓自定义控件(二)BitmapShader、ShapeDrawable、Shape
第一篇博客中,我已经Canvas.Paint.Shader.Xfermode这些对象做了总结,而现在这篇文章主要介绍BitmapShader位图渲染,Xfermode如何实际应用,还有形状的绘制.不过 ...
- ShapeDrawable做放大镜效果
引用一下ShapeDrawable的类的说明: java.lang.Object ? android.graphics.drawable.Drawable ? android.grap ...
- 06. Web大前端时代之:HTML5+CSS3入门系列~HTML5 画布
Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 我们先看看画布的魅力: 初始画布 canvas默认是宽3 ...
- 如何快速清除ZBrush画布中多余图像
ZBrush是一款数字雕刻与绘画软件,它以强大的功能和直观的工作流程彻底改变了整个三维行业.它的简洁化.智能化和人性化的设计无不让众多用户所折服.刚接触它的用户可能会因为找不到相关命令或不熟悉而觉得它 ...
- 矢量图绘制工具Svg-edit调整画布的大小
矢量图绘制工具Svg-edit调整画布的大小 ------------------------------ ------------------------
- HTML5 中的 canvas 画布(一)
---恢复内容开始--- 在HTML5中新添加的元素,canvas 现在支持 IE9+的版本 注意:HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript ...
- html 5 canvas画布整理
1. 创建canvas画布<canvas id="myCanvas" width="800" height="800" >< ...
- Quartz 2D在ios中的使用简述二:创建画布
在iOS中使用Quartz画图时,第一步就是要获取画布(图形上下文),然后再画布上做各种操作.先看下CoreGraphics.h这个头文件,就可以知道能够创建多少种上下文类型. #include &l ...
随机推荐
- spark 实现多文件输出
需求 不同的key输出到不同的文件 txt文件 multiple.txt 中国;22 美国;4342 中国;123 日本;44 日本;6 美国;55 美国;43765 日本;786 日本;55 sca ...
- awk初级教程
参考:sed & awk 概述 sed & awk指令组成 与sed区别 尽管awk指令与sed指令的结构相同,都由模式和过程两部分组成,但过程本身有很大不同. awk看上去不像编辑器 ...
- DNS服务——域名解析委派
域名解析委派 域名解析委派和DNS域名解析递归查询很像,举个例子解释域名解析委派 ①假设在.net域名下有台计算机想要访问www.cac.com. ②.net这台DNS服务器不知道www.cac.co ...
- Linux下Mysql服务安装【1】
https://www.cnblogs.com/xiaxiaoxu/p/9978976.html 第一步:获取mysql8.0的yum源 进入mysql官网获取RPM包下载地址 https://dev ...
- python requests包的基本使用
Requests 安装requests模块: D:\Install\Python36>pip3 install requests 请求方式 #coding:utf-8 import reques ...
- 你应该知道的16个linux命令
Linux常用命令很多比如cd.ls.pwd.du.df.tail.head.yum.mv.touch.mkdir.cp.tar等,根据工作内容不同,经常使用的命令也不同,比如运维人员使用tail.h ...
- 读取Excel数据到DataTable
读取Excel数据到DataTable 代码 /// <summary> /// 获取指定路径.指定工作簿名称的Excel数据:取第一个sheet的数据 /// </summary& ...
- nginx location中root指令和alias指令的区别
nginx location中root指令和alias指令 功能:将url映射为文件路径,以返回静态文件内容 差别:root会将完整的url映射进文件路径中 alias只会将location后的url ...
- JAVA的选择结构
1.基本选择结构if 案例:如果Java考试成绩大于98分则奖励MP4 public class Demo02 { public static void main ...
- MySQL 优化--持续整理
一.innodb体系结构优化: 1.IO优化 IO能力不足时 innodb_io_capacity 应该降低 innodb_max_dirty_pages_pct 应该降低 innodb_max_di ...