Android表情功能
qq_21467035 人气:0Dialog实现表情评论功能核心问题:
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布局高度是没有变化。
加载全部内容