Android draw border in ImageView

不羁的心 提交于 2019-12-18 05:02:36

问题


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:


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) 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);
    }

}

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).




回答2:


Alternatively, put the imageView in a layout of some sort and just set padding:

static class BorderView extends FrameLayout
    {
        public ImageView imageView;

        public BorderView(Context context)
        {
            super(context);

            setLayoutParams(//wrap content)
            imageView = new ImageView(context);//set image and so forth
            addView(imageView);
        }

        public void addSelectionBorder()
        {
            int border = 8;
            setPadding(border,border,border,border);
            setBackgroundColor(Color.BLUE);
        }

        public void removeSelectionBorder()
        {
            int border = 0;
            setPadding(border,border,border,border);
            setBackgroundColor(Color.BLACK);
        }
    }


来源:https://stackoverflow.com/questions/7626554/android-draw-border-in-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!