先看下效果图

防微信实现如图的 图片显示效果。

接上篇博客介绍的图形图片的实现 , 这里通过BitmapSharder来实现这个效果。 主要麻烦的地方就是画出气泡形状的path.

这里设置自定义的属性,方便设置 图片气泡方向与边框颜色, attrs文件如下

<resources><declare-styleable name="ChatImageView"><attr name="orientation"><enum name="left" value="0"></enum><enum name="right" value="1"></enum></attr><attr name="borderColor" format="color" /></declare-styleable>
</resources>

自定义ImageView代码如下

public class ChatImageView extends ImageView {private Bitmap srcBitmap;private Paint paint;private BitmapShader mBitmapShader;private Matrix mMatrix;private int width;private int height;private Paint borderPaint; //边框的画笔private int mOrientation; //方向private int mborderColor; //边框颜色public ChatImageView(Context context) {super(context);init();}public ChatImageView(Context context, AttributeSet attrs) {super(context, attrs);//获取自定义属性值TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.ChatImageView);mOrientation = ta.getInt(R.styleable.ChatImageView_orientation, 0);mborderColor = ta.getColor(R.styleable.ChatImageView_borderColor, Color.GRAY);ta.recycle();init();}public ChatImageView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}private void init() {paint = new Paint();paint.setAntiAlias(true);borderPaint = new Paint();borderPaint.setColor(mborderColor);borderPaint.setAntiAlias(true);borderPaint.setStyle(Paint.Style.STROKE);borderPaint.setStrokeWidth(1);srcBitmap = ((BitmapDrawable) getDrawable()).getBitmap();mBitmapShader = new BitmapShader(srcBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);mMatrix = new Matrix();}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);if (getWidth() > 0 && getHeight() > 0) {width = getWidth();height = getHeight();int mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());//设置缩放int bSize = Math.min(srcBitmap.getWidth(), srcBitmap.getHeight());float scale = mWidth * 1.0f / bSize;mMatrix.setScale(scale, scale);mBitmapShader.setLocalMatrix(mMatrix);}}@Overrideprotected void onDraw(Canvas canvas) {paint.setShader(mBitmapShader);Path path = null;if (mOrientation == 0) {path = getLeftPath();} else {path = getRightPath();}canvas.drawPath(path, paint);//画边框canvas.drawPath(path, borderPaint);}/*** 画出左朝向的path** @return*/private Path getLeftPath() {Path path = new Path();path.moveTo(40, 0);path.lineTo(width - 20, 0);RectF oval = new RectF(width - 40, 0, width, 40);path.arcTo(oval, 270, 90, false); //false表示不闭口path.lineTo(width, height - 20);oval = new RectF(width - 40, height - 40, width, height);path.arcTo(oval, 0, 90, false);path.lineTo(40, height);oval = new RectF(20, height - 40, 60, height);path.arcTo(oval, 90, 90, false);path.lineTo(20, 60);path.lineTo(0, 20);path.lineTo(20, 20);oval = new RectF(20, 0, 60, 40);path.arcTo(oval, 180, 90, false);path.close();//封闭return path;}/*** 画出右朝向的path** @return*/private Path getRightPath() {Path path = new Path();path.moveTo(20, 0);path.lineTo(width - 40, 0);RectF oval = new RectF(width - 60, 0, width - 20, 40);path.arcTo(oval, 270, 90, false);path.lineTo(width, 20);path.lineTo(width - 20, 60);path.lineTo(width - 20, height - 20);oval = new RectF(width - 60, height - 40, width - 20, height);path.arcTo(oval, 0, 90, false);path.lineTo(20, height);oval = new RectF(0, height - 40, 40, height);path.arcTo(oval, 90, 90, false);path.lineTo(0, 20);oval = new RectF(0, 0, 40, 40);path.arcTo(oval, 180, 90, false);path.close();//封闭return path;}
}

再看看布局文件对自定义属性的使用,要指定 名称为

“http://schemas.android.com/apk/res-auto”的名称空间
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:civ="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><wei.jiang.selfview.imageview.ChatImageView
        android:layout_width="200dp"android:layout_height="100dp"android:layout_centerInParent="true"android:src="@drawable/pic2"civ:borderColor="@color/colorPrimaryDark"civ:orientation="left" /></RelativeLayout>

