欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Surface

程序员文章站 2022-03-29 17:52:34
...

画简单图形(点、线、圆、弧线、扇形、图片、文字)

//画笔
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(5);
        paint.setTextSize(50);
        paint.setStyle(Paint.Style.STROKE);

        //画布
        Canvas canvas = surfaceHolder.lockCanvas();
        canvas.drawColor(Color.WHITE);

        //点
        canvas.drawPoint(500,500,paint);
        //直线
        canvas.drawLine(50,20,500,20,paint);
        //○
        canvas.drawOval(0,100,200,300,paint);
        //长方形
        canvas.drawRect(200,100,600,200,paint);
        //弧线和扇形
        canvas.drawArc(200,500,300,600,30,120,false,paint);
        //图片
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.a),200,500,paint);

视频进度条 暂停 横竖屏切换

package com.example.day1112;

import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, View.OnClickListener {

    private SurfaceView surface_id;
    private SurfaceHolder surfaceHolder;

    private String url = "http://vodkgeyttp8.vod.126.net/cloudmusic/OCFgJDQgIDAxNiAgNSE5IA==/mv/503273/468104880119c923975a4a40e93aa238.mp4?wsSecret=54823b58965d4b6d4b2cb5212c6e1e3e&wsTime=1573541268";

    private MediaPlayer mediaPlayer = new MediaPlayer();
    private SeekBar seekbar;
    private Button button_pause;
    private boolean isplay = true;
    private Handler handler = new Handler();
    private Button button_all;
    private boolean isvertical = true;

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

    private void initPlayer() {
        if (mediaPlayer != null) {
            mediaPlayer.reset();
            try {
                mediaPlayer.setDataSource(url);
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mediaPlayer.start();
                        initTimer();
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    private void initView() {
        surface_id = (SurfaceView) findViewById(R.id.surface_id);
        surfaceHolder = surface_id.getHolder();
        surfaceHolder.addCallback(this);
        seekbar = (SeekBar) findViewById(R.id.seekbar);
        seekbar.setOnClickListener(this);
        button_pause = (Button) findViewById(R.id.button_pause);
        button_pause.setOnClickListener(this);
        button_all = (Button) findViewById(R.id.button_all);

        button_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isvertical){
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    isvertical = false;
                }else {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    isvertical = true;
                }
            }
        });

        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    mediaPlayer.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (mediaPlayer != null) {
            mediaPlayer.setDisplay(holder);

            button_pause.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isplay) {
                        button_pause.setText("播放");
                        mediaPlayer.pause();
                        isplay = false;
                    } else {
                        button_pause.setText("暂停");
                        mediaPlayer.start();
                        isplay = true;
                    }
                }
            });
        }
    }

    Timer timer;

    private void initTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                final int position = mediaPlayer.getCurrentPosition();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        seekbar.setMax(mediaPlayer.getDuration());
                        seekbar.setProgress(position);
                    }
                });
            }
        }, 0, 1000);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

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

    @Override
    public void onClick(View v) {

    }
}

歌词滚动

package com.example.app3_lycir;

