Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现
Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现
LayerDrawable实现的结果和附录文章1,2,3中的layer-list一致。我写个例子,这次使用LayerDrawable把附录文章4的功能再次实现走通一遍。
写一个布局,简单放一个正方形的View:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:orientation="vertical"> <View
android:id="@+id/view"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true" /> </RelativeLayout>
然后在上层写Java代码:
package zhangphil.app; import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
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.view.View; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); int width = 50; //最外部的白色环形边框
OvalShape ovalShape0 = new OvalShape();
ShapeDrawable drawable0 = new ShapeDrawable(ovalShape0);
drawable0.getPaint().setColor(Color.WHITE);
drawable0.getPaint().setStyle(Paint.Style.FILL);
drawable0.getPaint().setAntiAlias(true);
drawable0.getPaint().setStrokeWidth(width); //黄色边框
OvalShape ovalShape1 = new OvalShape();
ShapeDrawable drawable1 = new ShapeDrawable(ovalShape1);
drawable1.getPaint().setColor(Color.YELLOW);
drawable1.getPaint().setStyle(Paint.Style.FILL);
drawable1.getPaint().setAntiAlias(true);
drawable1.getPaint().setStrokeWidth(width); //红色边框
OvalShape ovalShape2 = new OvalShape();
ShapeDrawable drawable2 = new ShapeDrawable(ovalShape2);
drawable2.getPaint().setColor(Color.RED);
drawable2.getPaint().setStyle(Paint.Style.FILL);
drawable2.getPaint().setAntiAlias(true);
drawable2.getPaint().setStrokeWidth(width); //最里面的图像
RoundedBitmapDrawable drawable3 = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil));
drawable3.setCircular(true);
drawable3.setAntiAlias(true); Drawable[] layers = new Drawable[4];
layers[0] = drawable0;
layers[1] = drawable1;
layers[2] = drawable2;
layers[3] = drawable3; LayerDrawable layerDrawable = new LayerDrawable(layers); //针对每一个图层进行填充,使得各个圆环之间相互有间隔,否则就重合成一个了。
layerDrawable.setLayerInset(0, width, width, width, width);
layerDrawable.setLayerInset(1, width * 2, width * 2, width * 2, width * 2);
layerDrawable.setLayerInset(2, width * 3, width * 3, width * 3, width * 3);
layerDrawable.setLayerInset(3, width * 4, width * 4, width * 4, width * 4); final View view = findViewById(R.id.view);
view.setBackgroundDrawable(layerDrawable);
}
}
代码运行结果:
最里面的图像是我csdn博客的头像。
需要注意的是,我在写xml布局时候,特意写了一个正方形的View,假设这个View不是正方形,那么在上层java代码中使用OvalShape绘制图形则会因为屏幕的宽高变化成为椭圆而非圆。因此,作为一点儿经验,如果要在项目开发中制作圆形,务必保持正方形的宽高比。
附录:
1,《Android layer-list(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/517209244
2,《Android layer-list:联合shape(2)》链接地址:http://blog.csdn.net/zhangphil/article/details/51721283
3,《Android layer-list(3)》链接地址:http://blog.csdn.net/zhangphil/article/details/51721816
4,《Android ImageView加载圆形图片且同时绘制圆形图片的外部边缘边线及边框》链接地址:http://blog.csdn.net/zhangphil/article/details/51944262
5,《Android ShapeDrawable之OvalShape、RectShape、PaintDrawable、ArcShape》链接地址:http://blog.csdn.net/zhangphil/article/details/52025152
6,《Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案》链接地址:http://blog.csdn.net/zhangphil/article/details/51829650
Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现的更多相关文章
- Android ImageView加载圆形图片且同时绘制圆形图片的外部边缘边线及边框
Android ImageView加载圆形图片且同时绘制圆形图片的外部边缘边线及边框 在Android早期的开发中,如果涉及到圆形图片的处理,往往需要借助于第三方的实现,见附录文章1,2.And ...
- Android Glide加载图片时转换为圆形、圆角、毛玻璃等图片效果
Android Glide加载图片时转换为圆形.圆角.毛玻璃等图片效果 附录1简单介绍了Android开源的图片加载框架.在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬 ...
- Android 高清加载巨图方案, 拒绝压缩图片
源地址:http://blog.csdn.net/lmj623565791/article/details/49300989 一.概述 距离上一篇博客有段时间没更新了,主要是最近有些私事导致的,那么就 ...
- Android框架 加载图片 库 Picasso 的使用简介
0 说明 现在Android开源库中有许多图片加载框架,本文以picasso为例,总结下开发过程中的一些优化经验,使用的picasso版本如下 compile 'com.squareup.picass ...
- 演化理解 Android 异步加载图片
原文:http://www.cnblogs.com/ghj1976/archive/2011/05/06/2038738.html#3018499 在学习"Android异步加载图像小结&q ...
- 演化理解 Android 异步加载图片(转)
演化理解 Android 异步加载图片(转)http://www.cnblogs.com/CJzhang/archive/2011/10/20/2218474.html
- Android高效加载大图、多图解决方案,有效避免程序内存溢出现象
好久没有写博客了,今天就先写一个小的关于在Android中加载大图如何避免内存溢出的问题. 后面会写如何使用缓存技术的核心类,android.support.v4.util.LruCache来加载图片 ...
- android动态加载
转载自: http://www.cnblogs.com/over140/archive/2012/03/29/2423116.html http://www.cnblogs.com/over140/a ...
- Android ListView加载更多
先看效果: ListView的footer布局: <?xml version="1.0" encoding="utf-8"?> <Relati ...
随机推荐
- h5-18-文件上传
参考博客地址:https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects 参考博客地址:http: ...
- mysql 如何创建一个简单的存储过程
1 用mysql客户端登入2 选择数据库 mysql>use test3 查询当前数据库有哪些存储过程 mysql>show procedure status where Db='test ...
- 基于CentOS 7.2个人网盘的实现
首先使用YUM安装依赖环境: [root@sishen ~]#yum install python python-setuptools python-imaging python-ldap pytho ...
- archsummit_bj2016
http://bj2016.archsummit.com/schedule 大会日程 2016年12月02日,星期五 7:45-9:00 签到 8:45-9:00 开始入场 9:00-9:30 开场致 ...
- Android开发二维码之坑
之前一直做的是.NET开发用的是C#语言,近段时间由于做一个APP这才用上了java,在二维码扫描整合到APP里面遇到扫描二维码之后没有返回值,经过反复的尝试最后终于拿到了返回值,之后觉得很有必要记录 ...
- Android图片压缩上传(二)
之前有用到libjpeg,还是有一定的局限性,最近用了一个新的方式,效果还是挺不错,随着作者的版本更新,Bug也随之变少,目前项目中运用已上线. 1.之前的方式Android图片压缩,不失真,上线项目 ...
- FileZilla Server 端设置passive模式注意事项
1,需求和问题的产生 实践中需要分布在各地的各个客户端向云端服务器上传文件,因此在阿里云服务器上安装了FileZilla Server软件作为文件FTP服务端. 客户端程序采用FTP方式向服务端传输文 ...
- sccm系统更新补丁后服务无法正常启动
更新完补丁后这几个应用无法启动,最后发现计算机丢失msvcp120.dll 文件,查询相关资料发现安装vcredist 2013 从官网下载Visual C++ Redistributable Pac ...
- 微信小程序开发系列四:微信小程序之控制器的初始化逻辑
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 这个教程的前两篇文章,介绍了如何 ...
- SQLite – LIMIT子句
SQLite - LIMIT子句 SQLite LIMIT子句是用来限制SELECT语句返回的数据量. 语法: SELECT语句.LIMIT子句的基本语法如下: SELECT column1, col ...