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

 一坛苦水_179 发布于 2023-02-08 14:19

我目前的代码:

    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添加边框?我试过了:



    
        
            
        
    
    
        
            
        
    

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

1 个回答
  • 您需要设置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)是方形,则边框将在图像周围完美显示.如果图像是非正方形,则图像周围将出现方形边框(绿色),剩余空间将填充黑色,如下所示:

    在此输入图像描述

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

    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
    

    这将产生如下效果:

    在此输入图像描述

    注意:(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_WIDTHvalue定义边框的粗细(以像素为单位).

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

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

    2023-02-08 14:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有