【问题标题】:make an ImageView with rounded corners?制作一个带圆角的 ImageView?
【发布时间】:2016-06-23 03:56:14
【问题描述】:

cardview的圆角 我想制作如上图所示的界面。图像不是固定的,这意味着我可以将图像传输给其他人。我想在 xml 文件中使用 pakage。类似的,

<com.example.widgets.RoundedImageView
    android: layout_width = "39dp"
    android: layout_height = "39dp"
    android: src = "@ drawable / your_drawable" />

【问题讨论】:

  • 使用安卓CardView
  • 是的,同意@Sanoop,您可以使用卡片视图来创建此布局

标签: android xml android-layout user-interface rounding


【解决方案1】:

你可以使用这个组件。 https://github.com/pungrue26/SelectableRoundedImageView

       <com.utility.SelectableRoundedImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:sriv_left_top_corner_radius="55dp"
            app:sriv_right_top_corner_radius="0dp"
            android:scaleType="fitXY"
           />

【讨论】:

    【解决方案2】:

    如果您不想使用任何第三方库,那么您可以自定义您的 ImageView。

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.RectF;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    
    public class RoundedImageView extends android.support.v7.widget.AppCompatImageView {
    private Paint mPaint;
    private Path mPath;
    private Bitmap mBitmap;
    private Matrix mMatrix;
    private int mRadius = convertDpToPixel(10);
    private int mWidth;
    private int mHeight;
    private Drawable mDrawable;
    
     public RoundedImageView(Context context) {
        super(context);
        init();
    }
    
    public RoundedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public RoundedImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
    
        mPath = new Path();
    }
    
    public int convertDpToPixel(int dp) {
        DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics);
    }
    
    @Override
    public void setImageDrawable(Drawable drawable) {
        mDrawable = drawable;
        if (drawable == null) {
            return;
        }
        mBitmap = drawableToBitmap(drawable);
        int bDIWidth = mBitmap.getWidth();
        int bDIHeight = mBitmap.getHeight();
        //Fit to screen.
        float scale;
        if ((mHeight / (float) bDIHeight) >= (mWidth / (float) bDIWidth)) {
            scale = mHeight / (float) bDIHeight;
        } else {
            scale = mWidth / (float) bDIWidth;
        }
        float borderLeft = (mWidth - (bDIWidth * scale)) / 2;
        float borderTop = (mHeight - (bDIHeight * scale)) / 2;
        mMatrix = getImageMatrix();
        RectF drawableRect = new RectF(0, 0, bDIWidth, bDIHeight);
        RectF viewRect = new RectF(borderLeft, borderTop, (bDIWidth * scale) + borderLeft, (bDIHeight * scale) + borderTop);
        mMatrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
        invalidate();
    }
    
    private Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap;
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if (bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }
        if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        if ((mDrawable != null) && (mHeight > 0) && (mWidth > 0)) {
            setImageDrawable(mDrawable);
        }
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mBitmap == null) {
            return;
        }
        canvas.drawColor(Color.TRANSPARENT);
        mPath.reset();
        mPath.moveTo(0, mRadius);
        mPath.lineTo(0, canvas.getHeight());
        mPath.lineTo(canvas.getWidth(), canvas.getHeight());
        mPath.lineTo(canvas.getWidth(), mRadius);
        mPath.quadTo(canvas.getWidth(), 0, canvas.getWidth() - mRadius, 0);
        mPath.lineTo(mRadius, 0);
        mPath.quadTo(0, 0, 0, mRadius);
        canvas.drawPath(mPath, mPaint);
        canvas.clipPath(mPath);
        canvas.drawBitmap(mBitmap, mMatrix, mPaint);
    }
    }
    

    在 layout.xml 中

    <com.example.widget.RoundedImageViewmageView
                android:id="@+id/ivProductImg"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:scaleType="fitXY"
                />
    

    【讨论】:

    • 它有效,但它设置了左上角和右上角的半径!
    【解决方案3】:

    将 imageView 放入 CardView 并为 CardView 设置 CornerRadius

    I already answered in the link below :
    

    Set Round Corner image in ImageView

    【讨论】:

      【解决方案4】:

      你的可绘制代码应该是这样的..

      <?xml version="1.0" encoding="utf-8"?>
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
                 android:shape="rectangle">
              <!-- <solid android:color="add your color here"/>-->
              <solid android:color="#00000"/> 
              <stroke
                  android:color="add your color here"
                  android:width="1dp"/>
              <corners
                  android:radius="5dp"/>
      
      
          </shape>
      

      在 XML 中

        <com.example.widgets.RoundedImageView
                      android: layout_width = "39dp"
                      android: layout_height = "39dp"
                      android:background="here is your background source"
                      android: src = "@ drawable / your_drawable"
            />
      

      【讨论】:

        猜你喜欢
        • 2011-01-28
        • 1970-01-01
        • 1970-01-01
        • 2020-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-16
        相关资源
        最近更新 更多