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
[java] view plain copy print ?
- RectShaperectShape=newRectShape();
- ShapeDrawabledrawable=newShapeDrawable(rectShape);
- drawable.getPaint().setColor(Color.RED);
- drawable.getPaint().setStyle(Paint.Style.FILL);//填充
- view.setBackgroundDrawable(drawable);
矩形
RoundRectShape
[java] view plain copy print ?
- float[]outerRadii={20,20,40,40,60,60,80,80};//外矩形左上、右上、右下、左下圆角半径
- //float[]outerRadii={20,20,20,20,20,20,20,20};//外矩形左上、右上、右下、左下圆角半径
- RectFinset=newRectF(100,100,200,200);//内矩形距外矩形,左上角x,y距离,右下角x,y距离
- float[]innerRadii={20,20,20,20,20,20,20,20};//内矩形圆角半径
- //RoundRectShaperoundRectShape=newRoundRectShape(outerRadii,inset,innerRadii);
- RoundRectShaperoundRectShape=newRoundRectShape(outerRadii,null,innerRadii);//无内矩形
- ShapeDrawabledrawable=newShapeDrawable(roundRectShape);
- drawable.getPaint().setColor(Color.MAGENTA);
- drawable.getPaint().setAntiAlias(true);
- drawable.getPaint().setStyle(Paint.Style.STROKE);//描边
- view.setBackground(drawable);
无内矩形的圆角矩形带内矩形的圆角矩形
OvalShape
[java] view plain copy print ?
- OvalShapeovalShape=newOvalShape();
- ShapeDrawabledrawable=newShapeDrawable(ovalShape);
- drawable.getPaint().setColor(Color.RED);
- drawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
- view.setBackgroundDrawable(drawable);
椭圆。 而当View的宽高相等时,就绘出了圆
ArcShape
[java] view plain copy print ?
- ArcShapearcShape=newArcShape(45,270);//顺时针开始角度45,扫描的角度270扇形
- ShapeDrawabledrawable=newShapeDrawable(arcShape);
- drawable.getPaint().setColor(Color.RED);
- drawable.getPaint().setStyle(Paint.Style.FILL);
- //Bitmapbitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.aa)).getBitmap();
- //BitmapShaderbitmapShader=newBitmapShader(bitmap,Shader.TileMode.MIRROR,Shader
- //.TileMode.REPEAT);
- //Matrixmatrix=newMatrix();
- //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
[java] view plain copy print ?
- Pathpath=newPath();
- path.moveTo(50,0);
- path.lineTo(0,50);
- path.lineTo(50,100);
- path.lineTo(100,50);
- path.lineTo(50,0);
- PathShapepathShape=newPathShape(path,200,100);
- ShapeDrawabledrawable=newShapeDrawable(pathShape);
- drawable.getPaint().setColor(Color.RED);
- drawable.getPaint().setStyle(Paint.Style.FILL);
- imageView.setBackgroundDrawable(drawable);
以Path路径对象,来设定图形。
PathShape的构造函数:PathShape(path, stdWidth, stdHeight);
stdWidth:标准宽度
stdHeight:标准高度
在构造PathShape对象时,设置了宽高的标准。内部函数
[java] view plain copy print ?
- protectedvoidonResize(floatwidth,floatheight){
- mScaleX=width/mStdWidth;
- mScaleY=height/mStdHeight;
- }
- publicvoiddraw(Canvascanvas,Paintpaint){
- canvas.save();
- canvas.scale(mScaleX,mScaleY);
- canvas.drawPath(mPath,paint);
- canvas.restore();
- }
有了设定的标准宽高,再算出实际宽高与标准宽高的比率,最后在绘制时,画布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
更多相关文章
- 图片下载工具类,可更新UI
- 仿抖音视频详情页的红心点赞动效
- Webview中所调网页的缩放
- WebView的使用(基本篇)
- 使用Vitamio打造自己的Android万能播放器(2)—— 手势控制亮度、音
- Android(安卓)图片缩放 BitmapFactory详解
- android中自定义WebView显示网页或本地html文件
- android手写签名遇见bitmap黑屏和本地html插入签名图片
- Android(安卓)GestureDetector ScaleGestureDetector