SurfaceView概念:
SurfaceView本身是一个View,符合一切View的特性,需要通过Canvas画布绘制。
SurfaceView与View的区别
View 主要适用于主动更新的情况,而 surfaceView 主要适用于被动更新,例如频繁的刷新。
View 在主线程中对画面进行刷新,而 surfaceView 通常会通过一个子线程来进行页面的刷新
View 在绘图时没有使用双缓冲机制,而 surfaceView 在底层实现机制上就已经实现了双缓冲机制。
总结就是,如果你的自定义 View 需要频繁刷新,或者刷新时数据处理量很大,考虑用 SurfaceView 来替代 View。
效果
实现视频的播放

xml布局
<?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=".play.Main2Activity">
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始"
/>
<Button
android:id="@+id/pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="暂停"
/>
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/surface"
/>
</LinearLayout>
activity代码
package com.example.day10exam.play;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import com.example.day10exam.R;
import java.io.IOException;
public class Main2Activity extends AppCompatActivity implements SurfaceHolder.Callback {
private Button start;
private Button pause;
private SurfaceView surface;
private SurfaceHolder holder;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
start = (Button) findViewById(R.id.start);
pause = (Button) findViewById(R.id.pause);
surface = (SurfaceView) findViewById(R.id.surface);
holder = surface.getHolder();
holder.addCallback(this);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
});
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(holder);
try {
mediaPlayer.setDataSource("https://vd3.bdstatic.com/mda-hkpwq17u3qwybp5d/mda-hkpwq17u3qwybp5d.mp4?auth_key=1565706208-0-0-b8db557efc8a60b424c1af965b2f99c1&bcevod_channel=searchbox_feed&pd=bjh");
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
效果
实现字体滚动

xml布局
<?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=".play.Main3Activity">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/surface"
/>
</LinearLayout>
activity代码
package com.example.day10exam.play;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import com.example.day10exam.R;
import java.io.IOException;
public class Main3Activity extends AppCompatActivity implements SurfaceHolder.Callback {
private SurfaceView surface;
private Button start;
private Button pause;
private SurfaceHolder holder;
private int count;
private MediaPlayer mediaPlayer;
private String s="好好学习天天向上好好学习天天向上好好学习天天向上好好学习天天向上";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
surface = (SurfaceView) findViewById(R.id.surface);
start = (Button) findViewById(R.id.start);
pause = (Button) findViewById(R.id.pause);
holder = surface.getHolder();
holder.addCallback(this);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
});
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer = new MediaPlayer();
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();
while (true){
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAlpha(100);
paint.setTextSize(80);
try {
Thread.sleep(100);
count-=10;
} catch (InterruptedException e) {
e.printStackTrace();
}
Canvas canvas = holder.lockCanvas();
if (canvas==null){
break;
}
canvas.drawColor(PixelFormat.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawText(s,count,100,paint);
holder.unlockCanvasAndPost(canvas);
}
}
}
}

本文深入探讨了Android中SurfaceView的概念及其与View的区别,重点介绍了SurfaceView在视频播放和字体滚动动画中的应用。通过实例代码,展示了如何利用SurfaceView实现视频播放的控制和自定义字体滚动效果。
1174

被折叠的 条评论
为什么被折叠?



