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

goo*_*ing -1 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);
Run Code Online (Sandbox Code Playgroud)

如何为此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>
Run Code Online (Sandbox Code Playgroud)

而且iv.setImageResource(R.drawable.style);,然后它只显示这个边框,而不是图像.可能吗?

Amu*_*are 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
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

但是,如果您不想要此效果,而是想要拉伸图像以填充边框(无黑色空格),则按如下方式更新代码:

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
Run Code Online (Sandbox Code Playgroud)

这将产生如下效果:

在此输入图像描述

注意:(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);
    }
}
Run Code Online (Sandbox Code Playgroud)

使用方法如下:

BorderDrawable drawable = new BorderDrawable(getResources(), bmp);
imageView.setImageDrawable(drawable);
Run Code Online (Sandbox Code Playgroud)

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

1)BORDER_WIDTHvalue定义边框的粗细(以像素为单位).

2)GRADIENT_COLORS颜色定义边框中用于渐变的颜色.根据您的值等更改它们image_border_start.

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