圆形相框的图片

效果如下

首先自定义一个控件继承imageview,然后绘制相框,注意代码中的+6是根据相框的宽定的,这里宽是5,再加1就差不多了,不加的话,相框会有部分画在外面。

package com.sharera.capitalcircle.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;
//圆形图像边框类配合OhterUtils的获取圆形图片方法,得到带边框的圆形图片
public class RoundImage extends ImageView {

	public RoundImage(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public RoundImage(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public RoundImage(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) { // 这里就是重写的方法了,想画什么形状自己动手
		super.onDraw(canvas);		
		Bitmap btiDrawable = ((BitmapDrawable) super.getDrawable()).getBitmap();
		// 画边框
		Rect rec = canvas.getClipBounds();
		rec.top = rec.top+6;
		rec.bottom = rec.bottom - 6;
		rec.left = rec.left + 6;
		rec.right = rec.right - 6;
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		//渐变边框颜色
		Shader mShader = new LinearGradient(0, 0, 0, 300,
				new int[] { 0xff000000,0xffeaeaea,0xffd6d6d6}, new float[]{0 ,0.5f,1.0f}, Shader.TileMode.CLAMP);
		paint.setShader(mShader); 
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(5);
		RectF rf2 = new RectF(rec);
		canvas.drawOval(rf2, paint);
	}
}

 页面代码如下:

<com.sharera.capitalcircle.base.RoundImage
                android:id="@+id/home_head"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_marginLeft="20dp"                
                android:padding="5dp"
                android:src="@drawable/welcome" />

后台部分如下:

imgHead = (ImageView) first.findViewById(R.id.home_head);		
		Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.frame_home)).getBitmap();
		Bitmap resultBitmap=OtherUtils.getRoundedCornerBitmap(bitmap);		
		imgHead.setImageBitmap(resultBitmap);
		imgHead.setScaleType(ScaleType.CENTER_CROP);

上面的代码中调用了裁剪图片为圆形的方法,注意ScaleType,我开始以为反正是圆形了,设什么都一样,结果用平铺模式,反而结果不是园的,下面是裁剪的方法,注意是canvas.drawCircle方法,开始用http://blog.csdn.net/mengweiqi33/article/details/7642813提供的方法,没注意看,这个剪出来的是椭圆,不是圆!!!。

package com.sharera.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;

import com.sharera.capitalcircle.base.RoundImage;

public class OtherUtils {

	public static void chanageToRoundedBitmap(RoundImage image) {
		Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
		Bitmap resultBitmap=getRoundedCornerBitmap(bitmap);
		image.setImageBitmap(resultBitmap);
	}
	
	public static void chanageToRoundedBitmap(ImageView image) {
		Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
		Bitmap resultBitmap=getRoundedCornerBitmap(bitmap);
		image.setImageBitmap(resultBitmap);
	}
	
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242; 
		final Paint paint = new Paint(); 
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
		final RectF rectF = new RectF(rect); 
		final float roundPx = rect.width()< rect.height()?rect.width():rect.height(); 

		paint.setAntiAlias(true); 
		canvas.drawARGB(0, 0, 0, 0); 
		paint.setColor(color);		
		canvas.drawCircle(rect.width()/2,rect.height()/2,roundPx/2,paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
		canvas.drawBitmap(bitmap, rect, rect, paint); 
		return output; 
	}
}

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值