2016-05-07 2 views
0

Как добавить границу в CircularNetworkImageView и контролировать ширину и цвет границы? Dont уведомление иконки вокруг изображения)Как добавить тень в CircularNetworkImageView

Как это: enter image description here

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; 
    } 
} 

ответ

0

Попробуйте использовать

this.setLayerType(LAYER_TYPE_SOFTWARE, paintBorder); 
paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK); 

В вашем коде, это может выглядеть следующим образом:

public class CircularNetworkImageView extends NetworkImageView { 
    Context mContext; 
    Paint mPaintBorder; 
    public CircularNetworkImageView(Context context) { 
     super(context); 
     init(context); 
    } 
    public CircularNetworkImageView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
     init(context); 
    } 

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

    private void init(Context context) { 
     mContext = context; 
     mPaintBorder = new Paint(); 
     this.setLayerType(LAYER_TYPE_SOFTWARE, mPaintBorder); 
     mPaintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK); 
    } 

    @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, mPaintBorder); 
     canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 

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

     return output; 
    } 
} 

Хотя Я действительно ненавижу, что вы создаете все те объекты i nside getCircularBitmap. Было бы гораздо лучше использовать IMHO, чтобы сделать ваш другой объект Paint полем и инициализировать его в новом методе init.

Смежные вопросы