在android中,google只提供了对图形的圆形操作,而没有实现对图片的圆形操作,所以我们无法实现上述操作,在此我们将使用框架进行设计(下述框架为as编写):

https://github.com/monsterLin/RoundedImageView

https://github.com/pungrue26/SelectableRoundedImageView
https://github.com/hdodenhof/CircleImageView
https://github.com/MostafaGazar/CustomShapeImageView
https://github.com/siyamed/android-shape-imageview

下面我们通过使用RoundedImageView来是实现这种效果:

首先我们在项目中导入开源框架:

导入成功后-

第一种方式:直接书写xml文件:

<com.makeramen.roundedimageview.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/photo"
app:riv_corner_radius="30dip"
app:riv_border_width="2dip"
app:riv_border_color="#333333"
app:riv_mutate_background="true"
app:riv_oval="true" />

第二种方式:通过代码实现:

public static class RoundedDrawable extends Drawable {

        protected final float cornerRadius;
protected final int margin; protected final RectF mRect = new RectF(),
mBitmapRect;
protected final BitmapShader bitmapShader;
protected final Paint paint; public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) {
this.cornerRadius = cornerRadius;
this.margin = margin; bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mBitmapRect = new RectF (margin, margin, bitmap.getWidth() - margin, bitmap.getHeight() - margin); paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(bitmapShader);
} @Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
mRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin); // Resize the original bitmap to fit the new bound
Matrix shaderMatrix = new Matrix();
shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);
bitmapShader.setLocalMatrix(shaderMatrix); } @Override
public void draw(Canvas canvas) {
canvas.drawRoundRect(mRect, cornerRadius, cornerRadius, paint);
} @Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
} @Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
} @Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
} imageAware.setImageDrawable(new RoundedDrawable(bitmap, cornerRadius, margin));

附录:

eclipse的开源框架:http://files.cnblogs.com/files/boy1025/roundedimageview.zip

Android程序设计-圆形图片的实现的更多相关文章

  1. Android实现圆形图片

     情景再现: 写Android程序也有一段时间了,今天突然被问怎么实现一个圆形图片,很多app图像是圆形的.但是用户上传的图像可不是圆的,所以问题就来了,需要我们代码实现圆形图片.但是大脑飞转想到第三 ...

  2. Android自定义圆形图片工具类(CTRL+C加CTRL+V直接使用)

    先贴一下工具类的代码!可直接复制粘贴 public class RoundImageView extends ImageView { private Paint mPaint; //画笔 privat ...

  3. Android 自定义圆形图片 CircleImageView

    1.效果预览 1.1.布局中写自定义圆形图片的路径即可 1.2.然后看一看图片效果 1.3.原图是这样的 @mipmap/ic_launcher 2.使用过程 2.1.CircleImageView源 ...

  4. Android 绘制圆形图片

    经常在项目中,会遇到使用圆形头像. 然而图片往往不是圆形的,我们须要对图片进行处理.以达到圆形图片的效果.这里.我总结了一下经常使用的android圆形图片的绘制的方法. 主要有以下几种方式:1.画布 ...

  5. android绘制圆形图片的两种方式

    看下效果先 下面有完整的示例代码 使用BitmapShader(着色器) 我们在绘制view 的时候 就是小学上美术课 用水彩笔在本子上画画 使用着色器绘制圆形图片最简单的理解方式 就是把bitmap ...

  6. android自定义圆形图片和遇到的问题

    画圆遇到的问题:图片单位不一样,导致图片只能显示出圆的一部分:看代码: public class MyCircleIamge extends ImageView { private Context c ...

  7. Android实现圆形圆角图片

    本文主要使用两种方法实现图形圆角图片 自定View加上使用Xfermode实现 Shader实现 自定View加上使用Xfermode实现 /** * 根据原图和变长绘制圆形图片 * * @param ...

  8. Android圆形图片--ImageView

    [ RoundImageView.java ] package com.dxd.roundimageview; import android.content.Context; import andro ...

  9. Android 自定义View修炼-Android实现圆形、圆角和椭圆自定义图片View(使用BitmapShader图形渲染方法)

    一.概述 Android实现圆角矩形,圆形或者椭圆等图形,一般主要是个自定义View加上使用Xfermode实现的.实现圆角图片的方法其实不少,常见的就是利用Xfermode,Shader.本文直接继 ...

随机推荐

  1. Swagger 增加 DocumentFilter 隐藏不需要显示的接口

    services.ConfigureSwaggerGen(options => { options.SingleApiVersion(new Info { Version = "v1& ...

  2. 截取视图某一段另存为部分视图(Partial View)

    在做ASP.NET MVC后台管理程序时,根据程序需要,Isus.NET需要实现一个功能,就是动态截取视图某一段另存为部分视图Partial View. 思路为在视图中,使用jQury的程序截图以及P ...

  3. weblogic.nodemanager.common.ConfigException: Native version is enabled but nodemanager native library could not be loaded 解决办法

    近日在一个原本工作正常的weblogic web server(操作系统为redhat 64位系统)上折腾安装redis/hadoop等东东,yum install了一堆第3方类库后,重启weblog ...

  4. struts2: config-browser-plugin 与 convention-plugin 学习

    struts2被很多新手诟病的一个地方在于“配置过于复杂”,相信不少初学者因为这个直接改投Spring-MVC了.convention-plugin. config-browser-plugin这二个 ...

  5. POJO, DTO, VO, JavaBean的区别

    POJO(plain ordinary java object 简单java对象) POJO是一个简单的普通的Java对象,它不包含业务逻辑或持久逻辑等,但不是JavaBean.EntityBean等 ...

  6. PRML读书会第十四章 Combining Models(committees,Boosting,AdaBoost,决策树,条件混合模型)

    主讲人 网神 (新浪微博: @豆角茄子麻酱凉面) 网神(66707180) 18:57:18 大家好,今天我们讲一下第14章combining models,这一章是联合模型,通过将多个模型以某种形式 ...

  7. jquery 使用方法(转)

    原文: http://www.cnblogs.com/Chenfengtao/archive/2012/01/12/2320490.html jQuery是目前使用最广泛的javascript函数库. ...

  8. node简单操作mysql的类

    Creative.js 'use strict'; var pool = require('../utils/MysqlUtils'); var util = require('util'); var ...

  9. javascript: return return false

    一:代码(王工)var flag=true; $(function(){ $("#ff").submit(function(){ // 表单submit事件 registerUse ...

  10. VS2013 无法在Web服务器上启动调试。IIS未列出与打开的URL匹配的网站。

    出现这个问题的原因是:没有用管理员权限运行VS2013. 本来遇到这个问题的时候,不知道什么原因.后来附加到进程调试的时候,提示要以管理员身份运行.才知道啥原因.