Android:自定义ImageView展示大图中的指定区域并切圆角(雪碧图)

@Override
protected void onDraw(Canvas canvas)
{
    Drawable drawable = getDrawable();
    if (drawable instanceof BitmapDrawable)
    {
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        Paint paint = new Paint();

        ...//根据需要计算展示区域在大图中的位置

        // 绘制大图的某个区域,左上右下
        Rect src = new Rect(left, top, right, bottom);

        // 绘制区域,填满控件
        Rect des = new Rect(0, 0, getWidth(), getHeight());

        // 圆角
        Path clipPath = new Path();
        clipPath.addRoundRect(new RectF(des), radius, radius, Path.Direction.CW);
        canvas.clipPath(clipPath);

        canvas.drawBitmap(bitmap, src, des, paint);
    }
    else
    {
        super.onDraw(canvas);
    }
}

你可能感兴趣的:(Android)