最简单的实现圆形图片的方式

很多demo中实现圆形图片的方面都使用了Xfermod,目前对Xfermod还不熟悉根据http://blog.csdn.net/sjf0115/article/details/7267532这篇文章中的思路,简单修个了下,

使用BitmapShader这种方式更好理解一些.github地址

package com.example.circle;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;

public class SimpleCircularImage extends View
{
    private  BitmapShader bitmapShader = null;
    private Bitmap bitmap = null;
    private ShapeDrawable shapeDrawable = null;
    private int width  = 0;
  
    public SimpleCircularImage(Context context)
    {
        super(context);
        bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.head)).getBitmap();  
        width = Math.min(bitmap.getWidth(), bitmap.getHeight());
        bitmapShader = new BitmapShader(bitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        //将图片裁剪为椭圆形  
        shapeDrawable = new ShapeDrawable(new OvalShape());
        //得到画笔并设置渲染器
        shapeDrawable.getPaint().setShader(bitmapShader);
        //设置显示区域
        shapeDrawable.setBounds(0, 0,width,width);
        //绘制shapeDrawable
        shapeDrawable.draw(canvas);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值