Android图片圆角控件

Android图片圆角控件

转载自第一代码原创文章https://www.diyidaima.com/android/100.html

绘制圆角图片控件
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import com.android.volley.toolbox.NetworkImageView;

/*【圆角】绘制圆角图片控件
 * NetworkImageView是Volley提供的一个的类,利用这个类,我们可以更有效率地去从网络去获取图片,
 * 因为它里面帮我们多设置了一个缓存,帮我们自己去处理请求的队列。
 * 请自行度娘下载volley.jar。
 * 如果只是加载本地drawable或者mipmap的图片,可以直接继承ImageView
*/
public class RoundCornerImageView extends NetworkImageView {
    public RoundCornerImageView(Context context) {
        super(context);
    }

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

    public RoundCornerImageView(Context context, AttributeSet attrs,
                                int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (getDrawable() == null) {
            return;
        }
        Path clipPath = new Path();
        int w = this.getWidth();
        int h = this.getHeight();
        //画出15度的圆角效果,15度的圆角比5度、10度更加顺滑
        float rx=15.0f;
        float ry=15.0f;
        clipPath.addRoundRect(new RectF(0, 0, w, h), rx, ry, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }
}

####在布局文件中使用:

<com.swissabl.utils.RoundCornerImageView
	 android:id="@+id/id_image_kaquan_name_pic"
     android:layout_width="50dp"
     android:layout_height="50dp"
     android:scaleType="centerCrop"
     android:padding="0dp"
     android:layout_marginLeft="10dp"
     android:layout_marginRight="10dp"/>

####在Java代码里绑定图片:

	public class ImageActivity{
	private RequestQueue queue;
	private ImageLoader imageLoader;
	
	 @Override
	protected void onCreate(Bundle savedInstanceState) {
		queue = Volley.newRequestQueue(this);
		imageLoader = new ImageLoader(queue, new BitmapCache());
		RoundCornerImageView id_image_kaquan_name_pic= (RoundCornerImageView) findViewById(R.id.id_image_kaquan_name_pic);
		String url="http://image.xinmin.cn/2012/11/01/20121101105203854339.jpg";
		id_image_kaquan_name_pic.setImageUrl(url,imageLoader);
	
	}
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值