27
2017
09

Android,实现一个弹幕效果的小demo

这里写图片描述
这里写图片描述
这里写图片描述

代码不是很多,花了十多分钟写的。。。

BarrageLauncher(弹幕发射器)

NativeVideoPlayer(视频播放器)

package com.zzyd.example;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.Random;

/** * Created by Administrator on 2017/9/26 0026. * 弹幕发射器 */

public class BarrageLauncher extends FrameLayout implements View.OnClickListener {

    private Context context;

    private NativeVideoPlayer nativeVideoPlayer;

    private View barrageView;

    private EditText editText;

    private Button sendBtn;

    private TranslateAnimation animation;

    private int viewWidth;

    private int viewHeight;

    public BarrageLauncher(@NonNull Context context) {
        super(context);
        this.context = context;
        init();
    }

    public BarrageLauncher(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        nativeVideoPlayer = new NativeVideoPlayer(context);
        barrageView = View.inflate(context, R.layout.view_barrage, null);
        editText = barrageView.findViewById(R.id.barrageEd);
        sendBtn = barrageView.findViewById(R.id.sendBtn);
        sendBtn.setOnClickListener(this);
        addView(nativeVideoPlayer);
        addView(barrageView);
        Resources resources = getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
    }

    public void loadBarrage(final String[] barrages){

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(String s : barrages){
                    Message msg = Message.obtain();
                    msg.what = 0;
                    Bundle b = new Bundle();
                    b.putString("key",s);
                    msg.setData(b);
                    handler.sendMessage(msg);
                    try {
                        Thread.sleep(80);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        viewHeight = MeasureSpec.getSize(heightMeasureSpec);
        animation = new TranslateAnimation(viewWidth,0,50,50);
        animation.setDuration(8500);
        animation.setInterpolator(new LinearInterpolator());
    }

    public void addItemView(final View child){
        super.addView(child);
        FrameLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// layoutParams.gravity = Gravity.RIGHT;
        layoutParams.topMargin = getRandom(10,viewHeight/2);
        child.setLayoutParams(layoutParams);
        child.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                    removeView(child);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    private TextView createItemView(String msg){
        TextView textView = new TextView(context);
        textView.setText(msg);
        textView.setTextColor(Color.YELLOW);
        textView.setTextSize(getRandom(15,25));
        return textView;
    }

    @Override
    public void onClick(View view) {
        addItemView(createItemView(editText.getText().toString()));
        editText.setText("");
    }

    private static int getRandom(int min , int max){
        Random random = new Random();
        return random.nextInt(max)%(max - min + 1) + min;
    }

    private Handler handler = new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            addItemView(createItemView(msg.getData().getString("key")));
        }
    };

}
package com.zzyd.example;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

/** * Created by Administrator on 2017/9/26 0026. */

public class NativeVideoPlayer extends SurfaceView {

    private SurfaceHolder surfaceHolder;

    private MediaPlayer mediaPlayer;

    private Context context;

    public NativeVideoPlayer(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public NativeVideoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init(){
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(callback2);
        mediaPlayer = new MediaPlayer();
    }

    private SurfaceHolder.Callback2 callback2 = new SurfaceHolder.Callback2() {
        @Override
        public void surfaceRedrawNeeded(SurfaceHolder surfaceHolder) {

        }

        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            mediaPlayer.setSurface(surfaceHolder.getSurface());
            try {
                AssetManager assetMg = context.getAssets();
                AssetFileDescriptor fileDescriptor = assetMg.openFd("test.mp4");
                mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
                        fileDescriptor.getStartOffset(), fileDescriptor.getLength());
                mediaPlayer.setLooping(true);
                mediaPlayer.setVolume(0, 0);
                mediaPlayer.prepare();
                mediaPlayer.start();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

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

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    };

}
package com.zzyd.example;

import android.content.pm.ActivityInfo;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private String[] barrages = new String[]{
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",
            "onCreate","AppCompatActivity",
            "MainActivity",
            "String[]",
            "barrages","String",
            "mipmap-hdpi",
            "activity_main.xml",
            "barrageLauncher",
            "ic_launcher_round.png",
            "AndroidManifest.xml",
            "mipmap-xxhdpi",
            "BarrageLauncher",
            "SCREEN_ORIENTATION_LANDSCAPE",
            "barrageLauncher",
            "BarrageLauncher",
            "ActivityInfo",
            "setRequestedOrientation",
            "onCreate",
            "savedInstanceState",

    };
    BarrageLauncher barrageLauncher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        barrageLauncher = new BarrageLauncher(this);
        setContentView(barrageLauncher);
        barrageLauncher.postDelayed(new Runnable() {
            @Override
            public void run() {
                Message message = Message.obtain();
                message.what = 0;
                handler.sendMessage(message);
            }
        },1000);

    }

    private Handler handler = new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            Toast.makeText(MainActivity.this,"发射",Toast.LENGTH_SHORT).show();
            barrageLauncher.loadBarrage(barrages);
        }
    };
}
上一篇:使用dataBinding出现Warning:Using incompatible plugins for the annotation processing: android-apt. 下一篇:三 service 面试详解