Android實(shí)現(xiàn)表情功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)表情功能的具體代碼,供大家參考,具體內(nèi)容如下
Dialog實(shí)現(xiàn)表情評(píng)論功能核心問題:
1、如何得到鍵盤彈起和隱藏狀態(tài)
2、在于表情和鍵盤切換時(shí)候,防止Dialog抖動(dòng)
問題1:由于無(wú)法獲取鍵盤彈起狀態(tài),但是鍵盤彈起,View尺寸變化,同時(shí)被onSizeChanged()調(diào)用。
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尺寸變化來(lái)判斷鍵盤是否彈起:
int minKeyboardHeight = dm.heightPixels / 4; (屏幕高度1/4)
當(dāng) oldh - h > minKeyboardHeight時(shí),鍵盤彈起
當(dāng) h - oldh > minKeyboardHeight時(shí),鍵盤隱藏
如此即可獲取鍵盤的彈起、隱藏狀態(tài) 和鍵盤高度 inputHeight(同時(shí)也是表情布局高度) 。
問題2:表情和鍵盤切換時(shí)候,防止Dialog抖動(dòng)
表情和鍵盤切換時(shí)候,由于DialogViewHeight 高度變化導(dǎo)致的Dialog高度重新計(jì)算高度而產(chǎn)生抖動(dòng);那么當(dāng)表情和鍵盤切換時(shí)DialogViewHeight 中間 DialogViewHeight 高度固定不變導(dǎo)致界面抖動(dòng)。
鍵盤——>表情:因?yàn)楫?dāng)鍵盤彈起時(shí)候,我們已經(jīng)知道鍵盤的高度,那么當(dāng)切換表情時(shí)候:(鍵盤高度==表情高度)
①、 鎖高度 DialogViewHeight = CommentView高度 + inputHeight(鍵盤高度)。鎖高重點(diǎn)在于設(shè)置 DialogView固定值,同時(shí)設(shè)置 layoutParams.weight = 0F
②、然后設(shè)置表情布局 VISIBLE 和 隱藏鍵盤
③、釋放鎖高。釋放鎖高重點(diǎn)在于設(shè)置 DialogViewHeight = LinearLayout.LayoutParams.MATCH_PARENT,同時(shí)設(shè)置 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);
表情——>鍵盤:表情切換鍵盤其實(shí)跟鍵盤切換表情一樣,分三步
①、 鎖高度:鎖高度 DialogViewHeight = CommentView高度 + inputHeight(鍵盤高度)。鎖高重點(diǎn)在于設(shè)置 DialogView固定值,同時(shí)設(shè)置 layoutParams.weight = 0F
②、然后設(shè)置表情布局 GONE 和 彈起鍵盤
③、釋放鎖高。釋放鎖高重點(diǎn)在于設(shè)置 DialogViewHeight = LinearLayout.LayoutParams.MATCH_PARENT,同時(shí)設(shè)置 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);
總結(jié):
1、onSizeChanged方法,重點(diǎn)在于獲取鍵盤的高度。方便后面表情布局高度設(shè)置。
2、表情切換主要在于對(duì)布局進(jìn)行鎖高和釋放高度,來(lái)實(shí)現(xiàn)表情、鍵盤切換時(shí)候,Dialog布局高度是沒有變化。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于Android開發(fā)支持表情的實(shí)現(xiàn)詳解
- Android開發(fā)技巧之像QQ一樣輸入文字和表情圖像
- Android自帶emoji表情的使用方法詳解
- Android編程實(shí)現(xiàn)QQ表情的發(fā)送和接收完整實(shí)例(附源碼)
- 完整的Android表情功能處理方案
- Android編程開發(fā)實(shí)現(xiàn)TextView顯示表情圖像和文字的方法
- 詳解Android過濾emoji表情正則表達(dá)式
- android高仿微信表情輸入與鍵盤輸入代碼(詳細(xì)實(shí)現(xiàn)分析)
- Android EdText編輯框禁止輸入表情符號(hào)(使用正則表達(dá)式)
- Android輸入框添加emoje表情圖標(biāo)的實(shí)現(xiàn)代碼
相關(guān)文章
Android懸浮按鈕點(diǎn)擊返回頂部FloatingActionButton
這篇文章主要為大家詳細(xì)介紹了Android懸浮按鈕FloatingActionButton點(diǎn)擊回到頂部的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android控件之使用ListView實(shí)現(xiàn)時(shí)間軸效果
這篇文章主要介紹了Android基礎(chǔ)控件之使用ListView實(shí)現(xiàn)時(shí)間軸效果的相關(guān)資料,本文是以查看物流信息為例,給大家介紹了listview時(shí)間軸的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-11-11Android Fragment(動(dòng)態(tài),靜態(tài))碎片詳解及總結(jié)
這篇文章主要介紹了Android Fragment詳解及總結(jié)的相關(guān)資料,這里對(duì)Android Fragment 動(dòng)態(tài),靜態(tài)碎片進(jìn)行了整理總結(jié),需要的朋友可以參考下2016-12-12Java語(yǔ)言讀取配置文件config.properties的方法講解
今天小編就為大家分享一篇關(guān)于Java語(yǔ)言讀取配置文件config.properties的方法講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03android studio安裝時(shí) AVD出現(xiàn)問題如何快速解決
這篇文章主要介紹了安裝android studio時(shí) AVD出現(xiàn)問題如何快速處理,其實(shí)解決方法也很簡(jiǎn)單,文中通過截圖的形式給大家及時(shí)的非常詳細(xì),對(duì)大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03