【问题标题】:Android How to draw a regular polygon via xml or programicalAndroid 如何通过 xml 或程序绘制正多边形
【发布时间】:2016-11-10 04:50:18
【问题描述】:

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

或者是否有任何帮助类作为库来绘制它们?

【问题讨论】:

    标签: android android-layout polygon


    【解决方案1】:

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

    在Android Studio中右键“drawable”文件夹,选择New->Drawable Resource File。

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

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

    Here 是有关您可以使用可绘制对象完成的一些更复杂事情的文档。

    【讨论】:

      【解决方案2】:

      我使用的是this Class的增强版

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

      像这样将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));
      

      【讨论】:

      • 谢谢,如何改变角度?例如从顶部看第三个形状PolygonalDrwable(Color.LTGRAY, 5)
      猜你喜欢
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 2012-12-10
      • 1970-01-01
      相关资源
      最近更新 更多