【问题标题】:Android Create ImageView with 3 rounded and 1 straight cornerAndroid 创建具有 3 个圆角和 1 个直角的 ImageView
【发布时间】:2019-06-05 11:52:12
【问题描述】:

我想创建一个带有 3 个圆角和一个直角的图像视图。

设置背景可绘制不起作用,我确实找到了一些 java 代码,但它只圆了 2 个角或 4 个而不是 3 个

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

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), 
bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new 
PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    那里有编写良好的库,因此您可以将图像剪辑到您想要的任何路径,但这里是代码:

    private Bitmap clip(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
    
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
    
    
        // creating a closing path with 3 rounded corners
        Path path = new Path();
        float radius = 48;
        float diameter = radius * 2;
        float width = bitmap.getWidth();
        float height = bitmap.getHeight();
        path.addArc(0, 0, diameter, diameter, 180, 90);
        path.lineTo(width - radius, 0);
        path.arcTo(width - diameter, 0, width, diameter, 270, 90, false);
        path.lineTo(width, height);
        path.lineTo(radius, height);
        path.arcTo(0, height - diameter, diameter, height, 90, 90, false);
        path.close();
    
    
        paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        canvas.drawPath(path, paint);
    
        return output;
    
    }
    

    【讨论】:

      【解决方案2】:

      在可绘制文件中创建新的可绘制资源文件并将背景设置为可绘制文件。

      <shape xmlns:android="http://schemas.android.com/apk/res/android">
      
      <!-- set the transparent background to image view -->
          <solid android:color="#00000000" />
      
      <!-- Replace the corner value with suitable value -->
          <corners android:bottomLeftRadius="0dp"
              android:bottomRightRadius="0dp"
              android:topLeftRadius="0dp"
              android:topRightRadius="0dp" />
      </shape>
      
      

      并在 xml 文件中应用适当的 scaletype 属性。

      1. CENTER – 图像居中但不缩放图像
      2. CENTER_CROP – 均匀缩放图像
      3. CENTER_INSIDE – 在容器内居中图像
      4. FIT_CENTER– 从中心缩放图像
      5. FIT_END – 从容器末端缩放图像。
      6. FIT_START – 从容器开始缩放图像
      7. FIT_XY - 从容器的 x 和 y 坐标填充图像
      8. MATRIX – 绘图时使用图像矩阵进行缩放

      【讨论】:

        【解决方案3】:

        在 Drawable @background.xml 文件中使用以下代码。里面

         <corners android:topLeftRadius="6dp" 
              android:topRightRadius="6dp"
              android:bottomLeftRadius="6dp" 
              android:bottomRightRadius="0dp"/>
        

        然后用作 Imageview 的背景

        【讨论】:

          猜你喜欢
          • 2021-11-22
          • 1970-01-01
          • 1970-01-01
          • 2020-04-02
          • 1970-01-01
          • 2012-09-15
          • 1970-01-01
          • 2021-06-09
          • 2014-09-13
          相关资源
          最近更新 更多