Skip to content

Commit

Permalink
增加颜文字高度自适应特性
Browse files Browse the repository at this point in the history
  • Loading branch information
HelloVass committed Jul 5, 2016
1 parent 933df73 commit 157c344
Showing 1 changed file with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public class FaceTextInputLayout extends LinearLayout {

private int mFaceTextViewRightMargin;

private int mFaceTextViewHeight;

private LinearLayout.LayoutParams mFaceTextContainerLayoutParams;

private TextView mTargetFaceTextView; // 用于测量颜文字长度的“TextView”
Expand Down Expand Up @@ -81,17 +79,25 @@ public void setOnFaceTextClickListener(OnFaceTextClickListener onFaceTextClickLi
}

public void updateUI() {
// 如果用户未设置“颜文字source”,辣么 return
if (mFaceTextProvider == null) {
return;
}

// TODO: 生成页面在主线程,需要放到非 UI线程
List<RecyclerView> allPageList = generateAllPage();
mMyPagerAdapter.setFaceTextInputPageList(allPageList);
mViewPager.setOffscreenPageLimit(mMyPagerAdapter.getCount());
mViewPager.setAdapter(mMyPagerAdapter);
mDotViewLayout.setViewPager(mViewPager);
// 利用 View 的 post 方法机智地获取 View 的高度
post(new Runnable() {
@Override public void run() {

if (mFaceTextProvider == null) { // 如果用户未设置“颜文字source”,抛出异常
throw new IllegalStateException("mFaceTextProvider can't be null !!!");
}

mFaceTextContainerLayoutParams = generateFaceTextContainerLayoutParams();

// TODO: 生成页面在主线程,需要放到非 UI线程
List<RecyclerView> allPageList = generateAllPage();
mMyPagerAdapter.setFaceTextInputPageList(allPageList);
mViewPager.setOffscreenPageLimit(mMyPagerAdapter.getCount());
mViewPager.setAdapter(mMyPagerAdapter);
mDotViewLayout.setViewPager(mViewPager);
}
});
}

@Override protected void onDetachedFromWindow() {
Expand Down Expand Up @@ -144,12 +150,6 @@ private void applyXMLAttributes(AttributeSet attrs, int defStyleAttr, int defSty
typedArray.getResourceId(R.styleable.FaceTextInputLayout_faceTextViewLeftMargin,
DensityUtil.dip2px(getContext(), 2));

mFaceTextViewHeight =
typedArray.getResourceId(R.styleable.FaceTextInputLayout_faceTextViewLeftMargin,
DensityUtil.dip2px(getContext(), 48));

mFaceTextContainerLayoutParams = generateFaceTextContainerLayoutParams();

typedArray.recycle();
}

Expand Down Expand Up @@ -251,13 +251,25 @@ private RecyclerView generateEachPage(List<List<FaceText>> faceTextList) {
* 生成每个“颜文字” item 对应的 layoutParams
*/
private LinearLayout.LayoutParams generateFaceTextContainerLayoutParams() {

LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(0, mFaceTextViewHeight, 1.0f);
new LinearLayout.LayoutParams(0, calculateFaceTextViewHeight(), 1.0F);
layoutParams.leftMargin = mFaceTextViewLeftMargin;
layoutParams.rightMargin = mFaceTextViewRightMargin;
return layoutParams;
}

/**
* 计算“颜文字”的高度,父容器的高度减去竖直方向上的 padding(2 * 4dp)和底部的留白(8dp)
*
* @return “颜文字item”的高度
*/
private int calculateFaceTextViewHeight() {
return getMeasuredHeight() / PAGE_MAX_LINE_NUM
- 2 * DensityUtil.dip2px(getContext(), 4.0F)
- DensityUtil.dip2px(getContext(), 8.0F);
}

/**
* 能否在单行中摆放
*
Expand Down Expand Up @@ -294,7 +306,7 @@ private TextView inflateTargetFaceTextView() {
}

/**
* 测量 颜文字 的长度
* 测量颜文字的长度
*/
private int measureFaceTextWidth(TextView faceTextView, FaceText faceText) {
if (faceTextView == null || faceText == null) {
Expand Down

0 comments on commit 157c344

Please sign in to comment.