【问题标题】:Android - ImageView with rounded only one cornerAndroid - ImageView 只有一个圆角
【发布时间】:2019-10-16 08:56:15
【问题描述】:

我想像这样创建 ImageView(图片右侧):

我在 CardView 布局中有它,所以我有卡片的圆角,但我需要单独(或与右上角)创建图像的圆角左下角。

我尝试了几个选项,但都无法正常工作。

我该怎么做?你有什么建议吗?

【问题讨论】:

  • imageview 包裹在cardView
  • 以编程方式将此ImageView 包裹在CardView 内,左下角圆角或圆角bitmap
  • CardView如何只设置一个圆角?
  • 可以分享代码吗?
  • 如果您使用CardView 包裹图像,但使用负的顶部和右侧边距以使您看不到那些圆角,该怎么办?

标签: android imageview android-imageview rounded-corners material-components-android


【解决方案1】:

您可以使用Material Components Library
1.2.0-alpha03 版本有新的 ShapeableImageView

只需在您的布局中使用:

  <com.google.android.material.imageview.ShapeableImageView
      app:srcCompat="@drawable/..."
      ../>

并在您的代码中应用 ShapeAppearanceModel

float radius = getResources().getDimension(R.dimen.default_corner_radius);
imageView.setShapeAppearanceModel(imageView.getShapeAppearanceModel()
    .toBuilder()
    .setTopRightCorner(CornerFamily.ROUNDED,radius)
    .setBottomLeftCorner(CornerFamily.ROUNDED,radius)
    .build());

【讨论】:

    【解决方案2】:

    您可以使用这个库并将您的 ImageView 放入布局中

    https://github.com/JcMinarro/RoundKornerLayouts

    并且可以像这样设置特定角的半径

    containerLayout.setCornerRadius(2f, CornerType.ALL);
    containerLayout.setCornerRadius(2f, CornerType.BOTTOM_LEFT);
    

    其他选择

    public final enum class CornerType private constructor() : kotlin.Enum<com.jcminarro.roundkornerlayout.CornerType> {
        ALL,
    
        TOP_LEFT,
    
        TOP_RIGHT,
    
        BOTTOM_RIGHT,
    
        BOTTOM_LEFT;
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      <androidx.cardview.widget.CardView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_margin="16dp"
          app:cardCornerRadius="20dp">
      
          <ImageView
              android:id="@+id/image_view"
              android:layout_width="150dp"
              android:layout_height="150dp"
              android:layout_gravity="top|right"
              android:scaleType="fitXY"
              android:src="@drawable/person" />
      </androidx.cardview.widget.CardView>
      

      在你的活动中:

       ImageView image = findViewById(R.id.image_view);
              Bitmap bitImg = BitmapFactory.decodeResource(getResources(),
                      R.drawable.person);
              image.setImageBitmap(createRoundedRectBitmap(bitImg, 0, 20, 0, 20));
          }
      
      
          private static Bitmap createRoundedRectBitmap(@NonNull Bitmap bitmap, float topLeftCorner, float topRightCorner, float bottomRightCorner,
                                                        float bottomLeftCorner) {
      
              Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
              Canvas canvas = new Canvas(output);
      
      
              final int color = Color.WHITE;
              final Paint paint = new Paint();
              final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
              final RectF rectF = new RectF(rect);
              Path path = new Path();
              float[] radii = new float[]{
                      topLeftCorner, bottomLeftCorner,
                      topRightCorner, topRightCorner,
                      bottomRightCorner, bottomRightCorner,
                      bottomLeftCorner, bottomLeftCorner
              };
      
              paint.setAntiAlias(true);
              canvas.drawARGB(0, 0, 0, 0);
              paint.setColor(color);
              path.addRoundRect(rectF, radii, Path.Direction.CW);
              canvas.drawPath(path, paint);
              paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
              canvas.drawBitmap(bitmap, rect, rect, paint);
              return output;
          }
      

      【讨论】:

        【解决方案4】:

        这很简单,只需制作可绘制的资源文件text_logo_fix.xml

        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="#fff"/>
            <corners android:bottomLeftRadius="30dp" android:topRightRadius="30dp"/>
        </shape>
        

        并在 ImageView 属性中设置这个可绘制文件android:background

        <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:background="@drawable/herenamedrawableresourcefile"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/text_logo_fix" />
        

        或者你可以看起来像这样

        <LinearLayout
                    android:padding="10dp"
                    android:layout_marginTop="20dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/herenamedrawableresourcefile">
                    <ImageView
                        android:id="@+id/imageView"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/text_logo_fix" />
                </LinearLayout>
        

        【讨论】:

          猜你喜欢
          • 2020-04-02
          • 1970-01-01
          • 2021-05-14
          • 2012-09-15
          • 2021-06-09
          • 2020-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多