【问题标题】:Android Circular Network Image View showing different sizes显示不同尺寸的 Android 循环网络图像视图
【发布时间】:2016-07-24 19:54:12
【问题描述】:

这里是实现CircularNetworkImageView的代码。我得到了我想要的,但这里的问题是圆形图像的大小不固定。有些要么小,要么大。并且根据大小,图像有点像在视图布局中的不同位置上跳跃。我已经在xml上设置了固定的宽度和高度,但问题仍然存在。请帮我解决这个问题。

    public class CircularNetworkImageView extends NetworkImageView {
    Context mContext;

    public CircularNetworkImageView(Context context) {
        super(context);
        mContext = context;
    }

    public CircularNetworkImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        mContext = context;
    }

    public CircularNetworkImageView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        if(bm==null) return;
        setImageDrawable(new BitmapDrawable(mContext.getResources(),
                getCircularBitmap(bm)));
    }

    /**
     * Creates a circular bitmap and uses whichever dimension is smaller to determine the width
     * <br/>Also constrains the circle to the leftmost part of the image
     * 
     * @param bitmap
     * @return bitmap
     */
    public Bitmap getCircularBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        int width = bitmap.getWidth();
        if(bitmap.getWidth()>bitmap.getHeight())
            width = bitmap.getHeight();
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, width, width);
        final RectF rectF = new RectF(rect);
        final float roundPx = width / 2;

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

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

        return output;
    }

}

我在此链接上获得了此代码:CircularNetworkImageView

