使用Universal Image Loader为ImageView Android添加边框

时间:2014-12-26 10:37:08

标签: android android-imageview universal-image-loader

我想在ImageView周围添加边框(白色)。

以下是布局代码:

<ImageView
                android:id="@+id/detail_annonce_image_user"
                android:layout_width="65dp"
                android:layout_height="65dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="30dp"
                android:layout_marginTop="225dp"
                android:scaleType="centerCrop"
                android:src="@drawable/avatar1" />

这是JAVA代码:

    options = new DisplayImageOptions.Builder()
            .displayer(new RoundedBitmapDisplayer(2000))
            .cacheOnDisc(true)
            .build();

if (avatarUser.contains("avatar") == true) {
    if (avatarUser.equals("avatar1")) {
        imageUser_txt.setImageResource(R.drawable.avatar1) ;
    }
    else if (avatarUser.equals("avatar2")) {
        imageUser_txt.setImageResource(R.drawable.avatar2) ;
    }
    else if (avatarUser.equals("avatar3")) {
        imageUser_txt.setImageResource(R.drawable.avatar3) ;
    }
    else if (avatarUser.equals("avatar4")) {
        imageUser_txt.setImageResource(R.drawable.avatar4) ;
    }
    else if (avatarUser.equals("avatar5")) {
        imageUser_txt.setImageResource(R.drawable.avatar5) ;
    }
    else if (avatarUser.equals("avatar6")) {
        imageUser_txt.setImageResource(R.drawable.avatar6) ;
    }
    else {
        imageUser_txt.setImageResource(R.drawable.avatar1) ;
    }

} else {
    imageLoader.displayImage(avatarUser, imageUser_txt, options);
}

imageUser_txt.bringToFront();

2 个答案:

答案 0 :(得分:0)

您需要通过扩展ImageView来创建自定义图像视图。使用以下代码在布局xml文件中使用此ImageView,您可以在图像视图周围创建白条

public class CustomImageView extends ImageView {
private Context myCtx;
private int borderSize = 6;
Bitmap bmp;
Bitmap bmpWithBorder;
private Drawable drawable;
public CustomImageView(Context context) {
super(context);
myCtx = context;
// TODO Auto-generated constructor stub
}
public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
myCtx = context;
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
myCtx = context;
}
/* (non-Javadoc)
* @see android.widget.ImageView#onDraw(android.graphics.Canvas)
*/
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
try{
drawable = getDrawable();
bmp = ((BitmapDrawable)drawable).getBitmap() ;
bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2, bmp.getHeight() + borderSize * 2, bmp.getConfig());
canvas.drawBitmap(bmpWithBorder, 0 , 0 , null);
canvas.drawColor(Color.WHITE);
Resources res = myCtx.getResources();
canvas.drawBitmap(bmp, borderSize, borderSize, null);
}
catch (NullPointerException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}

答案 1 :(得分:0)

为ImageView添加填充和背景。填充将是边框的宽度,背景将是它的颜色。

<ImageView
       android:id="@+id/detail_annonce_image_user"
       android:layout_width="65dp"
       android:layout_height="65dp"
       android:layout_alignParentRight="true"
       android:layout_marginRight="30dp"
       android:layout_marginTop="225dp"
       android:scaleType="centerCrop"
       android:src="@drawable/avatar1"
       android:padding="5dp"
       android:background="#ffffff" />