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);

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="292" height="295" alt=""> 矩形

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);

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="297" height="299" alt=""> 无内矩形的圆角矩形  带内矩形的圆角矩形

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);

 扇形图

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="353" height="415" alt=""> 结合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 形状的更多相关文章

  1. Android shape自定义形状,设置渐变色

      <?xml version="1.0" encoding="utf-8"?> <!-- android:shape=["rect ...

  2. android:shape

    android:shape=["rectangle" | "oval" | "line" | "ring"] shape ...

  3. android shape详解

    shape--> shape属性: rectangle: 矩形,默认的形状,可以画出直角矩形.圆角矩形.弧形等 solid: 设置形状填充的颜色,只有android:color一个属性 andr ...

  4. android shape(如自定义Button)

    Shape 前言:有时候会去自己去画一些Button的样式来展现在UI当中,其中主要用到的就是Shape 先来看一段代码: <?xml version="1.0" encod ...

  5. Android 之形状Drawable

    形状Drawable资源允许使用 <shape>标记指定基本形状的尺寸.背景.轮廓线,从而定义这些基本形状. 每个形状都包含一个类型(通过shape属性指定).定义该形状尺寸的属性,以及指 ...

  6. android shape 大全 (转)

    1. 各属性的配置语法 在项目 res/drawable 文件夹中创建一个以 shape 为根节点的 XML 文件,基本语法如下: <?xml version="1.0" e ...

  7. android:shape属性详解

    这一类的shape定义在xml中 file location: res/drawable/filename.xml The filename is used as the resource ID.(这 ...

  8. Android——shape和selector和layer-list的(详细说明 转)

    <shape>和<selector>在Android UI设计中经常用到.比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到<shape> ...

  9. android shape.xml 属性详解

    转载源:http://blog.csdn.net/harvic880925/article/details/41850723 一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标 ...

随机推荐

  1. vim配置总结

    本博文转自:http://www.cppblog.com/runsisi/archive/2013/04/06/199152.html? opt=admin 12年的最后一天配置了一下公司RHEL上的 ...

  2. 通过javascript获取元素position

    function getOffset( el ) { var _x = 0; var _y = 0; while( el && !isNaN( el.offsetLeft ) & ...

  3. Nubia Z5S官方4.4 UI2.0音频Audio部分简单分析(也适用于其它8974/8064机型)以及降低破音出现几率的方法

    转载请注明出处和网址链接: http://blog.csdn.net/syhost/article/details/31419749 此篇本是在Z5S的官方4.4内測版出来时写的, 主要是看到其在au ...

  4. C#基础视频教程5.3 如何编写简单的超级热键

    跟前面一章讲解计算器一样,到最后一小节,我们总是要把代码规整好,让整个程序显得非常简洁,先做个文件夹把我们自定义的类库都放进去   然后我们开始整理Form1里面的代码,为了实现超级热键的功能,我们应 ...

  5. C#基础视频教程4.1 如何编写简单的计算器

    要学会基本的窗体控件(button,textbox,label这种,以及他们的重要属性),每个控件的命名要符合规范(例如一般文本框都会重命名txtXXXXX,按钮都会重命名btnXXXXX)   为了 ...

  6. PHP出现Notice警告怎么办

    如下所示,出现Notice警告 程序并没有大错,只是一些变量没有定义,你在代码的第一行加入这一句就可以了:error_reporting(E_ALL ^ E_NOTICE); 恢复正常了.

  7. 浅谈PHP数据结构之栈

    今天開始进阶自己的PHP,首先一切的编程语言都须要修炼自己的"内功",何为程序猿的"内功",我想大概就是数据结构和算法了吧 .毕竟是灵魂,是普通程序猿到高级程序 ...

  8. gsoap

    C++中如何使用gsoap开发WebService 1. 什么是gSOAPgSOAP是一个夸平台的,用于开发Web Service服务端和客户端的工具,在Windows.Linux.MAC OS和UN ...

  9. vue - 子路由-路由嵌套

    描述:子路由,也叫路由嵌套,采用在children后跟路由数组来实现,数组里和其他配置路由基本相同,需要配置path和component,然后在相应部分添加<router-view/>来展 ...

  10. vb 获取打印机名称

    Const HKLM = &H80000002 '定义根键常数 '其他常用根键 Const HKCR = &H80000000 , Const HKCU = &H8000000 ...