假设我有一个垂直linearLayout:
[v1]
[v2]
Run Code Online (Sandbox Code Playgroud)
默认情况下,v1具有visibily = GONE.我想用扩展动画展示v1并同时向下推v2.
我试过这样的事情:
Animation a = new Animation()
{
int initialHeight;
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final int newHeight = (int)(initialHeight * interpolatedTime);
v.getLayoutParams().height = newHeight;
v.requestLayout();
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
initialHeight = height;
}
@Override
public boolean willChangeBounds() {
return true;
}
};
Run Code Online (Sandbox Code Playgroud)
但是使用这个解决方案,动画开始时我会闪烁.我认为这是由v1在应用动画之前显示完整大小引起的.
使用javascript,这是一行jQuery!用android做任何简单的方法吗?
对于动画,我需要知道视图的高度.问题是,除非绘制View,否则getHeight()方法总是返回0.那么有没有办法获得高度而不绘制它?
在这种情况下,View是LinearLayout.
编辑:我尝试从https://github.com/Udinic/SmallExamples/blob/master/ExpandAnimationExample/src/com/udinic/expand_animation_example/ExpandAnimation.java改编扩展动画
有了它,我想为列表项扩展一些更多的信息.我无法通过xml实现相同的效果.目前,当您在绘制之前知道布局大小时,动画才会起作用.
我试图得到的高度和宽度ImageView的Fragment有以下ViewTreeObserver:
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
private ImageView imageViewPicture;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_general_activity_add_recipe, container, false);
setHasOptionsMenu(true);
...
final ViewTreeObserver observer = imageViewPicture.getViewTreeObserver();
observer.addOnGlobalLayoutListener (new OnGlobalLayoutListener () {
@Override public void onGlobalLayout() {
observer.removeGlobalOnLayoutListener(this);
}
});
return view;
}
Run Code Online (Sandbox Code Playgroud)
运行此代码会导致以下异常:
10-12 23:45:26.145: E/AndroidRuntime(12592): FATAL EXCEPTION: main
10-12 23:45:26.145: E/AndroidRuntime(12592): java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again
10-12 23:45:26.145: E/AndroidRuntime(12592): at android.view.ViewTreeObserver.checkIsAlive(ViewTreeObserver.java:509)
10-12 23:45:26.145: E/AndroidRuntime(12592): …Run Code Online (Sandbox Code Playgroud) 我试图TextView 在绘制之前确定a的高度.我正在使用以下代码来执行此操作:
TextView textView = (TextView) findViewById(R.id.textview);
textView.setText("TEST");
int widthSpec = MeasureSpec.makeMeasureSpec(LayoutParams.MATCH_PARENT, MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(widthSpec, heightSpec);
System.out.println("MeasuredHeight: " + textView.getMeasuredHeight());
Run Code Online (Sandbox Code Playgroud)
输出是MeasuredHeight: 28.没有错.
但是,当我给出TextView一个长文本字符串,所以发生包装时,它仍然给出一行而不是两行的高度:
(...)
textView.setText("TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST");
(...)
Run Code Online (Sandbox Code Playgroud)
输出是MeasuredHeight: 28我所期望的MeasuredHeight: 56.
为什么它没有给我正确的价值,我怎样才能达到正确的价值?