【问题标题】:Rounded rect Android that works the same for different image sizes圆角矩形 Android,适用于不同图像尺寸
【发布时间】:2017-09-25 18:54:37
【问题描述】:

我发现了很多关于 Android 中 ImageViews 的圆角矩形的帖子。

目前我正在使用这个解决方案:How to make an ImageView with rounded corners?

但是,对于各种位图大小,圆角会因源大小而有很大差异。我需要在整个应用程序中保持恒定的圆角,而不是取决于位图大小。

有人知道解决办法吗?

【问题讨论】:

    标签: android imageview


    【解决方案1】:

    这个RoundedImageView Library 为我工作。我只是将圆角半径设置为 dimens.xml 文件中的一个维度,然后使用

    <com.makeramen.RoundedImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/imageView1"
            android:src="@drawable/photo1"
            android:scaleType="centerCrop"
            app:corner_radius="@dimen/mycorner_radius_in_dp"
            app:border_width="2dip"
            app:border_color="#333333"
            app:round_background="true"
            app:is_oval="true" />
    

    它不关心图像大小

    【讨论】:

      【解决方案2】:

      制作一个用于创建圆形视图的 java 文件。

      public class CircularImageView extends ImageView {
      
      //you can change the radius to modify the circlur shape into oval or rounded rectangle
      
      public static float radius = 100.0f;
      
      public CircularImageView(Context context) {
      super(context);
      }
      
      public CircularImageView(Context context, AttributeSet attrs) {
      super(context, attrs);
      }
      
      public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      }
      
      @Override
      protected void onDraw(Canvas canvas) {
      Path clipPath = new Path();
      RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
      clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
      canvas.clipPath(clipPath);
      super.onDraw(canvas);
      }
      }
      

      之后在你的布局中使用这个 java 文件,如下所示:

      <com.yourpackagename.CircularImageView
      android:id="@+id/logo_ngo"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:background="@drawable/circle"
      android:src="@drawable/file_logo"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:contentDescription="@string/content_description"
      android:layout_margin="@dimen/medium_margin"
      android:scaleType="centerCrop"/>
      

      这里 file_logo 是图像文件,圆圈是文件名,您将在下面用作 xml:

      <?xml version="1.0" encoding="utf-8"?>
      <shape
      xmlns:android1="http://schemas.android.com/apk/res/android"
      android1:shape="oval">
      
      <stroke android1:width="1dp"
              android1:color="#000" >
      </stroke>
      
      <size
          android1:width="50dp"
          android1:height="50dp" >
      </size>
      
      <corners android1:radius="1dp" >
      </corners>
      
      </shape>
      

      这对我有用,如果有人有更好的解决方案,也请分享你的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多