Android Shape 形状
Shape继承体系:
Shape (android.graphics.drawable.shapes)
----PathShape (android.graphics.drawable.shapes)
----RectShape (android.graphics.drawable.shapes)
--------ArcShape (android.graphics.drawable.shapes)
--------OvalShape (android.graphics.drawable.shapes)
--------RoundRectShape (android.graphics.drawable.shapes)
RectShape
RectShape rectShape = new RectShape();
ShapeDrawable drawable = new ShapeDrawable(rectShape);
drawable.getPaint().setColor(Color.RED);
drawable.getPaint().setStyle(Paint.Style.FILL); //填充
view.setBackgroundDrawable(drawable);
矩形
RoundRectShape
float[] outerRadii = {20, 20, 40, 40, 60, 60, 80, 80};//外矩形 左上、右上、右下、左下 圆角半径
//float[] outerRadii = {20, 20, 20, 20, 20, 20, 20, 20};//外矩形 左上、右上、右下、左下 圆角半径
RectF inset = new RectF(100, 100, 200, 200);//内矩形距外矩形,左上角x,y距离, 右下角x,y距离
float[] innerRadii = {20, 20, 20, 20, 20, 20, 20, 20};//内矩形 圆角半径
//RoundRectShape roundRectShape = new RoundRectShape(outerRadii, inset, innerRadii);
RoundRectShape roundRectShape = new RoundRectShape(outerRadii, null, innerRadii); //无内矩形ShapeDrawable drawable = new ShapeDrawable(roundRectShape);
drawable.getPaint().setColor(Color.MAGENTA);
drawable.getPaint().setAntiAlias(true);
drawable.getPaint().setStyle(Paint.Style.STROKE);//描边
view.setBackground(drawable);
无内矩形的圆角矩形
带内矩形的圆角矩形
OvalShape
OvalShape ovalShape = new OvalShape();
ShapeDrawable drawable = new ShapeDrawable(ovalShape);
drawable.getPaint().setColor(Color.RED);
drawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
view.setBackgroundDrawable(drawable);
椭圆。 而当View的宽高相等时,就绘出了圆
ArcShape
ArcShape arcShape = new ArcShape(45, 270); //顺时针 开始角度45, 扫描的角度270 扇形
ShapeDrawable drawable = new ShapeDrawable(arcShape);
drawable.getPaint().setColor(Color.RED);
drawable.getPaint().setStyle(Paint.Style.FILL);// Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.aa)).getBitmap();
// BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader
// .TileMode.REPEAT);
// Matrix matrix = new Matrix();
// matrix.preScale(600.00f / bitmap.getWidth(), 600.00f / bitmap.getHeight());//view:w=600,h=600
// bitmapShader.setLocalMatrix(matrix);
// drawable.getPaint().setShader(bitmapShader);view.setBackgroundDrawable(drawable);
扇形图
结合BitmapShader
PathShape
Path path = new Path();
path.moveTo(50, 0);
path.lineTo(0, 50);
path.lineTo(50, 100);
path.lineTo(100, 50);
path.lineTo(50, 0);
PathShape pathShape = new PathShape(path, 200, 100);
ShapeDrawable drawable = new ShapeDrawable(pathShape);
drawable.getPaint().setColor(Color.RED);
drawable.getPaint().setStyle(Paint.Style.FILL);
imageView.setBackgroundDrawable(drawable);
以Path路径对象,来设定图形。
PathShape的构造函数:PathShape(path, stdWidth, stdHeight);
stdWidth:标准宽度
stdHeight:标准高度
在构造PathShape对象时,设置了宽高的标准。内部函数
protected void onResize(float width, float height) {mScaleX = width / mStdWidth;mScaleY = height / mStdHeight;
}public void draw(Canvas canvas, Paint paint) {canvas.save();canvas.scale(mScaleX, mScaleY);canvas.drawPath(mPath, paint);canvas.restore();
}
Shape基类中有函数 resize(),其中调用了onResize();ShapeDrawable中会调用resize()。
有了设定的标准宽高,再算出实际宽高与标准宽高的比率,最后在绘制时,画布canvas缩放。
造成的效果: path中的(x,y)坐标值 乘以 比率值,即是 最终呈现出的坐标值(实际内部是缩放的canvas)
比如,这里view的 w=400, h=400
如果标准宽高都等于400,那么canvas最终不缩放,即1:1。
PathShape pathShape = new PathShape(path, 400, 400);
stdx=400, stdy=400
PathShape pathShape = new PathShape(path, 100, 100);
stdx=100, stdy=100
PathShape pathShape = new PathShape(path, 200, 100);
stdx=200, stdy=100
Android Shape 形状相关推荐
- android shape 按钮背景_Android button, xml文件定义形状,代码中修改背景颜色
1. 首先在drawable文件夹定义一个shape.xml文件,内容如下: xmlns:android="http://schemas.android.com/apk/res/androi ...
- Android Shape制定圆形和圆环形状
用来记录方便后期快速找到 圆形形状 <shape xmlns:android="http://schemas.android.com/apk/res/android" and ...
- android shape.xml 文件使用
设置背景色可以通过在res/drawable里定义一个xml,如下: <?xml version="1.0" encoding="utf-8"?> ...
- android shape的可选参数以及每个参数的含义与用法!
2019独角兽企业重金招聘Python工程师标准>>> <span style="font-size:18px"><?xml version=& ...
- Android Shape Drawable Resources
本文主要介绍Drawable Resources的一种,Shape Drawable Resources的使用.其他Drawable类似 经常需要自己设置某个view的背景,比如类似新浪微博客户端微博 ...
- item name=android:width,Android:形状中的笔划创建笔划宽度的边距
我创建了一个矩形形状,以便将其用作列表项背景. 我的问题是笔划不遵循视图边框但是让笔划宽度为+/-. 这是我的形状的xml:<?xml version="1.0" encod ...
- android shape.xml 属性详解
转载源:http://blog.csdn.net/harvic880925/article/details/41850723 一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标 ...
- 【Android基础】android shape详解
一,概述 我们在android开发中经常要用到图片,而一些简单的图片我们完全可以用shape形状drawable资源代替,使用shape有一个好处就是可以减小我们apk的大小,因为同样的效果,shap ...
- android shape 按钮背景_Android UI:XML文件配置按钮等背景方案
(1)在开发中,我们经常会遇到一些纯色或带边框的简单样式的按钮或div,很多时候,都是用九宫格的背景图片来贴上去.但仔细想来,使用图片也带来了一些性能上的问题,包括过多的图片文件会导致渲染页面的时候需 ...
最新文章
- canvas绘制的文字如何换行
- [.Net跨平台]部署DTCMS到Jexus遇到的问题及解决思路---Linux环境搭建
- IE的box模型显示bug
- (四十三)java版spring cloud+spring boot+redis多租户社交电子商务平台-eureka集群整合config配置中心...
- centos 安装java_在CentOS8上编译安装开源EDA工具——Surelog
- 关于 Repeater 控件嵌套的使用。在嵌套中添加 其它控件(如:按钮),并影响其它控件的方法,很重要哦,测试通过。...
- 《jQuery基础》总结
- 现代程序设计 作业9 - 综合练习
- 云存储精华问答 | 如何选择云迁移策略?
- 控制台下修改系统驱动状态的代码
- 常用的方法论-PDCA
- crammd5 php,使用CRAMMD5的SMTP身份验证
- msi笔记本u盘装linux,微星笔记本bios设置u盘启动的步骤方法详细教程 - 系统家园...
- laravel 的用户认证
- 计算机组成原理6-20,计算机组成原理课后题6.20PPT课件
- android模拟器连接不到本地服务器
- zabbix短信网关使用了中国网建SMS短信通
- VS2019++QT5.12.10+PCL1.11.1+VTK8.2.0+opencv(camke3.18.0)环境搭配及演示实例
- 济南技校计算机专业学什么,技校计算机专业学什么-邹城
- Python -- 堆数据结构 heapq - I love this game! - 博客频道 - CSDN.NET