1. 程式人生 > >Android:自定義ImageView展示大圖中的指定區域並切圓角(雪碧圖)

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); } }