2012-10-31 2 views
2

사용자 정의 ImageView 주위에 검은 색 테두리를 추가하고 싶습니다. 현재 나는 이미지 뷰에 둥근 상단 모서리를 구현하는이 클래스를 사용하고 있습니다 :사용자 정의 이미지 뷰 주위에 테두리를 그립니다.

public class RoundedBitmapDisplayer implements BitmapDisplayer { 

    private Context ctx; 
    public RoundedBitmapDisplayer(Context context) { 
     this.ctx = context; 
    } 
    @Override 
    public Bitmap display(Bitmap bitmap, ImageView imageView) { 
     Bitmap roundBitmap; 
     try { 

      roundBitmap = getRoundedCornerBitmap(ctx, bitmap, 10, bitmap.getWidth(), bitmap.getHeight(), 
         false, false, true, true); 
     } catch (OutOfMemoryError e) { 
      Log.e(ImageLoader.TAG, "Can't create bitmap with rounded corners. Not enough memory.", e); 
      roundBitmap = bitmap; 
     } 
     imageView.setImageBitmap(roundBitmap); 
     return roundBitmap; 
    } 

    /* 
    private Bitmap getRoundedCornerBitmap(Bitmap bitmap) { 
     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     final RectF rectF = new RectF(rect); 

     paint.setAntiAlias(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(0xFFFFFFFF); 
     canvas.drawRoundRect(rectF, roundPixels, roundPixels, paint); 

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

     return output; 
    }*/ 
    public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels , int w , int h , boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR ) { 

     Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 
     final float densityMultiplier = context.getResources().getDisplayMetrics().density; 

     final int color = 0xff424242; 
     final Paint paint = new Paint(); 

     final Rect rect = new Rect(0, 0, w, h); 
     final RectF rectF = new RectF(rect); 

     //make sure that our rounded corner is scaled appropriately 
     final float roundPx = pixels*densityMultiplier; 

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



     //draw rectangles over the corners we want to be square 
     if (squareTL){ 
      canvas.drawRect(0, 0, w/2, h/2, paint); 

     } 
     if (squareTR){ 
      canvas.drawRect(w/2, 0, w, h/2, paint); 

     } 
     if (squareBL){ 
      canvas.drawRect(0, h/2, w/2, h, paint); 

     } 
     if (squareBR){ 
      canvas.drawRect(w/2, h/2, w, h, paint); 

     } 

     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 

     canvas.drawBitmap(input, 0,0, paint); 
     return output; 
    } 
} 

답변

0

당신은 당신은 캔버스에 선을 그릴 수 있습니다 <stroke />

4

를 포함하는 배경 XML 드로어 블을 설정할 수 있습니다. 올바른 좌표를 전달했는지 확인하십시오. 의 onDraw에서 ()

Paint bp= new Paint(); 
    bp.setColor(Color.RED);//set a color 
    bp.setStrokeWidth(5);// set your stroke width 
    // w and h are width and height of your imageview 
    canvas.drawLine(0, 0, w, 0,bp); 
    canvas.drawLine(0, 0, 0, h,bp); 
    canvas.drawLine(w,h,w,0,bp); 
    canvas.drawLine(w, h, 0,h , bp); 
0

이 그릴 수

<gradient 
    android:angle="270"> 
</gradient> 
<stroke 
    android:width="2dp" 
    android:color="#3b64a8" /> 
<corners 
    android:radius="2dp" /> 
<padding 
    android:left="1dp" 
    android:top="1dp" 
    android:right="1dp" 
    android:bottom="1dp" /> 

에서 image_border.xml 파일을 생성하고 이미지 뷰의 XML 항목이 XML을 사용

<ImageView android:id="@+id/image" 
    android:background="@drawable/image_border" <!--add xml like this--> 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:src="@drawable/image" /> 
관련 문제