2016-10-26 25 views
0

如何建立这个视图如果...我如何构建这个customView?

每个按钮都是一个imageView,如果每个按钮都是一个带有透明度的正方形,可以对齐图像吗? 。问题是图像是一个“正方形”,不可能将图像对齐到任何想法?

我需要知道的是我如何对齐按钮,即 覆盖。

存在其他选择吗?

+0

是按钮的数量和位置动态或永远是这样呢? –

+0

给我的应用程序链接... –

+0

只是一个图像,不是一个apk –

回答

0

enter image description here

<View 
    android:id="@+id/uno" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_centerHorizontal="true" 
    android:background="@drawable/hexa"></View> 

<LinearLayout 
    android:id="@+id/layout_dos" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/uno" 
    android:layout_centerHorizontal="true" 
    android:gravity="center"> 

    <View 
     android:id="@+id/dos" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginRight="10dp" 
     android:background="@drawable/hexa"></View> 

    <View 
     android:id="@+id/tres" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:background="@drawable/hexa"></View> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/layout_dos" 
    android:layout_centerHorizontal="true"> 

    <View 
     android:id="@+id/cuatro" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginRight="10dp" 
     android:background="@drawable/hexa"></View> 

    <View 
     android:id="@+id/cinco" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginRight="10dp" 
     android:background="@drawable/hexa"></View> 

    <View 
     android:id="@+id/seis" 
     android:layout_width="50dp" 
     android:layout_height="50dp"></View> 

</LinearLayout> 

跟上这个代码玩!祝你好运。

+0

您的意见的结果是不一样的,我需要你的示例代码很容易创建... –

+0

只是旋转六角...(?) – elmontoya7

+0

我不能做你的工作的人 – elmontoya7

2

要将方形图像更改为六边形图像视图,您只需自定义图像视图。

定制HexagonImageView类:

public class HexagonImageView extends ImageView { 

    public HexagonImageView(Context context) { 
     super(context); 
    } 

    public HexagonImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public HexagonImageView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     Drawable drawable = getDrawable(); 
     if (drawable == null || getWidth() == 0 || getHeight() == 0) { 
      return; 
     } 

     Bitmap b = ((BitmapDrawable) drawable).getBitmap(); 
     Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); 

     int width = getWidth(), height = getHeight(); 
     int radius = width < height ? width : height; 
     Log.i("HexagonImage", "width : " + width + " " + "Height : " + height + " " + "Radius : " + radius); 

     Bitmap roundBitmap = getCroppedBitmap(bitmap, radius, width, height); 
     canvas.drawBitmap(roundBitmap, 0, 0, null); 
    } 

    public static Bitmap getCroppedBitmap(Bitmap bmp, int radius, int width, int height) { 
     Bitmap sbmp; 
     if (bmp.getWidth() != radius || bmp.getHeight() != radius) { 
      float _w_rate = 1.0f * radius/bmp.getWidth(); 
      float _h_rate = 1.0f * radius/bmp.getHeight(); 
      float _rate = _w_rate < _h_rate ? _h_rate : _w_rate; 
      sbmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() * _rate), (int)(bmp.getHeight() * _rate), false); 
     } 
     else 
      sbmp = bmp; 

     Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), 
       Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final Rect rect = new Rect(0, 0, width, width); 
     final int offset = (int) (width/(double) 2 * Math.tan(30 * Math.PI/(double) 180)); // (width/2) * tan(30deg) 
     Log.i("HexagonImage", "Offset : " + offset); 
     final int length = width - (2 * offset); 
     Log.i("HexagonImage", "Lenght : " + length); 

     final Path path = new Path(); 
     path.moveTo(width/2, 0); // top 
     path.lineTo(0, offset); // left top 
     path.lineTo(0, offset + length); // left bottom 
     path.lineTo(width/2, width); // bottom 
     path.lineTo(width, offset + length); // right bottom 
     path.lineTo(width, offset); // right top 
     path.close(); //back to top 

     Paint paint = new Paint(); 
     paint.setStrokeWidth(4); 
     canvas.drawARGB(0, 0, 0, 0); 
     canvas.drawPath(path, paint); 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
     canvas.drawBitmap(sbmp, rect, rect, paint); 

     paint.setColor(Color.parseColor("#BAB399")); 
     paint.setColor(Color.parseColor("white")); 
     paint.setStrokeWidth(4); 
     paint.setDither(true); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeCap(Paint.Cap.ROUND); 
     paint.setPathEffect(new CornerPathEffect(10)); 
     return output; 
    } 

} 

在XML添加以下内容:

<PackageName.HexagonImageView 
     android:id="@+id/iv_hexagon" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginTop="16dp" 
     android:src="@drawable/logo" 
     android:adjustViewBounds="true" /> 
+0

这只是为了创建te六边形,但问题是如何将第一个六边形叠加在接下来的两个六边形上。 –