Android Shape 形状
来源:互联网 发布:如何申请网站域名 编辑:程序博客网 时间:2024/04/29 13:54
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 UI---自定义形状shape
- Android UI---自定义形状shape
- Android UI---自定义形状shape
- Shape形状
- Android UI---用xml自定义形状shape
- Android按钮shape形状资源代码实现
- Android中shape自定义形状遇到问题
- 【Shape】Shape –形状
- Shape 属性形状控件Shape
- Android wiki:10.shape自定形状图形
- Android中使用shape来实现控件形状的设置
- Android——使用Shape制作特殊形状的Button
- Android利用GradientDrawable定义动态的shape形状探索
- Android shape属性详解(自定义图片和形状)
- shape context (形状上下文)
- Shape Drawable 形状图片
- C++排序方法汇总
- boost库解析json例子(二)
- 无需翻墙, Android官方开发相关下载资源一揽子大汇总
- Linux rpm 命令参数使用详解[介绍和应用]
- 抽象数据类型的表示与实现
- Android Shape 形状
- 0x3f3f3f3f...编程中无穷大常量的设置技巧
- 收藏网站11
- Java基础--对象内存管理
- 浅谈一下关于android碎片化的问题
- 2012年5月SAT香港真题解析
- 线程池例子
- Sublime Text 全程指引
- 收藏网站12