防微信聊天气泡图片实现
2016-02-24 22:22
543 查看
先看下效果图
![](https://img-blog.csdn.net/20160224222114810)
防微信实现如图的 图片显示效果。
接上篇博客介绍的图形图片的实现 , 这里通过BitmapSharder来实现这个效果。 主要麻烦的地方就是画出气泡形状的path.
这里设置自定义的属性,方便设置 图片气泡方向与边框颜色, attrs文件如下
自定义ImageView代码如下
再看看布局文件对自定义属性的使用,要指定 名称为
防微信实现如图的 图片显示效果。
接上篇博客介绍的图形图片的实现 , 这里通过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(); } @Override protected 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); } } @Override protected 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>
相关文章推荐
- 社交巨头三国杀:微信、WhatsApp、Line到底有啥区别?
- 微信悄悄升级群聊功能:个人微信营销号的福音
- 突击部队拼多多
- 我是运营,我没有假期
- 如何做到日消息量100万的微信公众号?
- 论微信取消推送功能的可能性(原创)
- 微信的成功,靠的是QQ导流吗?
- 「Linux 中国」2018 微信文章排行榜
- 微信服务号推送模板消息接口
- PHP限制页面只能在微信自带浏览器访问的代码
- HTML5实现微信拍摄上传照片功能
- 微信小程序去哪里找 小程序到底如何使用(附小程序名单)
- 使用jQuery.wechat构建微信WEB应用
- nw.js实现类似微信的聊天软件
- jquery1.8版本使用ajax实现微信调用出现的问题分析及解决办法
- JS版微信6.0分享接口用法分析
- 大家在抢红包,程序员在研究红包算法