从Android的autofittextview库变量:缩放发出奇怪的行为 [英] Variant from android-autofittextview library : scaling makes strange behaviour
问题描述
所以要告诉你到底发生了什么,让我先解释一下背景。
我跟着这个教程:
的https://viksaaskool.word$p$pss.com/2014/11/16/using-auto-resize-to-fit-edittext-in-android/
这基本上是一个简单autoresizeEditText基于autoFitTextView库( https://github.com/grantland / Android的autofittextview )。
我想什么来实现:当我点击一个按钮,整个布局被放大时,我模拟在EDITTEXT点击,让用户输入一些文字
我到目前为止已经实现:好,pretty太多的一切,节选,它仅适用于某些设备上,而不是在别人
。仅供参考,这里有两个GIF动画那将说明我说的是:
GOOD行为(Nexus 5的API 21): http://giphy.com/gifs/xTiTnH1hyuJzjnDl6w
问题行为(的Nexus 6 API 22): http://giphy.com/gifs / 3-1-2-3o85xDB4Wyve1jh0XK
正如你所看到的,变焦是好的,然后输入文字是罚款,直至在某些情况下,文本不会以后有什么似乎是未缩放EDITTEXT的右手边出现。但只要文字大小要改变(或我缩小视图),一切都恢复了正常。
所以我的问题:任何人都会有什么可以做这种奇怪的行为的想法?
任何人都遇到过这个问题,无论是在autoFitTextView或它的一个延伸?
在此先感谢!
PS:我可以张贴一些code,但既库提供+链接和是pretty长,我preFER这里不张贴他们,因为他们是基本相同的。我可以提供,虽然是制作动画的意见和显示键盘(即使我不知道它的相关)的调用:
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
MVIEW = inflater.inflate(R.layout.add_message_layout,NULL);
mMessageEditText =(AutoResizeEditText)mView.findViewById(R.id.messageEditText);
mMessageEditText.setEnabled(假);
mMessageEditText.setEnableSizeCache(假);
mMessageEditText.setMovementMethod(NULL);
mMessageEditText.setInterface(本);
mMessageEditText.setMinTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,MIN_SP_TEXT_SIZE,getResources()getDisplayMetrics()));
mMessageEditText.setMaxHeight((int)的DisplayUtils.convertDpToPixel(getActivity()getApplicationContext(),mMessageEditText.getMeasuredHeight())。);
mMessageEditText.setLines(MAX_LINES_COUNT);
mMessageEditText.addTextChangedListener(新TextWatcher(){
@覆盖
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){
mLastMessage = mMessageEditText.getText()的toString()。
} @覆盖
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
如果(mMessageEditText.getLineCount()> MAX_LINES_COUNT){
//如果用户试图输入比预期更多的文字!
mMessageEditText.setText(mLastMessage);
mMessageEditText.setSelection(mLastMessage.length());
Toast.makeText(getActivity()getApplicationContext(),极限达,Toast.LENGTH_SHORT).show();
}
} @覆盖
公共无效afterTextChanged(编辑S){
mLastMessage = mMessageEditText.getText()的toString()。
mMessageEditText.invalidate();
}
}); mAddMessageButton =(RelativeLayout的)mView.findViewById(R.id.rlAddMessage);
mAddMessageButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
setupAnimations();
animZoomIn();
}
}); mCardLayout =(的LinearLayout)mView.findViewById(R.id.rearMainCardLayout);
返回(MVIEW);
}私人无效setupAnimations(){
mPivotX = mMessageEditText.getPivotX()/ mCardLayout.getMeasuredHeight();
mPivotY = mMessageEditText.getPivotY()/ mCardLayout.getMeasuredWidth(); mZoomIn =新ScaleAnimation(1.0F,1.3F,1.0F,1.3F,mPivotX,mPivotY);
mZoomIn.setDuration(1000);
mZoomIn.setFillAfter(真); mSlideUp =新TranslateAnimation(0.0,100.0f,0.0,-100.0f);
mSlideUp.setDuration(1000);
mSlideUp.setFillAfter(真); mEnterAnimation =新AnimationSet(真);
mEnterAnimation.setInterpolator(新LinearInterpolator());
mEnterAnimation.addAnimation(mZoomIn);
mEnterAnimation.addAnimation(mSlideUp);
mEnterAnimation.setFillAfter(真);
mEnterAnimation.setAnimationListener(新Animation.AnimationListener(){
@覆盖
公共无效onAnimationStart(动画动画){ mBeginX = mMessageEditText.getLeft();
mBeginY = mMessageEditText.getTop();
} @覆盖
公共无效onAnimationEnd(动画动画){
mMessageEditText.set pressed(真); //的EditText将得到凸显
mMessageEditText.setFocusable(真);
mMessageEditText.setFocusableInTouchMode(真);
mMessageEditText.setTextIsSelectable(真);
mMessageEditText.requestFocus();
mMessageEditText.invalidate(); mMessageEditText.setEnabled(真);
showKeyboard(getActivity()getApplicationContext(),mMessageEditText);
} @覆盖
公共无效onAnimationRepeat(动画动画){}
}); mZoomOut =新ScaleAnimation(1.3F,1.0F,1.3F,1.0F,mPivotX,mPivotY);
mZoomOut.setDuration(1000);
mZoomOut.setFillAfter(真); mSlideDown =新TranslateAnimation(100.0f,0.0,-100.0f,0.0);
mSlideDown.setDuration(1000);
mSlideDown.setFillAfter(真); mExitAnimation =新AnimationSet(真);
mExitAnimation.setInterpolator(新LinearInterpolator());
mExitAnimation.addAnimation(mZoomOut);
mExitAnimation.addAnimation(mSlideDown);
mExitAnimation.setFillAfter(真);
mExitAnimation.setAnimationListener(新Animation.AnimationListener(){
@覆盖
公共无效onAnimationStart(动画动画){} @覆盖
公共无效onAnimationEnd(动画动画){
mMessageEditText.invalidate();
} @覆盖
公共无效onAnimationRepeat(动画动画){}
});
}私人无效animZoomIn(){
mCardLayout.startAnimation(mEnterAnimation);
}私人无效animZoomOut(){
mCardLayout.startAnimation(mExitAnimation);
}私有静态无效closeKeyboard(上下文C,的IBinder windowToken){
InputMethodManager经理=(InputMethodManager)c.getSystemService(Context.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(windowToken,0);
}私有静态无效showKeyboard(上下文C,文字的EditText){
InputMethodManager经理=(InputMethodManager)c.getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(文字,InputMethodManager.SHOW_IMPLICIT);
}
好了,感谢@android开发谁指出了这个问题,这是由旧版本我是用动画的造成的。
因此,通过替换
mZoomOut =新ScaleAnimation(1.3F,1.0F,1.3F,1.0F,mPivotX,mPivotY);
mZoomOut.setDuration(1000);
mZoomOut.setFillAfter(真);mSlideDown =新TranslateAnimation(100.0f,0.0,-100.0f,0.0);
mSlideDown.setDuration(1000);
mSlideDown.setFillAfter(真);mExitAnimation =新AnimationSet(真);
mExitAnimation.setInterpolator(新LinearInterpolator());
mExitAnimation.addAnimation(mZoomOut);
mExitAnimation.addAnimation(mSlideDown);
mExitAnimation.setFillAfter(真);
mExitAnimation.setAnimationListener(新Animation.AnimationListener(){
@覆盖
公共无效onAnimationStart(动画动画){} @覆盖
公共无效onAnimationEnd(动画动画){
mMessageEditText.invalidate();
} @覆盖
公共无效onAnimationRepeat(动画动画){}
});
mCardLayout.startAnimation(mEnterAnimation);
在新的:
<$p$p><$c$c>mCardLayout.animate().translationX(0.0f).translationY(0.0f).scaleX(1.0f).scaleY(1.0f).setDuration(1000).withEndAction(new可运行(){@覆盖
公共无效的run(){
mMessageEditText.invalidate();
mIsViewZoomed = FALSE;
}
});
解决一切。
So to tell you exactly what's going on, let me explain the context first.
I followed this tutorial : https://viksaaskool.wordpress.com/2014/11/16/using-auto-resize-to-fit-edittext-in-android/
which is basically a simple autoresizeEditText, based on the autoFitTextView library ( https://github.com/grantland/android-autofittextview ).
What I want to achieve : When I click on a button, the entire layout is zoomed in, and I simulate a click on the editText to let the user enter some text.
What I have been achieving so far : well, pretty much everything, excepts that it only works on some devices, and not on others.
FYI, here are two animated gifs that'll illustrate what I'm talking about :
GOOD BEHAVIOUR(Nexus 5 API 21) : http://giphy.com/gifs/xTiTnH1hyuJzjnDl6w
BAD BEHAVIOUR(Nexus 6 API 22) : http://giphy.com/gifs/3-1-2-3o85xDB4Wyve1jh0XK
As you can see, zooming is fine, then entering text is fine until, in some cases, the text won't appear after what seems to be the right-hand-side of the unzoomed editText. But as soon as the text size has to be changed (or I zoom out the views), everything goes back to normal.
So my question : anyone would have an idea of what could make this strange behaviour? Anyone encountered this problem, either on the autoFitTextView or an extension of it?
Thanks in advance!
PS : I could post some code, but as both libraries + links are provided and are pretty long, I prefer not posting them here, as they're basically the same. What I can provide though is the calls made to animate the views and show the keyboard (even if I'm not sure it's related) :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.add_message_layout, null);
mMessageEditText = (AutoResizeEditText) mView.findViewById(R.id.messageEditText);
mMessageEditText.setEnabled(false);
mMessageEditText.setEnableSizeCache(false);
mMessageEditText.setMovementMethod(null);
mMessageEditText.setInterface(this);
mMessageEditText.setMinTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, MIN_SP_TEXT_SIZE, getResources().getDisplayMetrics()));
mMessageEditText.setMaxHeight((int) DisplayUtils.convertDpToPixel(getActivity().getApplicationContext(), mMessageEditText.getMeasuredHeight()));
mMessageEditText.setLines(MAX_LINES_COUNT);
mMessageEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
mLastMessage = mMessageEditText.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (mMessageEditText.getLineCount() > MAX_LINES_COUNT) {
//If user tries to type more text than expected !
mMessageEditText.setText(mLastMessage);
mMessageEditText.setSelection(mLastMessage.length());
Toast.makeText(getActivity().getApplicationContext(), "Limit Reached", Toast.LENGTH_SHORT).show();
}
}
@Override
public void afterTextChanged(Editable s) {
mLastMessage = mMessageEditText.getText().toString();
mMessageEditText.invalidate();
}
});
mAddMessageButton = (RelativeLayout) mView.findViewById(R.id.rlAddMessage);
mAddMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setupAnimations();
animZoomIn();
}
});
mCardLayout = (LinearLayout) mView.findViewById(R.id.rearMainCardLayout);
return (mView);
}
private void setupAnimations() {
mPivotX = mMessageEditText.getPivotX() / mCardLayout.getMeasuredHeight();
mPivotY = mMessageEditText.getPivotY() / mCardLayout.getMeasuredWidth();
mZoomIn = new ScaleAnimation(1.0f, 1.3f, 1.0f, 1.3f, mPivotX, mPivotY);
mZoomIn.setDuration(1000);
mZoomIn.setFillAfter(true);
mSlideUp = new TranslateAnimation(0.0f, 100.0f, 0.0f, -100.0f);
mSlideUp.setDuration(1000);
mSlideUp.setFillAfter(true);
mEnterAnimation = new AnimationSet(true);
mEnterAnimation.setInterpolator(new LinearInterpolator());
mEnterAnimation.addAnimation(mZoomIn);
mEnterAnimation.addAnimation(mSlideUp);
mEnterAnimation.setFillAfter(true);
mEnterAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
mBeginX = mMessageEditText.getLeft();
mBeginY = mMessageEditText.getTop();
}
@Override
public void onAnimationEnd(Animation animation) {
mMessageEditText.setPressed(true); //EditText will get highlighted
mMessageEditText.setFocusable(true);
mMessageEditText.setFocusableInTouchMode(true);
mMessageEditText.setTextIsSelectable(true);
mMessageEditText.requestFocus();
mMessageEditText.invalidate();
mMessageEditText.setEnabled(true);
showKeyboard(getActivity().getApplicationContext(), mMessageEditText);
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
mZoomOut = new ScaleAnimation(1.3f, 1.0f, 1.3f, 1.0f, mPivotX, mPivotY);
mZoomOut.setDuration(1000);
mZoomOut.setFillAfter(true);
mSlideDown = new TranslateAnimation(100.0f, 0.0f, -100.0f, 0.0f);
mSlideDown.setDuration(1000);
mSlideDown.setFillAfter(true);
mExitAnimation = new AnimationSet(true);
mExitAnimation.setInterpolator(new LinearInterpolator());
mExitAnimation.addAnimation(mZoomOut);
mExitAnimation.addAnimation(mSlideDown);
mExitAnimation.setFillAfter(true);
mExitAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
mMessageEditText.invalidate();
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
}
private void animZoomIn() {
mCardLayout.startAnimation(mEnterAnimation);
}
private void animZoomOut() {
mCardLayout.startAnimation(mExitAnimation);
}
private static void closeKeyboard(Context c, IBinder windowToken) {
InputMethodManager manager = (InputMethodManager)c.getSystemService(Context.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(windowToken, 0);
}
private static void showKeyboard(Context c, EditText text) {
InputMethodManager manager = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(text, InputMethodManager.SHOW_IMPLICIT);
}
Okay, so thanks to @android developer who pointed out the problem, this was caused by the old version of the animations I was using.
So by replacing
mZoomOut = new ScaleAnimation(1.3f, 1.0f, 1.3f, 1.0f, mPivotX, mPivotY);
mZoomOut.setDuration(1000);
mZoomOut.setFillAfter(true);
mSlideDown = new TranslateAnimation(100.0f, 0.0f, -100.0f, 0.0f);
mSlideDown.setDuration(1000);
mSlideDown.setFillAfter(true);
mExitAnimation = new AnimationSet(true);
mExitAnimation.setInterpolator(new LinearInterpolator());
mExitAnimation.addAnimation(mZoomOut);
mExitAnimation.addAnimation(mSlideDown);
mExitAnimation.setFillAfter(true);
mExitAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
mMessageEditText.invalidate();
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
mCardLayout.startAnimation(mEnterAnimation);
by the new ones :
mCardLayout.animate().translationX(0.0f).translationY(0.0f).scaleX(1.0f).scaleY(1.0f).setDuration(1000).withEndAction(new Runnable() {
@Override
public void run() {
mMessageEditText.invalidate();
mIsViewZoomed = false;
}
});
resolved everything.
这篇关于从Android的autofittextview库变量:缩放发出奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!