在运行时向ImageView添加边框和其他一些样式

时间:2013-12-23 16:12:52

标签: java android imageview

我目前的代码:

    LinearLayout.LayoutParams params = new TableLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

        ImageView imageview= new ImageView(this);
        imageview.setImageBitmap(bmp);
        imageview.setLayoutParams(params);
        layout.addView(imageview);

如何为此imageview添加边框?我试过了:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <gradient android:angle="90"
                      android:startColor="@color/image_border_start"
                      android:centerColor="@color/image_border_center"
                      android:endColor="@color/image_border_end" />
        </shape>
    </item>
    <item android:top="1dp" android:left="1dp"
          android:right="1dp" android:bottom="1dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/default_back_color" />
        </shape>
    </item>
</layer-list>

iv.setImageResource(R.drawable.style);,但它只显示此边框,而不是图像。有可能吗?

3 个答案:

答案 0 :(得分:6)

您需要将R.drawable.style设置为背景。然后在ImageView添加一些填充。见代码:

ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bmp);
imageView.setLayoutParams(params);
imageView.setBackgroundResource(R.drawable.style);
imageView.setPadding(2,2,2,2); // <- try out different values

注意:如果图像(bmp)是方形的,则边框将完美地显示在图像周围。如果图像是非正方形,则图像周围将出现方形边框(绿色),剩余空间将填充黑色,如下所示:

enter image description here

但是,如果您不想要此效果,而是想要拉伸图像以填充边框(没有黑色空格),请按以下步骤更新代码:

ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bmp);
imageView.setLayoutParams(params);
imageView.setBackgroundResource(R.drawable.main_header_selector);
imageView.setPadding(2, 2, 2, 2);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); // <- set the scale
imageView.setCropToPadding(true); // <- requires API 16 or more

这将产生如下效果:

enter image description here

注意:(1)我使用绿色边框进行演示。 (2)通过更改android:top,left,right,bottom

中的style.xml值,可以增加边框尺寸

更新:如何将边框应用于图片的另一种方法是扩展BitmapDrawable,然后绘制自定义边框矩形。声明class如下:

public class BorderDrawable extends BitmapDrawable {

    private static final int BORDER_WIDTH = 10;
    private final int[] GRADIENT_COLORS = {Color.BLUE, Color.GREEN, Color.RED};
    Paint borderPaint;

    public BorderDrawable(Resources res, Bitmap bitmap) {
        super(res, bitmap);
        this.borderPaint = new Paint();
        borderPaint.setStrokeWidth(BORDER_WIDTH);
        borderPaint.setStyle(Paint.Style.STROKE);

        // set border gradient
        Shader shader = new LinearGradient(0, 0, 0, getBounds().bottom, GRADIENT_COLORS, null,  Shader.TileMode.CLAMP);
        borderPaint.setShader(shader);

        // or the border color
        // borderPaint.setColor(Color.GREEN);

    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        // draw
        canvas.drawRect(getBounds(), borderPaint);
    }
}

使用方法如下:

BorderDrawable drawable = new BorderDrawable(getResources(), bmp);
imageView.setImageDrawable(drawable);

可以修改上述课程以满足您的需求:

1)BORDER_WIDTH值定义边框的粗细(以像素为单位)。

2)GRADIENT_COLORS颜色定义边框中渐变使用的颜色。根据您的值更改它们,例如image_border_start等。

无论图像的尺寸如何,输出都将是完美的边框。

答案 1 :(得分:1)

你可以做一些像绘制图像本身的东西,这些都是

public Bitmap addBorder(Bitmap bitmap){
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANIT_ALIAS_FLAG);
    paint.setColor(Paint.BLACK);

    Path path = new Path();
    path.addRect(0,0,bitmap.getWidth(),bitmap.getHeight(),Direction.CW);

    canvas.drawPath(path,paint);
    return bitmap;
}

不完全确定这是否100%正确但基本上将位图转换为画布,您可以在其上绘制并只使用图像的尺寸在图像周围绘制一个直尺

答案 2 :(得分:0)

请勿使用iv.setImageResource(R.drawable.style)。它只是替换了实际的位图。

而不是那样,使用:

iv.setBackgroundResource(R.drawable.style).