1. 程式人生 > >簡單實現彈幕效果

簡單實現彈幕效果

     首先分析一下,他是由三層佈局來共同完成的,第一層視訊佈局,第二層字幕布局,第三層輸入框佈局,要想讓這三個佈局在同一頁面上,必須用相對佈局或幀佈局。

<?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"
    android:id="@+id/activity_main"
    tools:context="com.bwie.danmustudy.MainActivity">

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />
    <master.flame.danmaku.ui.widget.DanmakuView
        android:id="@+id/danmaku_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <LinearLayout
        android:id="@+id/operation_text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:visibility="gone"
        android:background="#fff"
        android:orientation="horizontal"
        >
        <EditText
            android:id="@+id/edit_text"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
        <Button
            android:id="@+id/send"
            android:text="send"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>

</RelativeLayout>
public class MainActivity extends AppCompatActivity {

    private boolean showDanmaku;
    private DanmakuView danmakuView;
    private DanmakuContext danmakuContext;
    //建立一個彈幕的解析器
    private BaseDanmakuParser parser=new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //播放視訊
        VideoView video_view= (VideoView) findViewById(R.id.video_view);
        Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.minion_08);
        video_view.setVideoURI(uri);
        video_view.start();

        danmakuView= (DanmakuView) findViewById(R.id.danmaku_view);
        //呼叫了enableDanmakuDrawingCache()方法來提升繪製效率,也就是繪製速度
        // 又呼叫了setCallback()方法來設定回撥函式。
        danmakuView.enableDanmakuDrawingCache(true);
        danmakuView.setCallback(new DrawHandler.Callback() {
            @Override
            public void prepared() {
                showDanmaku=true;
                danmakuView.start();
            }

            @Override
            public void updateTimer(DanmakuTimer timer) {

            }

            @Override
            public void danmakuShown(BaseDanmaku danmaku) {

            }

            @Override
            public void drawingFinished() {

            }
        });
        danmakuContext=danmakuContext.create();
        //第一個引數是彈幕的解析器
        //呼叫DanmakuView的prepare()方法來進行準備,準備完成後會自動呼叫剛才設定的回撥函式中的prepared()方法
        danmakuView.prepare(parser,danmakuContext);
        final LinearLayout operationLayout= (LinearLayout) findViewById(R.id.operation_text);
        final Button send= (Button) findViewById(R.id.send);
        final EditText edit_text= (EditText) findViewById(R.id.edit_text);
        danmakuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (operationLayout.getVisibility()==View.GONE){
                    operationLayout.setVisibility(View.VISIBLE);
                }else{
                    operationLayout.setVisibility(View.GONE);
                }
            }
        });
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content=edit_text.getText().toString();
                if (!TextUtils.isEmpty(content)){
                    addDanmaku(content,true);
                    edit_text.setText("");
                }
            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus&& Build.VERSION.SDK_INT>=19){
            View decorView=getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            |View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            |View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            |View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            |View.SYSTEM_UI_FLAG_FULLSCREEN
                            |View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            );
        }
    }
    private void addDanmaku(String content,boolean withBorder){
        BaseDanmaku danmaku=danmakuContext.mDanmakuFactory
                .createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
        danmaku.text=content;
        danmaku.padding=5;
        danmaku.textSize=50;

        danmaku.setTime(danmakuView.getCurrentTime());
        if (withBorder){
            danmakuView.addDanmaku(danmaku);
        }
    }

最後使頁面橫屏展示:

<activity android:name=".MainActivity"
    只需要加這一行程式碼就可以
     android:screenOrientation="landscape"
      >
    <intent-filter>
         <action android:name="android.intent.action.MAIN" />

         <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
</activity>