从左侧滑入添加视图

时间:2015-05-04 08:47:09

标签: android android-animation android-view

我想通过动画为片段添加视图。我希望通过从屏幕左侧滑入来显示视图,然后以相同的方式滑出来退出。

我无法让它发挥作用。当片段出现时,视图已经可见,没有动画。当我按回去关闭片段时,视图会向右移动。

这是视图类:

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));
    }

我真的很感谢帮助

2 个答案:

答案 0 :(得分:1)

这将解决你的问题

myView.startAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.slide_out_left));

答案 1 :(得分:0)

我通过另一种方式做到这一点,你可以尝试我的代码:(这是一个完整的项目)。您也可以下载完整的项目here

1)activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/frameToReplace"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>


</LinearLayout>

2)MainActivity.class

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Fragment fragment = FragmentParent.newInstance();
    getFragmentManager().beginTransaction().replace(R.id.frameToReplace,fragment).commit();
}

3)mdemir_layout.xml - &gt;这里发生了动画。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <Button
        android:id="@+id/btn_Click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add View"/>
    <RelativeLayout
        android:id="@+id/rlt"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </RelativeLayout>

    <FrameLayout
        android:id="@+id/fragInvi"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:visibility="invisible"
        android:background="#253">

    </FrameLayout>

</LinearLayout>

4)FragmentParent.class

public class FragmentParent extends Fragment {

    public FragmentParent (){

    }

    public static FragmentParent newInstance(){
        return new FragmentParent();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.mdemir_layout,container,false);

        final FrameLayout fragInvi = (FrameLayout)rootView.findViewById(R.id.fragInvi);
        Fragment fragment = FragmentForStefanFalk.newInstance();

        getFragmentManager().beginTransaction().replace(R.id.fragInvi,fragment).commit();
        //Prepair for translating View
        final int screenWidth = dpToPx(getResources().getConfiguration().screenWidthDp);
        // translate fragInvi to exact position and prepair size
        fragInvi.setTranslationX(-screenWidth);
        fragInvi.setTranslationY(-dpToPx(200));

        setLayoutSize(fragInvi,screenWidth,dpToPx(200));
        final boolean[] isTranslated = {false};

        Button btn_Click = (Button)rootView.findViewById(R.id.btn_Click);
        // translate view with animation when button is Clicked
        btn_Click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isTranslated[0]){
                    fragInvi.setVisibility(View.VISIBLE);
                    fragInvi.setTranslationX(0);
                    fragInvi.setTranslationX(-screenWidth);
                    fragInvi.animate().translationX(0).setDuration(1000);
                }else{
                    fragInvi.animate().translationX(fragInvi.getWidth()).setDuration(1000);
                }
                isTranslated[0] = !isTranslated[0];
            }
        });

        return rootView;
    }
    private static void setLayoutSize(View view, int width, int height) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }
    private int dpToPx(int dp) {
        return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
    }
}

5。 stefan_falk_frame.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lv_test"
        android:background="#253"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>

6)FragmentForStefanFalk.java

public class FragmentForStefanFalk extends Fragment {

    public FragmentForStefanFalk (){

    }

    public static FragmentForStefanFalk newInstance(){
        return new FragmentForStefanFalk();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.stefan_falk_frame,container,false);
        ListView lv_test = (ListView) rootView.findViewById(R.id.lv_test);
        List<String> arr = new ArrayList<>();
        for(int i = 0;i < 10; i++){arr.add("String "+i);}
        ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arr);
        lv_test.setAdapter(adapter);
        lv_test.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "Click on " + position, Toast.LENGTH_SHORT).show();
            }
        });
        return rootView;
    }

好的,这就是全部。试试并告诉我结果。