import android.Manifest;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.app3_lycir.bean.LyricBean;
import com.example.app3_lycir.bean.LyricsBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    private String lyric = "{\"sgc\":false,\"sfy\":false,\"qfy\":false,\"lyricUser\":{\"id\":29418062,\"status\":0,\"demand\":0,\"userid\":12907497,\"nickname\":\"Simfre\",\"uptime\":1441503572343},\"lrc\":{\"version\":5,\"lyric\":\"[by:简单de频率]\\n[00:00.000] 作曲 : 张超\\n[00:01.000] 作词 : 张超\\n[00:22.600]苍茫的天涯是我的爱\\n[00:26.280]绵绵的青山脚下花正开\\n[00:30.130]什么样的节奏是最呀最摇摆\\n[00:33.880]什么样的歌声才是最开怀\\n[00:37.750]弯弯的河水从天上来\\n[00:41.310]流向那万紫千红一片海\\n[00:45.330]火辣辣的歌谣是我们的期待\\n[00:48.880]一路边走边唱才是最自在\\n[00:52.630]我们要唱就要唱得最痛快\\n[00:56.610]你是我天边最美的云彩\\n[01:00.280]让我用心把你留下来(留下来)\\n[01:04.340]悠悠的唱着最炫的民族风\\n[01:07.840]让爱卷走所有的尘埃\\n[01:10.470](我知道)你是我心中最美的云彩\\n[01:15.380]斟满美酒让你留下来(留下来)\\n[01:19.380]永远都唱着最炫的民族风\\n[01:22.760]是整片天空最美的姿态\\n[01:26.700]哟啦啦呵啦呗\\n[01:28.520]伊啦嗦啦呵啦呗呀\\n[01:30.700]我听见你心中动人的天籁\\n[01:34.260]登上天外云霄的舞台\\n[01:37.530]\\n[01:53.250]苍茫的天涯是我的爱\\n[01:56.940]绵绵的青山脚下花正开\\n[02:00.820]什么样的节奏是最呀最摇摆\\n[02:04.570]什么样的歌声才是最开怀\\n[02:08.440]弯弯的河水从天上来\\n[02:12.000]流向那万紫千红一片海\\n[02:15.810]火辣辣的歌谣是我们的期待\\n[02:19.620]一路边走边唱才是最自在\\n[02:23.310]我们要唱就要唱得最痛快\\n[02:27.220]你是我天边最美的云彩\\n[02:30.920]让我用心把你留下来(留下来)\\n[02:34.920]悠悠的唱着最炫的民族风\\n[02:38.410]让爱卷走所有的尘埃\\n[02:41.410](我知道)你是我心中最美的云彩\\n[02:46.210]斟满美酒让你留下来(留下来)\\n[02:50.140]永远都唱着最炫的民族风\\n[02:53.460]是整片天空最美的姿态\\n[02:57.810]\\n[03:18.290]你是我天边最美的云彩\\n[03:21.910]让我用心把你留下来(留下来)\\n[03:26.030]悠悠的唱着最炫的民族风\\n[03:29.510]让爱卷走所有的尘埃\\n[03:32.260](我知道)你是我心中最美的云彩\\n[03:37.010]斟满美酒让你留下来(留下来)\\n[03:41.070]永远都唱着最炫的民族风\\n[03:44.570]是整片天空最美的姿态\\n[03:48.990]\\n[03:50.300]我听见你心中那动人的天籁\\n[03:53.990]就忽如一夜春风袭来满面桃花开\\n[03:57.860]我忍不住去采 我忍不住去摘\\n[04:01.620]我敞开胸怀为你等待\\n[04:04.340]\\n[04:05.840]你是我天边最美的云彩\\n[04:09.280]让我用心把你留下来\\n[04:13.010]悠悠的唱着最炫的民族风\\n[04:16.750]让爱卷走所有的尘埃\\n[04:19.500](我知道)你是我心中最美的云彩\\n[04:24.250]斟满美酒让你留下来(留下来)\\n[04:28.380]永远都唱着最炫的民族风\\n[04:31.750]是整片天空最美的姿态\\n[04:36.190]\\n\"},\"klyric\":{\"version\":0,\"lyric\":null},\"tlyric\":{\"version\":0,\"lyric\":null},\"code\":200}";
    private List<LyricsBean> lyricsBeans = new ArrayList<>();
    private SurfaceView sv;
    private SurfaceHolder surfaceHolder;
    private MediaPlayer mediaPlayer;
    private int position;
    private Timer timer = new Timer();

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 102);
        }

        initView();
        initLyric();
        initPlayer();
    }

    private void initPlayer() {

        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.reset();
            try {
                mediaPlayer.setDataSource("/sdcard/Music/凤凰传奇 - 最炫民族风(Live).mp3");
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mediaPlayer.start();
                        initTimer();
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void initTimer() {

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                int currentPosition = mediaPlayer.getCurrentPosition();
                for (int i = 0; i < lyricsBeans.size(); i++) {
                    if (currentPosition <= lyricsBeans.get(i).getTime()) {
                        position = i - 1;
                        break;
                    }
                }

            }
        }, 0, 100);
    }


    private void initView() {
        sv = (SurfaceView) findViewById(R.id.sv);
        surfaceHolder = sv.getHolder();
        surfaceHolder.addCallback(this);
    }

    private void initLyric() {
        LyricBean lyricBean = new Gson().fromJson(lyric, LyricBean.class);
        LyricBean.LrcBean lrc = lyricBean.getLrc();
        String lyric = lrc.getLyric();
        String[] split = lyric.split("\n");
        for (int i = 0; i < split.length; i++) {
            String[] strings = split[i].split("\\[|\\:|\\.|\\]");
            if (strings.length > 4) {
                long time = Integer.parseInt(strings[1]) * 60 * 1000 + Integer.parseInt(strings[2]) * 1000 + Integer.parseInt(strings[3]);
                if (strings.length > 5) {
                    lyricsBeans.add(new LyricsBean(strings[4] + ":" + strings[5], time));
                } else {
                    lyricsBeans.add(new LyricsBean(strings[4], time));
                }
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new MyThread().start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

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

    class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setStrokeWidth(50);
            paint.setTextSize(50);
            while (true) {

                Canvas canvas = surfaceHolder.lockCanvas();
                if (canvas == null) {
                    break;
                }
                canvas.drawColor(Color.WHITE);
                for (int i = 0; i < position; i++) {
                    canvas.drawText(lyricsBeans.get(i).getText(), getWindowManager().getDefaultDisplay().getWidth() / 4, 500 - (position - i) * 100, paint);
                }
                canvas.drawText(lyricsBeans.get(position).getText(), getWindowManager().getDefaultDisplay().getWidth() / 4, 500, paint);
                for (int i = position + 1; i < lyricsBeans.size(); i++) {
                    canvas.drawText(lyricsBeans.get(i).getText(), getWindowManager().getDefaultDisplay().getWidth() / 4, 500 + (i - position) * 100, paint);
                }
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

画板

package com.example.day1113;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    private Button button_clear;
    private SurfaceView sv;
    private SurfaceHolder surfaceHolder;
    private Path path = new Path();

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

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        button_clear = (Button) findViewById(R.id.button_clear);
        sv = (SurfaceView) findViewById(R.id.sv);

        surfaceHolder = sv.getHolder();
        surfaceHolder.addCallback(this);

        button_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                path.reset();
            }
        });

        sv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                float x = event.getX();
                float y = event.getY();
                if (action == MotionEvent.ACTION_DOWN){
                    path.moveTo(x,y);
                }else if (action == MotionEvent.ACTION_MOVE){
                    path.lineTo(x,y);
                }

                return true;
            }
        });
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new MyThread().start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();

            Paint paint = new Paint();
            paint.setColor(Color.GREEN);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(20);

            while (true) {
                Canvas canvas = surfaceHolder.lockCanvas();
                if (canvas == null){
                    break;
                }
                canvas.drawColor(Color.WHITE);
                canvas.drawPath(path,paint);
                surfaceHolder.unlockCanvasAndPost(canvas);
            }

        }
    }
}