【问题讨论】:

    标签: android imageview android-volley networkimageview


    【解决方案1】:

    我之前也遇到过这个问题,幸运的是我找到了这个问题。试试这个代码:

        public class CircleImageView extends NetworkImageView {
    
        private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;
    
        private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
        private static final int COLORDRAWABLE_DIMENSION = 2;
    
        private static final int DEFAULT_BORDER_WIDTH = 0;
        private static final int DEFAULT_BORDER_COLOR = Color.BLACK;
        private static final boolean DEFAULT_BORDER_OVERLAY = false;
    
        private final RectF mDrawableRect = new RectF();
        private final RectF mBorderRect = new RectF();
    
        private final Matrix mShaderMatrix = new Matrix();
        private final Paint mBitmapPaint = new Paint();
        private final Paint mBorderPaint = new Paint();
    
        private int mBorderColor = DEFAULT_BORDER_COLOR;
        private int mBorderWidth = DEFAULT_BORDER_WIDTH;
    
        private Bitmap mBitmap;
        private BitmapShader mBitmapShader;
        private int mBitmapWidth;
        private int mBitmapHeight;
    
        private float mDrawableRadius;
        private float mBorderRadius;
    
        private ColorFilter mColorFilter;
    
        private boolean mReady;
        private boolean mSetupPending;
        private boolean mBorderOverlay;
    
        public CircleImageView(Context context) {
            super(context);
    
            init();
        }
    
        public CircleImageView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0);
    
            mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_civ_border_width, DEFAULT_BORDER_WIDTH);
            mBorderColor = a.getColor(R.styleable.CircleImageView_civ_border_color, DEFAULT_BORDER_COLOR);
            mBorderOverlay = a.getBoolean(R.styleable.CircleImageView_civ_border_overlay, DEFAULT_BORDER_OVERLAY);
    
            a.recycle();
    
            init();
        }
    
        private void init() {
            super.setScaleType(SCALE_TYPE);
            mReady = true;
    
            if (mSetupPending) {
                setup();
                mSetupPending = false;
            }
        }
    
        @Override
        public ScaleType getScaleType() {
            return SCALE_TYPE;
        }
    
        @Override
        public void setScaleType(ScaleType scaleType) {
            if (scaleType != SCALE_TYPE) {
                throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));
            }
        }
    
        @Override
        public void setAdjustViewBounds(boolean adjustViewBounds) {
            if (adjustViewBounds) {
                throw new IllegalArgumentException("adjustViewBounds not supported.");
            }
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            if (getDrawable() == null) {
                return;
            }
    
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);
            if (mBorderWidth != 0) {
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint);
            }
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            setup();
        }
    
        public int getBorderColor() {
            return mBorderColor;
        }
    
        public void setBorderColor(int borderColor) {
            if (borderColor == mBorderColor) {
                return;
            }
    
            mBorderColor = borderColor;
            mBorderPaint.setColor(mBorderColor);
            invalidate();
        }
    
        public void setBorderColorResource(@ColorRes int borderColorRes) {
            setBorderColor(getContext().getResources().getColor(borderColorRes));
        }
    
        public int getBorderWidth() {
            return mBorderWidth;
        }
    
        public void setBorderWidth(int borderWidth) {
            if (borderWidth == mBorderWidth) {
                return;
            }
    
            mBorderWidth = borderWidth;
            setup();
        }
    
        public boolean isBorderOverlay() {
            return mBorderOverlay;
        }
    
        public void setBorderOverlay(boolean borderOverlay) {
            if (borderOverlay == mBorderOverlay) {
                return;
            }
    
            mBorderOverlay = borderOverlay;
            setup();
        }
    
        @Override
        public void setImageBitmap(Bitmap bm) {
            super.setImageBitmap(bm);
            mBitmap = bm;
            setup();
        }
    
        @Override
        public void setImageDrawable(Drawable drawable) {
            super.setImageDrawable(drawable);
            mBitmap = getBitmapFromDrawable(drawable);
            setup();
        }
    
        @Override
        public void setImageResource(@DrawableRes int resId) {
            super.setImageResource(resId);
            mBitmap = getBitmapFromDrawable(getDrawable());
            setup();
        }
    
        @Override
        public void setImageURI(Uri uri) {
            super.setImageURI(uri);
            mBitmap = getBitmapFromDrawable(getDrawable());
            setup();
        }
    
        @Override
        public void setColorFilter(ColorFilter cf) {
            if (cf == mColorFilter) {
                return;
            }
    
            mColorFilter = cf;
            mBitmapPaint.setColorFilter(mColorFilter);
            invalidate();
        }
    
        private Bitmap getBitmapFromDrawable(Drawable drawable) {
            if (drawable == null) {
                return null;
            }
    
            if (drawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) drawable).getBitmap();
            }
    
            try {
                Bitmap bitmap;
    
                if (drawable instanceof ColorDrawable) {
                    bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
                } else {
                    bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
                }
    
                Canvas canvas = new Canvas(bitmap);
                drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
                drawable.draw(canvas);
                return bitmap;
            } catch (OutOfMemoryError e) {
                return null;
            }
        }
    
        private void setup() {
            if (!mReady) {
                mSetupPending = true;
                return;
            }
    
            if (mBitmap == null) {
                return;
            }
    
            mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    
            mBitmapPaint.setAntiAlias(true);
            mBitmapPaint.setShader(mBitmapShader);
    
            mBorderPaint.setStyle(Paint.Style.STROKE);
            mBorderPaint.setAntiAlias(true);
            mBorderPaint.setColor(mBorderColor);
            mBorderPaint.setStrokeWidth(mBorderWidth);
    
            mBitmapHeight = mBitmap.getHeight();
            mBitmapWidth = mBitmap.getWidth();
    
            mBorderRect.set(0, 0, getWidth(), getHeight());
            mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2);
    
            mDrawableRect.set(mBorderRect);
            if (!mBorderOverlay) {
                mDrawableRect.inset(mBorderWidth, mBorderWidth);
            }
            mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2);
    
            updateShaderMatrix();
            invalidate();
        }
    
        private void updateShaderMatrix() {
            float scale;
            float dx = 0;
            float dy = 0;
    
            mShaderMatrix.set(null);
    
            if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
                scale = mDrawableRect.height() / (float) mBitmapHeight;
                dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
            } else {
                scale = mDrawableRect.width() / (float) mBitmapWidth;
                dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
            }
    
            mShaderMatrix.setScale(scale, scale);
            mShaderMatrix.postTranslate((int) (dx + 0.5f) + mDrawableRect.left, (int) (dy + 0.5f) + mDrawableRect.top);
    
            mBitmapShader.setLocalMatrix(mShaderMatrix);
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      使用这个:

      public class CircularImageView extends ImageView{
      
      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) {
      
          Drawable drawable = getDrawable();
      
          if (drawable == null) {
              return;
          }
      
          if (getWidth() == 0 || getHeight() == 0) {
              return;
          }
          Bitmap b = ((BitmapDrawable) drawable).getBitmap();
          if(b != null) {
              Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
      
              int w = getWidth(), h = getHeight();
      
              Bitmap roundBitmap = getRoundBitmap(bitmap, w);
              canvas.drawBitmap(roundBitmap, 0, 0, null);
          }
      }
      
      public static Bitmap getRoundBitmap(Bitmap bmp, int radius) {
          Bitmap sBmp;
      
          if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
              float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
              float factor = smallest / radius;
              sBmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() / factor), (int)(bmp.getHeight() / factor), false);
          } else {
              sBmp= bmp;
          }
      
          Bitmap output = Bitmap.createBitmap(radius, radius,
                  Bitmap.Config.ARGB_8888);
      
      
          Canvas canvas = new Canvas(output);
      
          final int color = 0xffa19774;
          final Paint paint = new Paint();
          final Rect rect = new Rect(0, 0, radius, radius);
      
          paint.setAntiAlias(true);
          paint.setFilterBitmap(true);
          paint.setDither(true);
          canvas.drawARGB(0, 0, 0, 0);
          paint.setColor(Color.parseColor("#BAB399"));
          canvas.drawCircle(radius / 2 + 0.7f,
                  radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
          paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
          canvas.drawBitmap(sBmp, rect, rect, paint);
      
          return output;
      }
      
      }
      

      【讨论】:

        【解决方案3】:

        原始的“CircularNetworkImageView.java”类没有很好地组织在recyclerview 或listview 中使用。它为每个图像计算不同的大小。我已经编辑了原始类的“getCircularBitmap”方法,以便在recyclerview,listview中获得相同大小和相同位置的圆形图像视图。

        Here is the edited class link(GitHub)

        【讨论】:

          【解决方案4】:

          您可以使用带有Circular imageview 的毕加索库来显示来自 url 的图像

          将这2个文件添加到gradle

          compile 'com.squareup.picasso:picasso:2.5.2'
          compile 'de.hdodenhof:circleimageview:2.1.0'
          

          然后将此行添加到xml中

          <de.hdodenhof.circleimageview.CircleImageView
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/profile_image"
              android:layout_width="96dp"
              android:layout_height="96dp"
              android:src="@drawable/profile"
              app:civ_border_width="2dp"
              app:civ_border_color="#FF000000"/>
          

          最后在onCreate中添加这些行

              CircleImageView image = (CircleImageView) findViewById(R.id.profile_image);
              Picasso.with(this).load("image url here")).noFade().into(image);
          

          就是这样..现在您可以在圆形图像视图中从在线查看图像

          【讨论】:

            【解决方案5】:

            我通常会设置一个切出圆形的图像作为前景。我使图像的其余部分与它所在视图的背景颜色相同。不需要额外的代码。

            【讨论】:

              猜你喜欢
              • 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
              相关资源
              最近更新 更多