首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >圆形图像周围有黑色方块

圆形图像周围有黑色方块
EN

Stack Overflow用户
提问于 2014-02-26 10:06:36
回答 1查看 197关注 0票数 0

我有一个矩形图像,我将其转换为圆形,然后将该图像设置为imageview。然而,我在圆形图像周围看到了一个黑色的正方形。我需要它来匹配布局的其余部分,这是white.Can,有人解释为什么?下面的布局是一个更大的布局的一部分,该布局用作列表视图的布局。我在不同的应用程序中单独尝试了这段代码,它工作得很好。在该应用程序中,xml布局的默认颜色是白色,所以它可能仍在绘制正方形,但我就是看不到它?在我的原始应用程序中,xml的默认颜色是黑色。这可能是导致问题的原因吗?

以下是布局的一部分

代码语言:javascript
代码运行次数:0
运行
复制
   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#fff" >

        <ImageView
            android:id="@+id/iv_profile"
            android:layout_width="90dp"
            android:layout_height="90dp" />
    </RelativeLayout>
EN

回答 1

Stack Overflow用户

发布于 2014-03-04 21:58:31

使用这个类

代码语言:javascript
代码运行次数:0
运行
复制
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundedCustomImageView extends ImageView{

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

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

    public RoundedCustomImageView(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; 
        }

        try {
            Bitmap b = ((BitmapDrawable) drawable).getBitmap();
            Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
            int w = getWidth(), h = getHeight();
            Bitmap roundBitmap =  getCroppedBitmap(bitmap, w);
            canvas.drawBitmap(roundBitmap, 0,0, null);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    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(), Config.ARGB_8888);
        //Bitmap output = Bitmap.createBitmap(200,200, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xffa19774;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
        //      final Rect rect = new Rect(0, 0, 200, 200);
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#030302"));
        canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
                sbmp.getWidth() / 2+0.1f, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(sbmp, rect, rect, paint);
        return output;
    }}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22030270

复制
相关文章

相似问题

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