防微信聊天气泡图片实现
来源:互联网 发布:pycharm tensorflow 编辑:程序博客网 时间:2024/05/01 02:28
先看下效果图
防微信实现如图的 图片显示效果。
接上篇博客介绍的图形图片的实现 , 这里通过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>
0 0
- 防微信聊天气泡图片实现
- 聊天气泡效果实现
- android实现气泡聊天
- 仿微信聊天气泡效果实现
- 仿微信聊天气泡效果实现
- 仿微信聊天气泡效果实现
- 实现QQ聊天气泡效果
- 利用css实现聊天气泡
- 网页聊天气泡效果实现
- iOS 图片 的 聊天气泡显示 Objective-C 和 Swift,如何实现聊天软件的图片显示的剪裁后的气泡呢!!!
- UIImageResizingModeStretch聊天气泡的图片拉伸
- UIImageResizingModeStretch聊天气泡的图片拉伸
- 气泡聊天
- css3实现气泡效果的聊天框
- android 仿微信聊天气泡效果实现思路
- 【HTML5】实现QQ聊天气泡效果
- 纯CSS实现聊天框小尖角、气泡效果
- 使用css实现QQ聊天气泡效果
- form表单提交数据编码方式和tomcat接受数据解码方式的思考
- Bluetooth
- 设计模式学习笔记——简单工厂模式
- View的事件分发机制分析
- 让Faster R-CNN支持TX1的fp16(half float, float16)特性
- 防微信聊天气泡图片实现
- Queue Java
- JAVASCRIPT入门 之 面向对象+匿名函数
- 基于JavaScriptCore的OC与JS互相调用
- Android消息提示方法介绍
- C/C++知识点整理(1)
- Cocos2d-x 3.x图元绘制
- 计算机网络12--TCP/IP参考模型
- 【SPOJ-ABCPATH】ABC Path【BFS/DFS】