android画图----ShapeDrawable和shader

xiaoxiao2021-03-01  5

在这个例程中主要讲述了ShapeDrawable,以及各种Gradient 首先初始化了 mDrawables = new ShapeDrawable[7];默认的是矩形, 当然还有其他形状: mDrawables[0] = new ShapeDrawable(new RectShape()); mDrawables[1] = new ShapeDrawable(new OvalShape()); mDrawables[4] = new ShapeDrawable(new RoundRectShape(outerR, inset, innerR)); mDrawables[5] = new ShapeDrawable(new PathShape(path, 100, 100)); float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 }; RectF inset = new RectF(20, 20, 20, 20); float[] innerR = new float[] { 12, 12, 0, 0, 12, 12, 0, 0 }; Path path = new Path(); path.moveTo(50, 0); path.lineTo(0, 50); path.lineTo(50, 100); path.lineTo(100, 50); path.close(); 实例化ShapeDrawable后就可以对他们进行设置: 设置渐变 mDrawables[0].getPaint().setShader(makeTiling()); 设置颜色 mDrawables[1].getPaint().setColor(0xFF00FF00); 设置影响 影响呢有很多 可以设置单个 也可以用组合 PathEffect pe = new DiscretePathEffect(10, 2); PathEffect pe2 = new CornerPathEffect(8); mDrawables[3].getPaint().setPathEffect( new SumPathEffect(pe2, pe)); 其中渐变又包括: private static Shader makeSweep() { return new SweepGradient(150, 25, new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFF0000 }, null); //return new SweepGradient(150, 25, new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF }, new float[]{0.9f,0.1f,0.9f}); } private static Shader makeLinear() { return new LinearGradient(0, 0, 50, 50, new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF }, null, Shader.TileMode.REPEAT); } private Shader makeTiling() { int[] pixels = new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0}; Bitmap bm = Bitmap.createBitmap(pixels, 2, 2, Bitmap.Config.ARGB_8888); // Bitmap bm = Bitmap.createBitmap(mBitmap, 60, 45, Bitmap.Config.ARGB_8888); return new BitmapShader(bm, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); } 最后在这个实例中 可以自定义一个ShapeDrawable private static class MyShapeDrawable extends ShapeDrawable { private Paint mStrokePaint = new Paint(Paint.LINEAR_TEXT_FLAG); public MyShapeDrawable(Shape s) { super(s); mStrokePaint.setStyle(Paint.Style.STROKE); mStrokePaint.setColor(Color.GREEN); } public Paint getStrokePaint() { return mStrokePaint; } @Override protected void onDraw(Shape s, Canvas c, Paint p) { //第一个调用父类的p,第二个调用自己刚才设置的p,如果你不想使用默认的父类,默认的颜色为黑了 s.draw(c, p); s.draw(c, mStrokePaint); } mDrawables[6] = new MyShapeDrawable(new ArcShape(45, 360)); MyShapeDrawable msd = (MyShapeDrawable)mDrawables[6]; msd.getStrokePaint().setStrokeWidth(1); }
转载请注明原文地址: https://www.6miu.com/read-3100231.html

最新回复(0)