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

Android Shape 形状_第1张图片 矩形


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

Android Shape 形状_第2张图片 无内矩形的圆角矩形带内矩形的圆角矩形


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

Android Shape 形状_第3张图片 椭圆。 而当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);

Android Shape 形状_第4张图片 扇形图

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

Android Shape 形状_第5张图片 stdx=400, stdy=400


PathShape pathShape = new PathShape(path, 100, 100);

stdx=100, stdy=100


PathShape pathShape = new PathShape(path, 200, 100);

Android Shape 形状_第6张图片stdx=200, stdy=100




更多相关文章

  1. android 一些路径的标准写法
  2. Android内存缓存图片的标准方法
  3. android蓝牙开发入门到精通4------通信标准
  4. Android——使用Shape制作特殊形状的Button
  5. Android RSA 与标准Java RSA实现区别
  6. canvas.drawRoundRect方法,绘制圆角矩形
  7. a64_7.1-v3.1android拉伸绘制矩形区域距离默认平板外边界问题
  8. JKS密匙库专用格式,建议使用“keytool -importkeystore -srckeyst
  9. 【Android】Web开发之通过标准Java接口处理Http请求

随机推荐

  1. Android开发资料视频教程(光盘6.2G)
  2. 【转】 Android转屏时禁止调用onCreate
  3. Android app内部下载安装
  4. android init 进程分析
  5. Android(安卓)TabActivity的嵌套
  6. 在低版本Android上使用Material Design
  7. Android环境SDK搭建问题
  8. Android简易计算器(破烂Alpha版,后续更新)
  9. Android中的动画研究(二)
  10. Android 汉字转拼音之工具篇