Android draw border in ImageView(Android 在 ImageView 中绘制边框)
问题描述
我想在图像周围画一个边框.但是我无法对齐 ImageView 本身的边框(就像大多数情况下一样),因为我使用 ImageMatrix 翻译和缩放 ImageView 内部的图像(ImageView 本身是 fill_parent/填充整个屏幕).我的想法是添加第二张图片(看起来像边框)并翻译 &以与应该有边框的图像相同的方式缩放它,但这样做不是很方便.有没有人更好的想法来实现这个目标?
I want to draw a border around an image. But I can't align the border at the ImageView itself (like it is done mostly) because I translate and scale the image inside of the ImageView with the ImageMatrix (the ImageView itself is fill_parent / fills the whole screen). I had the idea to add a second image (which looks like a border) and translate & scale it in the same way as the image which should have a border, but it isn't very handy to do it this way. Has anybody a better idea to reach that goal?
推荐答案
有两种方法可以实现:1) 为 imageView 添加填充并为其设置背景颜色.
There are two ways to achieve this: 1) add padding to the imageView and set a background color to it.
final ImageView imageView = new ImageView(context);
imageView.setPadding(2*border,2*border,0,0);
final ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(width,height);
params.leftMargin = marginYouWouldSet + border;
params.topMargin = marginYouWouldSet + border;
imageView.setBackgroundDrawable(drawable);
imageView.setBackgroundColor(borderColor);
addView(imageView, params);
2) 另一种选择是覆盖视图的 draw 方法并在那里绘制边框:
2) another option is to override the draw method of your view and there draw the border:
@Override
protected void dispatchDraw(Canvas canvas)
{
 borderDrawable.draw(canvas);
 super.dispatchDraw(canvas);
}
...
public class BorderDrawable extends Drawable{
    private Rect mBounds;
    private Paint mBorderPaint;
    public BorderDrawable(Rect bounds, int thickness, int color) {
        mBounds = bounds;
        mBorderPaint = new Paint();
        mBorderPaint.setStrokeWidth(thickness);
        mBorderPaint.setColor(color);
    }
    @Override
    public void draw(Canvas canvas) {
        //left border
        canvas.drawLine(
                mBounds.left - thickness/2, 
                mBounds.top,
                mBounds.left - thickness/2,
                mBounds.bottom,
                mBorderPaint);
        //top border
        canvas.drawLine(
                mBounds.left, 
                mBounds.top - thickness/2,
                mBounds.right, 
                mBounds.top - thickness/2, 
                mBorderPaint);
        //right border
        canvas.drawLine(
                mBounds.right + thickness/2, 
                mBounds.top,
                mBounds.right + thickness/2,
                mBounds.bottom, 
                mBorderPaint);
        //bottom border
        canvas.drawLine(
                mBounds.left, 
                mBounds.bottom + thickness/2, 
                mBounds.right, 
                mBounds.bottom + thickness/2, 
                mBorderPaint);
    }
}
请注意,您要给出要绘制的行的中间(!)而且我还没有运行,也没有编译它,所以我不能 100% 确定它是正确的,但这些是方法:) 矩形边界应该是视图的边界矩形 - (0,0,width,height).
Note that you are to give the middle of the line you want to draw(!) And also I haven't run, nor compiled this, so I'm not 100% sure it's correct, but these are the ways :) Rect bounds should be the bounding rect of your view - (0,0,width,height).
这篇关于Android 在 ImageView 中绘制边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 在 ImageView 中绘制边框
				
        
 
            
        - Android - 拆分 Drawable 2022-01-01
 - 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
 - MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
 - 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
 - android 4中的android RadioButton问题 2022-01-01
 - 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
 - 想使用ViewPager,无法识别android.support.*? 2022-01-01
 - Android viewpager检测滑动超出范围 2022-01-01
 - 用 Swift 实现 UITextFieldDelegate 2022-01-01
 - Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
 
				
				
				
				