您的位置:首页 > 移动开发 > Android开发

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

2015-02-06 11:21 375 查看
很多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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息