User Tools

Site Tools


android:round-image

Android Round corner Drawable

Round Corner Drawable

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.mv);
		ImageView iv = (ImageView) findViewById(R.id.id_one);
		iv.setImageDrawable(new RoundImageDrawable(bitmap));

package com.zhy.view;
 
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
 
public class RoundImageDrawable extends Drawable
{
 
	private Paint mPaint;
	private Bitmap mBitmap;
 
	private RectF rectF;
 
	public RoundImageDrawable(Bitmap bitmap)
	{
		mBitmap = bitmap;
		BitmapShader bitmapShader = new BitmapShader(bitmap, TileMode.CLAMP,
				TileMode.CLAMP);
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setShader(bitmapShader);
	}
 
	@Override
	public void setBounds(int left, int top, int right, int bottom)
	{
		super.setBounds(left, top, right, bottom);
		rectF = new RectF(left, top, right, bottom);
	}
 
	@Override
	public void draw(Canvas canvas)
	{
		canvas.drawRoundRect(rectF, 30, 30, mPaint);
	}
 
	@Override
	public int getIntrinsicWidth()
	{
		return mBitmap.getWidth();
	}
 
	@Override
	public int getIntrinsicHeight()
	{
		return mBitmap.getHeight();
	}
 
	@Override
	public void setAlpha(int alpha)
	{
		mPaint.setAlpha(alpha);
	}
 
	@Override
	public void setColorFilter(ColorFilter cf)
	{
		mPaint.setColorFilter(cf);
	}
 
	@Override
	public int getOpacity()
	{
		return PixelFormat.TRANSLUCENT;
	}
 
}

CircleImageDrawable

package com.zhy.view;
 
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
 
public class CircleImageDrawable extends Drawable
{
 
	private Paint mPaint;
	private int mWidth;
	private Bitmap mBitmap ; 
 
	public CircleImageDrawable(Bitmap bitmap)
	{
		mBitmap = bitmap ; 
		BitmapShader bitmapShader = new BitmapShader(bitmap, TileMode.CLAMP,
				TileMode.CLAMP);
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setShader(bitmapShader);
		mWidth = Math.min(mBitmap.getWidth(), mBitmap.getHeight());
	}
 
	@Override
	public void draw(Canvas canvas)
	{
		canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mPaint);
	}
 
	@Override
	public int getIntrinsicWidth()
	{
		return mWidth;
	}
 
	@Override
	public int getIntrinsicHeight()
	{
		return mWidth;
	}
 
	@Override
	public void setAlpha(int alpha)
	{
		mPaint.setAlpha(alpha);
	}
 
	@Override
	public void setColorFilter(ColorFilter cf)
	{
		mPaint.setColorFilter(cf);
	}
 
	@Override
	public int getOpacity()
	{
		return PixelFormat.TRANSLUCENT;
	}
 
}

/var/www/dokuwiki/wiki/data/pages/android/round-image.txt · Last modified: 2016/05/05 13:07 by 127.0.0.1