代码之家  ›  专栏  ›  技术社区  ›  DolDurma

Android如何通过xml或编程绘制正多边形

  •  6
  • DolDurma  · 技术社区  · 8 年前

    有没有办法在Android xml布局上绘制多边形?

    enter image description here

    或者是否有helper类作为库来绘制它们?

    2 回复  |  直到 8 年前
        1
  •  6
  •   Edric Prince Bansal    7 年前

    我使用的是增强版 this Class

    参见上的工作示例 github ( https://github.com/hiteshsahu/Benzene )

    Demo app screenshot

    可提取类别

    import android.graphics.Canvas;
    import android.graphics.ColorFilter;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.Rect;
    import android.graphics.drawable.Drawable;
    
    /**
     * Originally Created by AnderWeb (Gustavo Claramunt) on 7/10/14.
     */
    public class PolygonalDrwable extends Drawable {
    
        private int numberOfSides = 3;
        private Path polygon = new Path();
        private Path temporal = new Path();
        private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    
        public PolygonalDrwable(int color, int sides) {
            paint.setColor(color);
            polygon.setFillType(Path.FillType.EVEN_ODD);
            this.numberOfSides = sides;
        }
    
        @Override
        public void draw(Canvas canvas) {
            canvas.drawPath(polygon, paint);
        }
    
        @Override
        public void setAlpha(int alpha) {
            paint.setAlpha(alpha);
        }
    
        @Override
        public void setColorFilter(ColorFilter cf) {
            paint.setColorFilter(cf);
        }
    
        @Override
        public int getOpacity() {
            return paint.getAlpha();
        }
    
        @Override
        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
            computeHex(bounds);
            invalidateSelf();
        }
    
        public void computeHex(Rect bounds) {
    
            final int width = bounds.width();
            final int height = bounds.height();
            final int size = Math.min(width, height);
            final int centerX = bounds.left + (width / 2);
            final int centerY = bounds.top + (height / 2);
    
            polygon.reset();
            polygon.addPath(createHexagon(size, centerX, centerY));
            polygon.addPath(createHexagon((int) (size * .8f), centerX, centerY));
        }
    
        private Path createHexagon(int size, int centerX, int centerY) {
            final float section = (float) (2.0 * Math.PI / numberOfSides);
            int radius = size / 2;
            Path polygonPath = temporal;
            polygonPath.reset();
            polygonPath.moveTo((centerX + radius * (float)Math.cos(0)), (centerY + radius
                    * (float)Math.sin(0)));
    
            for (int i = 1; i < numberOfSides; i++) {
                polygonPath.lineTo((centerX + radius * (float)Math.cos(section * i)),
                        (centerY + radius * (float)Math.sin(section * i)));
            }
    
            polygonPath.close();
            return polygonPath;
        }
    }
    

    将drawable设置为任何Imageview,如下所示

    //Triangle
    ((ImageView) findViewById(R.id.triangle))
                    .setBackgroundDrawable(new PolygonalDrwable(Color.GREEN, 3));
    
            //Square
            ((ImageView) findViewById(R.id.square))
                    .setBackgroundDrawable(new PolygonalDrwable(Color.MAGENTA, 4));
    
            //Pentagon
            ((ImageView) findViewById(R.id.pentagon))
                    .setBackgroundDrawable(new PolygonalDrwable(Color.LTGRAY, 5));
    
    
            //Hexagon
            ((ImageView) findViewById(R.id.hex))
                    .setBackgroundDrawable(new PolygonalDrwable(Color.RED, 6));
    
    
            //Heptagon
            ((ImageView) findViewById(R.id.hept))
                    .setBackgroundDrawable(new PolygonalDrwable(Color.MAGENTA, 7));
    
    
            //Octagon
            ((ImageView) findViewById(R.id.oct))
                    .setBackgroundDrawable(new PolygonalDrwable(Color.YELLOW, 8));
    
        2
  •  4
  •   karthik prasad    8 年前

    您可以创建自定义绘图和形状作为绘图资源。

    右键单击Android Studio中的“drawable”文件夹,然后选择新建->可绘制资源文件。

    Here 是一个不错的图形和笔划入门教程。

    Here 是一些可供使用的示例形状。

    Here 是一些更复杂的事情的文档,您可以使用drawable。