首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在圆形imageView android上添加阴影和边框?

如何在圆形imageView android上添加阴影和边框?
EN

Stack Overflow用户
提问于 2013-07-15 21:21:21
回答 7查看 75.7K关注 0票数 63

我创建了一个带有这个问题的CircularImageView:Create circular image view in android

在上下载项目

1)这是CircularImageView类:

代码语言:javascript
复制
public class CircularImageView extends ImageView {
    public CircularImageView(Context context) {
        super(context);
    }

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

    public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return; 
        }
        Bitmap b =  ((BitmapDrawable)drawable).getBitmap() ;
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);      

        Bitmap roundBitmap =  getCroppedBitmap(bitmap, getWidth());
        canvas.drawBitmap(roundBitmap, 0, 0, null);
    }

    public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
        Bitmap sbmp;
        if(bmp.getWidth() != radius || bmp.getHeight() != radius)
            sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
        else
            sbmp = bmp;

        Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Bitmap.Config.ARGB_8888);
        final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);      
        paint.setColor(Color.parseColor("#BAB399"));

        Canvas c = new Canvas(output);        
        c.drawARGB(0, 0, 0, 0);
        c.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f, sbmp.getWidth() / 2+0.1f, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        c.drawBitmap(sbmp, rect, rect, paint);

        return output;
    }
}

2)我在布局中使用如下所示:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cccccc"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp" >

    <com.mikhaellopez.circularimageview.CircularImageView
        android:id="@+id/imageViewCircular"
        android:layout_width="@dimen/image_view_size"
        android:layout_height="@dimen/image_view_size"
        android:layout_gravity="center"
        android:background="@drawable/border"
        android:src="@drawable/image" />

</LinearLayout>

3)图片中的当前结果:

如何将此代码更改为在imageView周围显示阴影和圆形边框?

Objectif结果:

2015年10月15日编辑:

您可以使用或下载我的GitHub库 所有修复,方法是使用gradle dependency

代码语言:javascript
复制
compile 'com.mikhaellopez:circularimageview:2.0.1'
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17655264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档