【问题标题】:How to set image in custom rounded corner image view in android如何在android中的自定义圆角图像视图中设置图像
【发布时间】:2016-06-30 12:10:07
【问题描述】:

您好,我正在使用 android 相机应用程序,我想在圆角图像视图中显示单击的图像,我该如何实现这些。我发布了下面给出的图像。请帮助我?

Imageview 是这样的

【问题讨论】:

    标签: android android-layout uiimageview android-camera


    【解决方案1】:

    使用下面的自定义 ImageView 类。

    public class RoundedImageView extends ImageView {
        private Path mMaskPath;
        private Paint mMaskPaint    = new Paint(Paint.ANTI_ALIAS_FLAG);
        private int mCornerRadius   = 10;
    
        public RoundedImageView(Context context) {
            super(context);
    
            init(context);
        }
    
        public RoundedImageView(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
    
            init(context);
        }
    
        public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
            init(context);
        }
    
        private void init(Context context) {
            ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, null);
            mMaskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            mMaskPaint.setColor(context.getResources().getColor(R.color.transparent));
    
            mCornerRadius = (int) context.getResources().getDimension(R.dimen.image_border_curvature);
        }
    
        /**
         * Set the corner radius to use for the RoundedRectangle.
         */
        public void setCornerRadius(int cornerRadius) {
            mCornerRadius = cornerRadius;
            generateMaskPath(getWidth(), getHeight());
            invalidate();
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldW, int oldH) {
            super.onSizeChanged(w, h, oldW, oldH);
    
            if (w != oldW || h != oldH) {
                generateMaskPath(w, h);
            }
        }
    
        private void generateMaskPath(int w, int h) {
            mMaskPath = new Path();
            mMaskPath.addRoundRect(new RectF(0,0,w,h), mCornerRadius, mCornerRadius, Path.Direction.CW);
            mMaskPath.setFillType(Path.FillType.INVERSE_WINDING);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            if(canvas.isOpaque()) { // If canvas is opaque, make it transparent
                canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), 255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
            }
    
            super.onDraw(canvas);
    
            if(mMaskPath != null) {
                canvas.drawPath(mMaskPath, mMaskPaint);
            }
        }
    }
    

    在你的 xml 中使用这个 ImageView。

    【讨论】:

    • 谢谢 Febi 我如何在 xml 中使用你能帮我吗
    • 以及如何设置您在代码中提到的维度
    • 您可以提供 wrap_content 作为高度和宽度,或者提供一个 dp 值。例如; android:layout_width="60dp"
    • (R.dimen.image_border_curvature) 什么是 image_border_curvature
    【解决方案2】:

    我使用这个库https://github.com/vinc3m1/RoundedImageView 两年多了,我很高兴。你只需要在你的build.gradle中添加这行代码

    dependencies {
        compile 'com.makeramen:roundedimageview:2.2.1'
    }
    

    并像这样定义您的RoundedImageView 小部件

    <com.makeramen.roundedimageview.RoundedImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/imageView1"
            android:src="@drawable/photo1"
            android:scaleType="fitCenter"
            app:riv_corner_radius="30dip"
            app:riv_border_width="2dip"
            app:riv_border_color="#333333"
            app:riv_mutate_background="true"
            app:riv_tile_mode="repeat"
            app:riv_oval="true" />
    

    你准备好了!

    希望能提供帮助!

    【讨论】:

      【解决方案3】:

      您可以将 ImageView 包装在 CardView 中。您可以使用 app:cardCornerRadius 设置拐角半径。就这么简单

      <android.support.v7.widget.CardView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              app:cardCornerRadius="5dp"
              app:cardElevation="0dp">
          <ImageView
              android:id="@+id/imgUserHome"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/photo"/>
       </android.support.v7.widget.CardView>
      

      【讨论】:

      • 我正在使用这项工作,一切似乎都很好,哈哈
      【解决方案4】:

      与其自己尝试,不如使用this 之类的库来满足您的目的。它有不同的形状,效果很好。

      依赖于应用程序模块的build.gradle 添加此行:

      compile 'com.github.siyamed:android-shape-imageview:0.9.+@aar'
      

      同步 gradle,现在您的应用可以访问该库。现在使用以下代码将其添加到您的布局中:

      <com.github.siyamed.shapeimageview.BubbleImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:src="@drawable/neo"
      app:siArrowPosition="right"
      app:siSquare="true"/>
      

      关注库页面以获得更多自定义。

      【讨论】:

      • 但我不知道如何导入和使用库
      • 好的,我在我的 gradle 中编译它,但它在 gradle 包装文件中显示错误
      • Error:(8, 0) Gradle DSL method not found: 'compile()' 可能的原因:
      【解决方案5】:

      请在gradle中添加如下依赖并在xml中使用如下

      编译'de.hdodenhof:circleimageview:1.3.0'

      <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
                  android:id="@+id/profile_image"
                  android:layout_width="@dimen/pad_120dp"
                  android:layout_height="@dimen/pad_120dp"
                  android:layout_centerHorizontal="true"
                  android:layout_centerVertical="true"
                  android:src="@drawable/icon_profile" />
      

      【讨论】:

        猜你喜欢
        • 2020-10-09
        • 2016-10-14
        • 1970-01-01
        • 2021-03-23
        • 1970-01-01
        • 1970-01-01
        • 2015-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多