How to display video in VideoView in the center of

2020-07-31 06:42发布

问题:

I am creating ViewPager with two VideoViews. On sliding pages, video on current page start playing. One problem: how to display video in VideoView in the center of the screen ?

Here is my code,

MainActivity.java:

public class MainActivity extends Activity{

    private class MyViewPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener
    {
         private Vector<View> pages;         
         private ViewGroup myContainer;
         private int currentPageId;
         private boolean flag;

         public MyViewPagerAdapter(Context context, Vector<View> pages) 
         {
             this.pages=pages;
         }

         @Override
         public int getCount() 
         {
             return pages.size();
         }

         @Override
         public Object instantiateItem(ViewGroup container, int position)
         {
             flag = true;
             currentPageId = 0;
             int positionNow = myPager.getCurrentItem();

             View page = pages.get(position);
             container.addView(page);
             myContainer = container;

             if(positionNow == position)
                StartVideo(container, positionNow);

             return page;
         }

         @Override
         public void destroyItem(ViewGroup container, int position, Object object)
         {
             container.removeView((View) object);
             myContainer = container;
         }

         @Override
         public boolean isViewFromObject(View view, Object object)
         {
             return view.equals(object);
         }

        @Override
        public void onPageScrollStateChanged(int arg0) 
        {
            if(flag)
            {
                MyVideoView currVideo = (MyVideoView) myContainer.getChildAt(currentPageId+1);
                if(currVideo != null)
                    if(currVideo.getVideoPath() != null)
                        currVideo.pause();
            }
            else
                flag = !flag;
        }

        @Override
        public void onPageSelected(int arg0) 
        {
            flag = false;
            currentPageId = myPager.getCurrentItem();
            StartVideo(myContainer, myPager.getCurrentItem());
        }

        private void StartVideo(ViewGroup container, int position)
        {
            View page = container.getChildAt(position+1);
            MyVideoView curVideo = (MyVideoView) page;
            Log.d("Andrew", curVideo.getVideoPath());
            curVideo.start();
        }
    }

    private ViewPager myPager;
    private MyViewPagerAdapter myAdapter;
    private Vector<View> pages;
    private MyVideoView myVideoView;
    int currentPage, previousPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pages = new Vector<View>();

        for(int i=1; i <= 2; i++)
        {
            myVideoView = new MyVideoView(this);
            myVideoView.setVideoPath("videoPath");
            pages.add(myVideoView);
        }

        this.myAdapter = new MyViewPagerAdapter(this, pages);
        this.myPager = (ViewPager) this.findViewById(R.id.viewpageronecard);
        this.myPager.setAdapter(myAdapter);

        myPager.setOnPageChangeListener(myAdapter);
        myPager.setOffscreenPageLimit(1);
        myPager.setCurrentItem(0);
    }
}

activity_main.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpageronecard"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#222222" >

     </android.support.v4.view.ViewPager>

</RelativeLayout>

回答1:

Although it is an old question, perhaps someone else is struggling with it, too, as I had the same problem. I solved it by changing

 android:layout_centerInParent="true"
 android:layout_centerVertical="true"
 android:layout_centerHorizontal="true"

to

 android:layout_gravity="center"

in the xml for the VideoView I used to populate the ViewPager.