如何缩放视图以适应 Android 动画中的内容大小?
How to scale the view to fit the content size in Android animation?
我正在使用 CardView 作为 Recycler Adapter 的一个项目。
起初,所有项目不会显示全部内容,(我将其剪切并放置'......'代替)
但是点击它之后,被点击的项目会缩放它的高度以适应内容的高度。 (这里我将文字设置为全部内容,希望卡片可以缩放以适应它)
看起来像:
I know how to animate the height to the specific target height..
but in this case, I don't know how to measure the height needed to show the whole content, each item should have different target height.
我该怎么做?
你可以做的是让 View
测量自身,不限制其高度。请注意对 view.getWidth()
的调用,您只能在 View
布局后执行此操作,因为您在 onClick()
中调用它应该没问题。
int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthSpec, heightSpec);
int targetHeight = view.getMeasuredHeight();
假设您的视图是一个设置了这些属性的 TextView:
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
完整的例子是
// this is the height measured with maxLines 1 and height
// to wrap_content
final int startHeight = view.getHeight();
// you want to measure the TextView with all text lines
view.setMaxLines(Integer.MAX_VALUE);
int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthSpec, heightSpec);
// final height of the TextView
int targetHeight = view.getMeasuredHeight();
// this is the value that will be animated from 0% to 100%
final int heightSpan = targetHeight-startHeight;
// remove that wrap_content and set the starting point
view.getLayoutParams().height = startHeight;
view.setLayoutParams(view.getLayoutParams());
Animation animation = new Animation(){
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
view.getLayoutParams().height = (int) (startHeight + heightSpan*interpolatedTime);
view.setLayoutParams(view.getLayoutParams());
}
};
animation.setDuration(1000);
view.startAnimation(animation);
你可以很简单地做到这一点。
第一次给TextView设置文本时,这样写:
int minLineNumber = 2;
textView.setMaxLines(minLineNumber); // 2 is your number of line for the first time
当点击它时:
textView.setMaxLines(Integer.MAX_VALUE); // set the height like wrap_content
如果你想要动画,你有这样的东西:
ObjectAnimator animation = ObjectAnimator.ofInt(
textView,
"maxLines",
textView.getLineCount());
int duration = (textView.getLineCount() - minLineNumber) * 50;
animation.setDuration(duration);
animation.start();
如果您的 TextView 中的文本太多,最好使用固定的持续时间,而不是取决于高度。
我正在使用 CardView 作为 Recycler Adapter 的一个项目。
起初,所有项目不会显示全部内容,(我将其剪切并放置'......'代替)
但是点击它之后,被点击的项目会缩放它的高度以适应内容的高度。 (这里我将文字设置为全部内容,希望卡片可以缩放以适应它)
看起来像:
I know how to animate the height to the specific target height.. but in this case, I don't know how to measure the height needed to show the whole content, each item should have different target height.
我该怎么做?
你可以做的是让 View
测量自身,不限制其高度。请注意对 view.getWidth()
的调用,您只能在 View
布局后执行此操作,因为您在 onClick()
中调用它应该没问题。
int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthSpec, heightSpec);
int targetHeight = view.getMeasuredHeight();
假设您的视图是一个设置了这些属性的 TextView:
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
完整的例子是
// this is the height measured with maxLines 1 and height
// to wrap_content
final int startHeight = view.getHeight();
// you want to measure the TextView with all text lines
view.setMaxLines(Integer.MAX_VALUE);
int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthSpec, heightSpec);
// final height of the TextView
int targetHeight = view.getMeasuredHeight();
// this is the value that will be animated from 0% to 100%
final int heightSpan = targetHeight-startHeight;
// remove that wrap_content and set the starting point
view.getLayoutParams().height = startHeight;
view.setLayoutParams(view.getLayoutParams());
Animation animation = new Animation(){
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
view.getLayoutParams().height = (int) (startHeight + heightSpan*interpolatedTime);
view.setLayoutParams(view.getLayoutParams());
}
};
animation.setDuration(1000);
view.startAnimation(animation);
你可以很简单地做到这一点。 第一次给TextView设置文本时,这样写:
int minLineNumber = 2;
textView.setMaxLines(minLineNumber); // 2 is your number of line for the first time
当点击它时:
textView.setMaxLines(Integer.MAX_VALUE); // set the height like wrap_content
如果你想要动画,你有这样的东西:
ObjectAnimator animation = ObjectAnimator.ofInt(
textView,
"maxLines",
textView.getLineCount());
int duration = (textView.getLineCount() - minLineNumber) * 50;
animation.setDuration(duration);
animation.start();
如果您的 TextView 中的文本太多,最好使用固定的持续时间,而不是取决于高度。