Android如何通过xml或程序绘制常规多边形 [英] Android How to draw a regular polygon via xml or programical

查看:269
本文介绍了Android如何通过xml或程序绘制常规多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there any way to draw polygonal shapes on Android xml layouts?

还是有任何帮助程序类作为库来绘制它们?

or is any helper class as library to draw them?

推荐答案

我正在使用增强版本的此班级

GitHub 上查看工作示例( https://github.com/hiteshsahu/苯)

I am using enhanced version of this Class

See working sample on GitHub (https://github.com/hiteshsahu/Benzene)

可绘制类

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

设置可绘制到任何这样的Imageview

Set drawable to any Imageview like this

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

这篇关于Android如何通过xml或程序绘制常规多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