圆形的imageview问题android

时间:2015-05-25 18:39:21

标签: android rounded-corners

我正在尝试为imageview获取圆角边框。这是代码:

http://stackoverflow.com/a/3292810/4224275

我的问题是如下声明的普通imageview,如何使用类对象使图像舍入。这是我的代码,它没有用。

private ImageView imageView;
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.buildDrawingCache();

Bitmap bmap = imageView.getDrawingCache();
ImageHelper imh = new ImageHelper();
imh.getRoundedCornerBitmap(bmap, 10);

它对我不起作用。我不知道该怎么做。

这是logcat输出:

logcat

1 个答案:

答案 0 :(得分:1)

看看我的例子:

public class RoundedNetworkImageView extends NetworkImageView {

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

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

    public RoundedNetworkImageView(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 _bitmap = ((BitmapDrawable)_drawable)
                .getBitmap().copy(Bitmap.Config.ARGB_8888, true);

        canvas.drawBitmap(getCroppedBitmap(_bitmap, (int) (getWidth() / 1.2f)), 0,0, null);

    }

    public static Bitmap getCroppedBitmap(Bitmap bitmap, int radius) {

        Bitmap _newBitmap;

        if(bitmap.getWidth() != radius || bitmap.getHeight() != radius) {

            _newBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);

        }
        else{

            _newBitmap = bitmap;

        }

        Bitmap _outBitmap = Bitmap.createBitmap(_newBitmap.getWidth(),
                _newBitmap.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas _canvas = new Canvas(_outBitmap);

        Paint _paint = new Paint();

        Rect _rect = new Rect(0, 0, _newBitmap.getWidth(), _newBitmap.getHeight());

        _paint.setAntiAlias(true);
        _paint.setFilterBitmap(true);
        _paint.setDither(true);

        _canvas.drawARGB(0, 0, 0, 0);

        _paint.setColor(Color.parseColor("#BAB399"));

        _canvas.drawCircle(_newBitmap.getWidth() / 2, _newBitmap.getHeight() / 2,
                _newBitmap.getWidth() / 2.5f, _paint);

        _paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        _canvas.drawBitmap(_newBitmap, _rect, _rect, _paint);


        return _outBitmap;
    }

}

在xml中:

<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/imgShot_shotDetail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"/>

在活动中:

NetworkImageView imgShot = headerView.findViewById(R.id.imgShot_shotDetail);

imgShot.setImageUrl(url, AppControllerImage.getInstance().getImageLoader());
imgShot.setDefaultImageResId(R.drawable.notfound);
imgShot.setErrorImageResId(R.drawable.notfound);

在这种情况下,我使用了Volley库中的NetworkImageView,但您可以使用ImageView代替。你只需要调整代码。