Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案

RoundedBitmapDrawable是Android在support v4的扩展包中新增的实现圆角图形的关键类,借助RoundedBitmapDrawable的帮助,可以轻松的以Android标准方式实现圆角图形图象。

写一个简单的例子。

写一个线性布局,竖直方向放三个ImageView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="zhangphil.app.MainActivity"> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" /> <ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" /> <ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" /> </LinearLayout>

然后通过Java代码分别放原始图,圆角图,圆形图作为对比:

package zhangphil.app;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //未做圆角处理的原始图
ImageView image1 = (ImageView) findViewById(R.id.imageView1);
image1.setImageResource(R.drawable.zhangphil); //圆角图
RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil));
roundedDrawable.getPaint().setAntiAlias(true);
roundedDrawable.setCornerRadius(30);
ImageView image2 = (ImageView) findViewById(R.id.imageView2);
image2.setImageDrawable(roundedDrawable); //圆形图
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil);
RoundedBitmapDrawable circleDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil));
circleDrawable.getPaint().setAntiAlias(true);
circleDrawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()));
ImageView image3 = (ImageView) findViewById(R.id.imageView3);
image3.setImageDrawable(circleDrawable);
}
}

代码运行结果:

附录文章:

1,《Android圆形头像图Circle ImageView》链接地址:http://blog.csdn.net/zhangphil/article/details/44960551


2,《Android CustomShapeImageView对图片进行各种样式裁剪:圆形、星形、心形、花瓣形等》链接地址:http://blog.csdn.net/zhangphil/article/details/50119921

Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案的更多相关文章

  1. Android 正 N 边形圆角头像的实现

    卖一下广告,欢迎大家关注我的微信公众号,扫一扫下方二维码或搜索微信号 stormjun94(徐公码字),即可关注. 目前专注于 Android 开发,主要分享 Android开发相关知识和一些相关的优 ...

  2. 解决Android微信支付官方demo运行失败

    Android微信支付官方demo运行失败,在此简单记录一下解决步骤 1.httpclient错误 官方给的demo是eclipse的,打开之后提示httpclient的错误,我知道在as下解决htt ...

  3. Android一个炫酷的树状图组织架构图开源控件实现过程

    Android一个炫酷的树状图组织架构图开源控件 文章目录 [1 简介] [2 效果展示] [3 使用步骤] [4 实现基本布局流程] [5 实现自由放缩及拖动] [6 实现添加删除及节点动画] [7 ...

  4. Android 中shape的使用(圆角矩形)

    一.在res/drawable文件夹下创建一个名为gradient_box的xml文件: <?xml version="1.0" encoding="utf-8&q ...

  5. 蓝牙4.0——Android BLE开发官方文档翻译

    ble4.0开发整理资料_百度文库 http://wenku.baidu.com/link?url=ZYix8_obOT37JUQyFv-t9Y0Sv7SPCIfmc5QwjW-aifxA8WJ4iW ...

  6. Android中实现控件圆角边框

    首先,在drawable文件夹下新建一个xml文件: <?xml version="1.0" encoding="utf-8"?> <shap ...

  7. 【Android】Android布局中实现圆角边框

    设置corners_bg.xml 设置边框圆角可以在drawable-mdpi目录里定义一个xml: <?xml version="1.0" encoding="u ...

  8. android ndk调用OpenGL 实现纹理贴图Texture

    android ndk调用OpenGL 实现纹理贴图Texture 时间 2014-06-25 05:24:39  CSDN博客 原文  http://blog.csdn.net/chrisfxs/a ...

  9. Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图

    Android图表库MPAndroidChart(八)--饼状图的扩展:折线饼状图 我们接着上文,饼状图的扩展,增加折现的说明,来看下我们要实现的效果 因为之前对MPAndroidChart的熟悉,所 ...

随机推荐

  1. 16-6 WEB存储-通讯录实战

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. UML 用例图(转载)

    UML是系统架构设计师考试的一个重要考点,需要考生掌握.但是有些考生,在学习的过程中会有这样的疑问,在敏捷开发时代,UML还有没有必要去学习? UML还是有用的,主要用在设计和分析阶段,但是UML不适 ...

  3. js点击修改按钮后修改

    <button id="click">改变内容</button> <div id="t">要改变的内容</div> ...

  4. 解决ef第一次启动较慢

    protected void Application_Start() { //禁用第一次ef查询对表__MigrationHistory的问题使用了ef的Code first会在第一次ef查询的时候会 ...

  5. sh/bash/csh/Tcsh/ksh/pdksh等shell本质区别

    sh/bash/csh/Tcsh/ksh/pdksh等shell本质区别 1. Shell脚本的书写 在写Shell脚本时,往往第一行要注明用什么解释器来解释这个脚本. 如#!/bin/bash即用/ ...

  6. 全志R58平台调通s5k5eya(RAW+MIPI)

    全志R58平台调通s5k5eya(RAW+MIPI) 2017/5/31 10:30 版本:V1.1 1.前期使用的是s5k5eyx的ISP的固件/tuning 文件 Y:\s5k5eya_r58_d ...

  7. Android开源项目:GifView——Android显示GIF动画

    下载:http://code.google.com/p/gifview/downloads/list 简介:android中现在没有直接显示gif的view,只能通过mediaplay来显示,且还常常 ...

  8. 【Python】第一个爬虫

    import urllib.request import re class DownPic: def __init__(self,url,re_str): self.url = url self.re ...

  9. Android基础TOP3:线性布局的特点,常用属性,及权重值

    线性布局是一种让视图水平或者垂直布排列的布局: 常用属性: androuid:orientation :表示布局方向 取值vertical表示垂直布局 取值horizontal表示水平布局 andro ...

  10. 掌握Spark机器学习库-05-spark中矩阵与向量的使用

    1)介绍 矩阵: Matrix,看做二维表,基本运算(+,-,*,T) 向量: Vectors,方向和大小,基本运算,范数 2)spark中向量的使用(主要使用breeze.linalg) 3)spa ...