RoundedImageView添加边框和阴影。

时间:2014-11-10 11:15:42

标签: java android android-imageview

我使用以下类来获取舍入的ImageView:https://github.com/vinc3m1/RoundedImageView

一切正常,但我想在我的图片视图中添加阴影和边框,例如:How to add a shadow and a border on circular imageView android?

我修改了Rounded ImageView类以包含这些函数:

private void setup()
{
    // init paint
    paint = new Paint();
    paint.setAntiAlias(true);

    paintBorder = new Paint();
    setBorderColor(Color.WHITE);
    paintBorder.setAntiAlias(true);
    this.setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
    paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
}



    public Bitmap convertToBitmap(Drawable drawable, int widthPixels, int heightPixels) {
        Bitmap mutableBitmap = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mutableBitmap);
        drawable.setBounds(0, 0, widthPixels, heightPixels);
        drawable.draw(canvas);

        return mutableBitmap;
    }

    @Override
    public void onDraw(Canvas canvas)
    {

            BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(convertToBitmap(mDrawable,canvas.getWidth(), canvas.getHeight()), canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            paint.setShader(shader);
            int circleCenter = viewWidth / 2;
            canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - 4.0f, paintBorder);
            canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - 4.0f, paint);
    }

现在我得到了阴影效果,但我没有得到白色边框。任何提示?

1 个答案:

答案 0 :(得分:0)

使用“线性”布局,然后设置布局背景 android:background="@drawable/layout_shadow_border"

然后设置padding = 4dp并将图像视图放在线性布局中。

///////////////////////// layout_shadow_border.xml /////////////////

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#CABBBBBB"/>
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:left="0dp"
        android:right="0dp"
        android:top="0dp"
        android:bottom="2dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>