Android实现表情功能
作者:qq_21467035 时间:2023-03-05 06:46:27
本文实例为大家分享了Android实现表情功能的具体代码,供大家参考,具体内容如下
Dialog实现表情评论功能核心问题:
1、如何得到键盘弹起和隐藏状态
2、在于表情和键盘切换时候,防止Dialog抖动
问题1:由于无法获取键盘弹起状态,但是键盘弹起,View尺寸变化,同时被onSizeChanged()调用。
View 源码:
/**
* This is called during layout when the size of this view has changed. If
* you were just added to the view hierarchy, you're called with the old
* values of 0.
*
* @param w Current width of this view.
* @param h Current height of this view.
* @param oldw Old width of this view.
* @param oldh Old height of this view.
*/
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
}
我们可以通过继承View 重写 onSizeChanged方法得到View尺寸变化来判断键盘是否弹起:
int minKeyboardHeight = dm.heightPixels / 4; (屏幕高度1/4)
当 oldh - h > minKeyboardHeight时,键盘弹起
当 h - oldh > minKeyboardHeight时,键盘隐藏
如此即可获取键盘的弹起、隐藏状态 和键盘高度 inputHeight(同时也是表情布局高度) 。
问题2:表情和键盘切换时候,防止Dialog抖动
表情和键盘切换时候,由于DialogViewHeight 高度变化导致的Dialog高度重新计算高度而产生抖动;那么当表情和键盘切换时DialogViewHeight 中间 DialogViewHeight 高度固定不变导致界面抖动。
键盘——>表情:因为当键盘弹起时候,我们已经知道键盘的高度,那么当切换表情时候:(键盘高度==表情高度)
①、 锁高度 DialogViewHeight = CommentView高度 + inputHeight(键盘高度)。锁高重点在于设置 DialogView固定值,同时设置 layoutParams.weight = 0F
②、然后设置表情布局 VISIBLE 和 隐藏键盘
③、释放锁高。释放锁高重点在于设置 DialogViewHeight = LinearLayout.LayoutParams.MATCH_PARENT,同时设置 layoutParams.weight = 1.0F
代码:
//①锁高:
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) CommentView.getLayoutParams();
layoutParams.height = DialogView.getHeight();
layoutParams.weight = 0.0f;
llContentView.setLayoutParams(layoutParams);
//②表情布局显示
EmotionView.setVisibility(View.VISIBLE)
//隐藏键盘
//③释放高度
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) DialogView.getLayoutParams();
layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
layoutParams.weight = 1.0f;
llContentView.setLayoutParams(layoutParams);
表情——>键盘:表情切换键盘其实跟键盘切换表情一样,分三步
①、 锁高度:锁高度 DialogViewHeight = CommentView高度 + inputHeight(键盘高度)。锁高重点在于设置 DialogView固定值,同时设置 layoutParams.weight = 0F
②、然后设置表情布局 GONE 和 弹起键盘
③、释放锁高。释放锁高重点在于设置 DialogViewHeight = LinearLayout.LayoutParams.MATCH_PARENT,同时设置 layoutParams.weight = 1.0F
//①锁高:
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) CommentView.getLayoutParams();
layoutParams.height = DialogView.getHeight();
layoutParams.weight = 0.0f;
llContentView.setLayoutParams(layoutParams);
//②表情布局隐藏
EmotionView.setVisibility(View.GONE)
//显示键盘
//③释放高度
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) DialogView.getLayoutParams();
layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
layoutParams.weight = 1.0f;
llContentView.setLayoutParams(layoutParams);
总结:
1、onSizeChanged方法,重点在于获取键盘的高度。方便后面表情布局高度设置。
2、表情切换主要在于对布局进行锁高和释放高度,来实现表情、键盘切换时候,Dialog布局高度是没有变化。
来源:https://blog.csdn.net/qq_21467035/article/details/116716341
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android.bp语法和使用方法讲解
![](https://img.aspxhome.com/file/2023/7/111717_0s.png)
SpringBoot使用Redis实现分布式锁
java 读取excel文件转换成json格式的实例代码
利用Java连接Hadoop进行编程
![](https://img.aspxhome.com/file/2023/5/77095_0s.png)
c# 引用类型和值类型
基于opencv实现车道线检测
![](https://img.aspxhome.com/file/2023/1/127531_0s.jpg)
Android 吸入动画效果实现分解
![](https://img.aspxhome.com/file/2023/7/137897_0s.gif)
分享一个Android设置圆形图片的特别方法
![](https://img.aspxhome.com/file/2023/7/111687_0s.png)
SpringBoot整合Elasticsearch游标查询的示例代码(scroll)
GraalVM和Spring Native尝鲜一步步让Springboot启动飞起来66ms完成启动
![](https://img.aspxhome.com/file/2023/0/122330_0s.jpg)
android使用DataBinding来设置空状态
![](https://img.aspxhome.com/file/2023/4/88344_0s.gif)
Java如何基于command调用openssl生成私钥证书
![](https://img.aspxhome.com/file/2023/6/113466_0s.png)