flycoTablayout和SurfaceView

一.flycoTablayout

1.使用方式

使用前要先导入依赖

implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.2@aar'//flycotablayout

布局文件中引用

<com.flyco.tablayout.CommonTabLayout
    android:id="@+id/common"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:tl_textsize="13sp"
    app:tl_textSelectColor="#E9251E"
    app:tl_textUnselectColor="#000"
    app:tl_iconWidth="30dp"
    app:tl_iconHeight="30dp"
    app:tl_iconMargin="-5dp"
    android:layout_weight="1">

</com.flyco.tablayout.CommonTabLayout>

2.简单使用

利用flycoTablayout+fragment完成换页

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

    <FrameLayout
        android:id="@+id/lay_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10">

    </FrameLayout>

    <com.flyco.tablayout.CommonTabLayout
        android:id="@+id/common"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:tl_textsize="13sp"
        app:tl_textSelectColor="#E9251E"
        app:tl_textUnselectColor="#000"
        app:tl_iconWidth="30dp"
        app:tl_iconHeight="30dp"
        app:tl_iconMargin="-5dp"
        android:layout_weight="1">

    </com.flyco.tablayout.CommonTabLayout>

</LinearLayout>

在设置数据源时要自定义类实现CustomTabEntity

public class MyTabEntity implements CustomTabEntity {
    private String title;//标题
    private int selectIcon;//选中图标
    private int unSelectIcon;//未选中图标

    public MyTabEntity(String title, int selectIcon, int unSelectIcon) {
        this.title = title;
        this.selectIcon = selectIcon;
        this.unSelectIcon = unSelectIcon;
    }

    @Override
    public String getTabTitle() {
        return title;
    }

    @Override
    public int getTabSelectedIcon() {
        return selectIcon;
    }

    @Override
    public int getTabUnselectedIcon() {
        return unSelectIcon;
    }
}
public class MainActivity extends AppCompatActivity {
    private CommonTabLayout common;
    private ArrayList<CustomTabEntity> datas = new ArrayList<>();
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    private Fragment4 fragment4;

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

        initDatas();
        initViews();
    }

    private void initDatas() {
        datas.add(new MyTabEntity("新闻", R.drawable.tab_home_selected, R.drawable.tab_home_normal));
        datas.add(new MyTabEntity("视频", R.drawable.tab_video_selected, R.drawable.tab_video_normal));
        datas.add(new MyTabEntity("推荐", R.drawable.tab_micro_selected, R.drawable.tab_micro_normal));
        datas.add(new MyTabEntity("我的", R.drawable.tab_me_selected, R.drawable.tab_me_normal));

        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();


        getSupportFragmentManager().beginTransaction()
                .add(R.id.lay_content, fragment1)
                .add(R.id.lay_content, fragment2)
                .add(R.id.lay_content, fragment3)
                .add(R.id.lay_content, fragment4)
                .commit();

        showFragemnt(fragment1);
    }

    private void initViews() {
        common = (CommonTabLayout) findViewById(R.id.common);
	//设置数据源
        common.setTabData(datas);
        common.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switch (position) {
                    case 0:
                        showFragemnt(fragment1);
                        break;
                    case 1:
                        showFragemnt(fragment2);
                        break;
                    case 2:
                        showFragemnt(fragment3);
                        break;
                    case 3:
                        showFragemnt(fragment4);
                        break;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }

    public void showFragemnt(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .hide(fragment1)
                .hide(fragment2)
                .hide(fragment3)
                .hide(fragment4)
                .show(fragment)
                .commit();
    }
}

二.SurfaceView

1.简介

surfaceview 主要作用

(1)视频输出的屏幕。

(2)绘制图像

注意事项
surfaceCreated有特定的生命周期,注意在callback中进行操作。

2.简单使用

利用SurfaceView写一个简单的视频播放器

