我試圖做的是這樣的動畫:的Android - TranslateAnimation更新動態佈局的大小
上下文:
我有兩個EditText
的我需要你的時候點擊一個,另一個從第一個後面出來。在這裏你有一些圖片來獲得我想要的圖像。
要做到這一點很明顯,我以第二EditText
從後面的第一個移動需要TranslateAnimation
。
我的方法:
,我能想到的是在使用FrameLayout
把EditText
另一層之上,然後在第一個的onTouch事件上的第二個做TranslateAnimation
的第一件事。這個問題是,如果我在wrap_content
中有FrameLayout
高度,那麼動畫對用戶是不可見的。如果我改變運行時FrameLayout
的高度,我會離開第一EditText
下方的空隙,例如你可以在這個圖片中看到:
,我認爲第二種解決方案是增加一個AnimationListener
到TranslateAnimation
並在onAnimationStart
方法中改變FrameLayout
的高度。但問題在於FrameLayout
的高度變化太突然。我想保持動畫順暢。
我的問題:
我怎樣才能獲得第二EditText
的流暢的動畫從後面的第一個,改變FrameLayout
的高度爲第二EditText
向下移動?
謝謝!
更新:
我被RelativeLayout
我搜索,有這種情況沒有什麼區別改變了FrameLayout
。我嘗試縮放這個RelativeLayout
,其中包含EditText
和AnimationScale
以便順利顯示動畫,但沒有奏效。這裏是我的代碼:
protected void expanse() {
Log.d("Accordion", "expandAccordion");
//Translate the top of the second EditText to its bottom
TranslateAnimation translateSecond = new TranslateAnimation(second.getLeft(), second.getLeft(),
second.getTop(), second.getBottom());
translateSecond.setDuration(1000);
//Scale the relative layout to show the both of them
ScaleAnimation scaleContainer = new ScaleAnimation(container.getLeft(), container.getLeft(),
container.getTop(), second.getBottom());
scaleContainer.setDuration(1000);
//At the end of the scaling, change the height of the relative layout
scaleContainer.setAnimationListener(new AnimationListenerAdapter() {
@Override
public void onAnimationEnd(Animation animation) {
RelativeLayout.LayoutParams params = (LayoutParams) container.getLayoutParams();
params.height = second.getBottom();
container.setLayoutParams(params);
super.onAnimationEnd(animation);
}
});
container.startAnimation(scaleContainer);
second.startAnimation(translateSecond);
}
順便說一句,我可以保證,如果我硬編碼了一些大的高度像350dp,動畫顯示正確。
UPDATE:
我試圖移動兩個,第二EditText
和下面的佈局。這是代碼。 BTW另一個原因,我改變了ListView
自定義ViewPager
,這並沒有改變任何東西。
public class MainActivity extends FragmentActivity {
private EditText first;
private EditText second;
private HoboViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (HoboViewPager) findViewById(R.id.pager);
pager.setPageTransformer(true, new RotationPageTransformer());
pager.setAdapter(new SampleAdapter(this, getSupportFragmentManager()));
first = (EditText) findViewById(R.id.location_search_field);
second = (EditText) findViewById(R.id.term_search_field);
first.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
expanseSecondField();
return true;
}
});
}
protected void expanseSecondField() {
TranslateAnimation translateSecondField = new TranslateAnimation(second.getLeft(), second.getLeft(),
second.getTop(), second.getBottom());
translateSecondField.setFillAfter(true);
translateSecondField.setDuration(1000);
TranslateAnimation translateContainer = new TranslateAnimation(pager.getLeft(), pager.getLeft(),
pager.getTop(), pager.getTop() + second.getBottom());
translateContainer.setFillAfter(true);
translateContainer.setDuration(1000);
pager.startAnimation(translateContainer);
second.startAnimation(translateSecondField);
}
}
這不起作用,因爲容器的TranslateAnimation立即執行。結果與當我試圖在動畫結束時更改容器大小的結果相同。
好的方式認爲它。我會試一試,如果有作品,我會接受你的回答。謝謝! – 4gus71n
我認爲這個解決方案不起作用,因爲當我在'FrameLayout'中執行動畫時,視圖會失效,而我看不到'EditText'的動畫。請檢查我的更新。 – 4gus71n
你需要創建2個獨立的TranslateAnimation實例 - 你在做什麼? –