防微信聊天气泡图片实现相关推荐

  1. UIImageView 显示不规则图片,类似微信聊天气泡图片展示效果

    微信聊天气泡,图片充满显示,效果: -(void)layoutSubviews { [super layoutSubviews]; CAShapeLayer * _maskLayer = [CASha ...

  2. android 仿微信聊天气泡显示图片,仿微信聊天气泡 图片尖角 按下变暗

    实现微信气泡图片尖角 //-------------gen corner bitmap flow------------------------ //load the bg: .9.png which ...

  3. android 图片气泡,关于实现微信聊天气泡里显示图片解决方案

    关于实现微信聊天气泡里显示图片 这是微信的效果,气泡中的图片没有边距 这是我的效果,背景气泡是用.9.png图片组成的一个selector,气泡中的图片有边距  如何才能像微信那样没有边矩呢? --- ...

  4. 微信聊天气泡随意换,一键制作超级简单,让聊天不再枯燥!

    除了QQ微信也可以制作聊天气泡,今天小编给大家分享一款微信小程序,只需简单几步就可以制作出个性十足的聊天气泡效果了,赶快一起来看看吧! 这款小程序中提供了丰富的气泡模板,其中包括游戏.明星以及动漫模块 ...

  5. Android气泡弹幕,Android弹幕实现:基于B站弹幕开源系统(7)QQ、微信聊天气泡样式的弹幕...

    Android弹幕实现:基于B站弹幕开源系统(7)QQ.微信聊天气泡样式的弹幕 在附录文章得基础上,改进普通文本弹幕,实现一种特殊效果的文本弹幕,像QQ.微信一样的带有气泡背景的弹幕.实现的重点是在S ...

  6. iOS实现类似微信聊天气泡

    1. 说明 要实现类似微信聊天气泡功能,主要有以下几个主要技术点需要实现: 背景图图拉伸后不变形 UILabel自适应宽度和高度 2. 背景图图拉伸后不变形 要实现背景图拉升不变形,就要设置只让指定部 ...

  7. 小程序 背景图 repeat_简单可爱的微信聊天背景图片

    简单可爱的微信聊天背景图片 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  8. 聊天气泡图片的动态拉伸、适配与镜像

    聊天气泡图片的动态拉伸.适配与镜像 前情提要 创建.9.png格式的图片 从资源文件夹加载.9.png图片 从本地文件加载".9.png"图片 项目痛点 进阶探索 iOS中的方式 ...

  9. 微信视频气泡 android,变变微信聊天气泡

    变变微信聊天气泡是一款非常好用的气泡美化软件,让你的聊天框看起来更加个性化,不需要ROOT即可完成气泡设计,更具趣味性,喜欢的小伙们快来下载试试吧! 软件详情 让气泡在你的指尖跳跃吧! 变变聊天气泡( ...

最新文章

  1. Java Web开发中路径问题小结
  2. javascript 中的getter,setter
  3. sql语言和php,SQL语言快速入门(三)_php
  4. 什么是软件 什么是硬件
  5. SyntaxError: Missing parentheses in call to 'print' 这个错误原因是Python版本问题
  6. 深入浅出数据分析 - 数据分析引言
  7. 全国雾霾大数据,哪里才是桃花源?
  8. 高速PCB设计的一些注意事项
  9. Python Excel 批量生成二维码
  10. TC软件概要设计文档(手机群控)
  11. mac 运行android模拟器速度慢,Android模拟器速度慢 启动时间长的解决办法
  12. Windows server 2008 R2安装详细教程
  13. 阿里p9 Python工程师,进阶书籍推荐
  14. 如何用matlab绘制心形线,心形线的matlab程序
  15. 美团后端2020.4.23笔试题目
  16. linux磁盘管理相关命令
  17. 关不掉,新版微信这功能引用户怨声载道...
  18. hive之窗口函数理解与实践
  19. 百度金融独立分拆,不过朱光透露的这个信息更值得关注
  20. 千兆以太网工程(高速接口)

热门文章

  1. 华为高端麒麟芯片或将绝版,余承东:应对方案已出!【附演讲全文】
  2. uni-appの发展和应用
  3. uniapp云开发微信小程序 云函数配置
  4. yapi接口测试--自定义脚本编写(高级mock)
  5. 一加ace2v和2区别对比 一加ace2和一加ace2v哪个好
  6. 【微信】微信,是一个生活方式
  7. TeamViewer远程服务器管理工具使用哪些端口
  8. Flash常见问题与解答
  9. 头腾大战再升级 字节跳动做游戏的胜算有多少?
  10. python 将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。