首页 > 怎么给一个imageview设置一个shadow 设置一个阴影

怎么给一个imageview设置一个shadow 设置一个阴影

怎么给一个imageview设置一个shadow 设置一个阴影


比较简单的办法,效果比较丑陋,同理,你可以用阴影图片替代画线得到好的效果

重写ImageView

public class HKImageView extends ImageView {

    public HKImageView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public HKImageView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.d("lg", "onDraw");
        super.onDraw(canvas);

        // 画边框
        Rect rect1 = getRect(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.GRAY);
        paint.setStyle(Paint.Style.STROKE);

        // 画边框
        canvas.drawRect(rect1, paint);
        
        paint.setColor(Color.LTGRAY);

        // 画一条竖线,模拟右边的阴影
        canvas.drawLine(rect1.right + 1, rect1.top + 2, rect1.right + 1,
                rect1.bottom + 2, paint);
        // 画一条横线,模拟下边的阴影
        canvas.drawLine(rect1.left + 2, rect1.bottom + 1, rect1.right + 2,
                rect1.bottom + 1, paint);
        
        // 画一条竖线,模拟右边的阴影
        canvas.drawLine(rect1.right + 2, rect1.top + 3, rect1.right + 2,
                rect1.bottom + 3, paint);
        // 画一条横线,模拟下边的阴影
        canvas.drawLine(rect1.left + 3, rect1.bottom + 2, rect1.right + 3,
                rect1.bottom + 2, paint);
    }

    Rect getRect(Canvas canvas) {
        Rect rect = canvas.getClipBounds();
        rect.bottom -= getPaddingBottom();
        rect.right -= getPaddingRight();
        rect.left += getPaddingLeft();
        rect.top += getPaddingTop();
        return rect;
    }
}

使用
要给图片添加padding才有效果
imageView.setPadding(3, 3, 5, 5);

【热门文章】
【热门文章】