首先自定义类继承SurfaceView

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder surfaceHolder;
    private MediaPlayer mediaPlayer;
	
	//播放视频
    public void setDataPath(String path) {
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public MySurfaceView(Context context) {
        super(context);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //活动surfaceHolder(操作者)
        surfaceHolder = getHolder();
        //回调获取生命周期
        surfaceHolder.addCallback(this);
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
        }
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        mediaPlayer.setDisplay(surfaceHolder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    //暂停播放
    public void playOrNo() {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            } else {
                mediaPlayer.start();
            }
        }
    }

    //拖动设置进度
    public void seekTo(int progress) {
        if (mediaPlayer != null) {
            int duration = mediaPlayer.getDuration();
            int current = progress * duration / 100;
            mediaPlayer.seekTo(current);
        }
    }

    //获取播放进度
    public int getPrograss() {
        if (mediaPlayer != null) {
            int duration = mediaPlayer.getDuration();
            int currentPosition = mediaPlayer.getCurrentPosition();
            int progress = currentPosition * 100 / duration;
            return progress;
        }
        return 0;
    }

    //获取当前播放时长
    public String getCurrentTime() {
        if (mediaPlayer != null) {
            long currentPosition = mediaPlayer.getCurrentPosition();
            SimpleDateFormat format = new SimpleDateFormat("mm:ss");
            String format1 = format.format(currentPosition);
            return format1 + "";
        }
        return "";
    }

    //获取时长
    public String getDuration() {
        if (mediaPlayer != null) {
            long duration = mediaPlayer.getDuration();
            SimpleDateFormat format = new SimpleDateFormat("mm:ss");
            String format1 = format.format(duration);
            return format1 + "";
        }
        return "";
    }


}

在布局文件中直接引用

<com.example.surfaceview.view.MySurfaceView
    android:id="@+id/sv"
    android:layout_width="match_parent"
    android:layout_height="250dp" />

整体布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.surfaceview.view.MySurfaceView
        android:id="@+id/sv"
        android:layout_width="match_parent"
        android:layout_height="250dp" />

    <ImageView
        android:id="@+id/iv_full"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/sv"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:src="@drawable/full" />

    <TextView
        android:id="@+id/te_nowTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:layout_marginBottom="10dp"
        android:layout_alignBottom="@id/sv"
        android:text="0:00" />

    <SeekBar
        android:id="@+id/sb"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/sv"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp" />

    <TextView
        android:id="@+id/te_allTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="320dp"
        android:layout_marginBottom="10dp"
        android:layout_alignBottom="@id/sv"
        android:text="0:00" />

    <Button
        android:id="@+id/but_play"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/sv"
        android:layout_marginLeft="10dp"
        android:text="暂停播放"
        android:textSize="10sp" />


</RelativeLayout>

Activity中的代码

public class MainActivity extends AppCompatActivity {
    private MySurfaceView mySurfaceView;
    private ImageView ivFull;
    private Button butPlay;
    private SeekBar sb;
    private Timer timer;
    private TextView teNowTime;
    private TextView teAllTime;
    private Handler handler = new Handler();
    private boolean flag = true;


    private String url = "https://vdept.bdstatic.com/77654876766a566e5050393567776e4a/434e796648484676/e03aed2f6c37ebd00d194be53bed213b83af723b13c44f2a459123a94b251e9ec07f9cb450250aef311987de5dea3ea2.mp4?auth_key=1585660878-0-0-43694f3d63feb80e621fe791fcd73a60";

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

        initViews();
        initTimer();
    }

    private void initTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                final String currentTime = mySurfaceView.getCurrentTime();
                final String duration = mySurfaceView.getDuration();

                final int prograss = mySurfaceView.getPrograss();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        sb.setProgress(prograss);//设置进度条进度
                        teAllTime.setText(duration);//设置总时长
                        teNowTime.setText(currentTime);//设置当前时长
                    }
                });
            }
        }, 0, 100);
    }

    private void initViews() {
        mySurfaceView = findViewById(R.id.sv);
        mySurfaceView.setDataPath(url);
        butPlay = (Button) findViewById(R.id.but_play);
        sb = (SeekBar) findViewById(R.id.sb);


        teNowTime = (TextView) findViewById(R.id.te_nowTime);
        teAllTime = (TextView) findViewById(R.id.te_allTime);

        //设置拖动
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (b) {
                    mySurfaceView.seekTo(i);//视频播放移动
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        ivFull = (ImageView) findViewById(R.id.iv_full);

        //切换横竖屏
        ivFull.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change();
            }
        });

        //暂停/播放
        butPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mySurfaceView.playOrNo();
            }
        });


    }

    private void change() {
        if (flag) {//竖屏
            ivFull.setImageResource(R.drawable.unfull);
            flag = false;
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {//横屏
            ivFull.setImageResource(R.drawable.full);
            flag = true;
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值