2014-11-03 171 views
0

我有以下代碼來製作ImageView循環。我如何用顏色實現邊框?我試圖實現一些解決方案,也在Stackoverflow上,但沒有成功。這裏目前的進展:帶邊框顏色的RoundedImageview

public class RoundedImageView extends ImageView { 
    public RoundedImageView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    public RoundedImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public RoundedImageView(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(); 
     Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); 

     int w = getWidth(), h = getHeight(); 
     int radius = w < h ? w : h; 

     Bitmap roundBitmap = getCroppedBitmap(bitmap, radius, w, h); 
     // roundBitmap= ImageUtils.setCircularInnerGlow(roundBitmap, 0xFFBAB399, 
     // 4, 1); 
     canvas.drawBitmap(roundBitmap, 0, 0, null); 

    } 

    public static Bitmap getCroppedBitmap(Bitmap bmp, int radius, int w, int h) { 
     Bitmap sbmp; 
     if (bmp.getWidth() != radius || bmp.getHeight() != radius) { 
      float _w_rate = 1.0f * radius/bmp.getWidth(); 
      float _h_rate = 1.0f * radius/bmp.getHeight(); 
      float _rate = _w_rate < _h_rate ? _h_rate : _w_rate; 
      sbmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() * _rate), (int)(bmp.getHeight() * _rate), false); 
     } 
     else 
      sbmp = bmp; 

     Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), 
       Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final int color = 0xffa19774; 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight()); 

     paint.setAntiAlias(true); 
     paint.setFilterBitmap(true); 
     paint.setDither(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(Color.parseColor("#BAB399")); 
     canvas.drawCircle(w/2, h/2, (w < h ? w : h)/2, paint); 
     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     canvas.drawBitmap(sbmp, rect, rect, paint); 

     return output; 
    } 


} 

回答

0

這裏是爲了幫助你幾個步驟:P

1 - 創建一個XML繪製的文件中可繪製您指定的文件夾固色和半徑。

2-將ImageView放在高於ImageView尺寸的RoundImageView後面。

3-將drawable加載到新的ImageView中。