在ViewPager中的Fragment添加控件及TextureView

在ViewPager中的Fragment添加控件及TextureView
首先做一个ViewPager,这是利用一个利用FragmentPagerAdapter制作的页面。
MainActivity:

package example.com.testtextureview;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity
{

    private ViewPager pager;
    private List titleList;
    private List fragList;

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


        titleList = new ArrayList();
        titleList.add("第1页");
        titleList.add("第2页");
        titleList.add("第3页");
        titleList.add("第4页");

        pager = (ViewPager) findViewById(R.id.pager);

        fragList = new ArrayList();
        fragList.add(new Fragment1());
        fragList.add(new Fragment2());
        fragList.add(new Fragment3());
        fragList.add(new Fragment4());

        tab = (PagerTabStrip) findViewById(R.id.tab);
        tab.setBackgroundColor(Color.RED);
        tab.setTextColor(Color.WHITE);
        tab.setDrawFullUnderline(false);
        tab.setTabIndicatorColor(Color.WHITE);


        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(
                getSupportFragmentManager(), fragList, titleList);
        pager.setAdapter(adapter);
    }

}

适配器MyFragmentPagerAdapter:

package example.com.testtextureview;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    private ListfragList;
    private ListtitleList;
    public MyFragmentPagerAdapter(FragmentManager fm,List fragList,ListtitleList) {
        super(fm);
        this.fragList=fragList;
        this.titleList=titleList;
    }

    @Override
    public Fragment getItem(int arg0) {
        return fragList.get(arg0);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }


}

content_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="com.example.testviewpager.MainActivity">



        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">

            <android.support.v4.view.PagerTabStrip
                android:id="@+id/tab"
                android:layout_width="fill_parent"
                android:layout_height="45dp"
                android:layout_gravity="top"/>
        android.support.v4.view.ViewPager>




RelativeLayout>

之后新建4个Fragment分别为Fragment1,Fragment2,Fragment3,Fragment4

现在我要在Fragment1中添加一个Button,
首先在xml中添加一个Button,
之后在Fragment1中代码为:

package example.com.testtextureview;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;


/**
 * Created by bazinga on 2015/10/9.
 */
public class Fragment1 extends Fragment implements View.OnClickListener
{
    private Button mButton;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        View view = inflater.inflate(R.layout.fragment1,null);

        mButton = (Button) view.findViewById(R.id.btn);

        mButton.setOnClickListener(this);

        return view;
    }


    @Override
    public void onClick(View view)
    {
        Toast.makeText(getActivity(), "start", Toast.LENGTH_SHORT).show();
    }
}

然后我想在Fragment2中获取一个Camera,在第二页中显示TextureView;

package example.com.testtextureview;

import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import java.io.IOException;

/**
 * Created by bazinga on 2015/10/9.
 */
public class Fragment2 extends Fragment implements TextureView.SurfaceTextureListener
{
    private Camera mCamera;
    private TextureView mTextureView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        RelativeLayout view = new RelativeLayout(getActivity());
        view.setLayoutParams(lp);

        mTextureView = new TextureView(getActivity());
        mTextureView.setSurfaceTextureListener(this);
        mTextureView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

        view.addView(mTextureView);

        return view;
    }


    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
    {
        mCamera = Camera.open();
        try
        {
            mCamera.setPreviewTexture(surface);
            mCamera.startPreview();
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        mTextureView.setRotation(90);
        mTextureView.setScaleX(2f);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1)
    {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture)
    {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture)
    {

    }
}

其中这里不需要有他自己的xml,因为这是由java代码创建,这是创建一个RelativeLayout布局,不需要获取Camera的宽度和高度,只需要将布局的宽高设置出来,将mTextureView添加进View中,设置监听。
在这里会出现摄像屏幕不能铺满整个屏幕的问题,这时候需要设置 mTextureView.setScaleX(2f);,才能将屏幕铺满,这应该是一个bug吧。

你可能感兴趣的:(android)