我想通過動畫向視圖添加視圖。我希望通過從屏幕左側滑入並通過以相同方式滑出來出現視圖。通過從左側滑入來添加視圖
我無法得到這個工作。片段出現時,視圖已經可見,沒有動畫。當我按回來解散片段時,視圖向右移動。
這裏的視圖類:
public class BannerView extends LinearLayout {
Context context;
public BannerView(Context context) {
super(context);
this.context = context;
View.inflate(context, R.layout.heading_view, this);
}
public BannerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
View.inflate(context, R.layout.heading_view, this);
}
public BannerView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
View.inflate(context, R.layout.heading_view, this);
}
public void show(final LinearLayout viewGroup){
final LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
viewGroup.addView(this,p);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Animation anim_list = AnimationUtils.loadAnimation(context, R.anim.slide_in);
LayoutAnimationController controller = new LayoutAnimationController(anim_list, 0.1f);
setLayoutAnimation(controller);
}
}
動畫XML是
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%" android:toXDelta="0%" android:duration="1000"/>
</set>
這是我如何添加視圖:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
BannerView b = new BannerView(getActivity());
b.show((LinearLayout) getView().findViewById(R.id.footerContainerLayout));
}
我真的很感激有幫助此
那麼當你運行它會發生什麼? – Clyde
我已更新我的問題 –
嘗試使用http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/中的動畫res/res/anim/slide_in_left.xml?av = f –