当前位置:编程学习 > wap >>

android 显示 圆形图片

RT,怎样做一个圆形的控件来显示一个图片,使得图片出现圆形的效果。
类似效果如 新版手机qq空间 里个人的头像

我之前给button设置了一个background----自定义shape,实现了圆形按钮。
然后我用同样的方法放到imageview上就没效果,原因可能是imageView的src覆盖了background

我自己写了一个ImageView ,并在OnDraw中给canvas 设置了一个 path,虽然实现了这种圆形效果,但是有锯齿。而抗锯齿只有使用paint才能实现,所以想问下大家,如何完美实现这种效果??
我的代码:

@Override
    protected void onDraw(Canvas canvas)
    {
        Path clipPath = new Path();
        clipPath.addCircle(getWidth()/2,getHeight()/2,Math.min(getWidth()/2,getHeight()/2),
                           Path.Direction.CW);

        canvas.clipPath(clipPath);

        super.onDraw(canvas);
    }



不需要提供代码,只需要一个思路,几个关键字,我自己研究就行,谢谢各位! --------------------编程问答-------------------- http://blog.csdn.net/whu_zhangmin/article/details/12652045
我之前实现过,正好写了记录,楼主可以去看看。
传入原Bitmap,函数返回的就是变圆后的Bitmap
private Bitmap circleBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff000000;
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        mPaint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        mPaint.setColor(color);
        final int width = bitmap.getWidth();
        canvas.drawCircle(width / 2, width / 2, width / 2, mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, mPaint);//将图片绘制成白色图片
        return output;
}
--------------------编程问答--------------------
引用 1 楼 ncepu307 的回复:
http://blog.csdn.net/whu_zhangmin/article/details/12652045
我之前实现过,正好写了记录,楼主可以去看看。
传入原Bitmap,函数返回的就是变圆后的Bitmap
private Bitmap circleBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff000000;
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        mPaint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        mPaint.setColor(color);
        final int width = bitmap.getWidth();
        canvas.drawCircle(width / 2, width / 2, width / 2, mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, mPaint);//将图片绘制成白色图片
        return output;
}



万分感谢,你给了我启发。
我又查看了下 ApiDemo中Graphics/Xfermodes 工程,确实有很多效果,也推荐给你看看。
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,