surfaceview
程序员文章站
2022-03-29 19:43:18
...
@[toc] surfaceview
activity
package com.example.day10_cinema_exe;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.VideoView;
import java.net.URI;
public class MainActivity extends AppCompatActivity {
private Button call;
private Button videos;
private Button photos;
private Button picKu;
private Button liulan;
private Button jietu;
private VideoView videoView;
private ImageView imv;
@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,
Manifest.permission.CALL_PHONE,
Manifest.permission.CAMERA
}, 120);
}
initView();
}
private void initView() {
call = (Button) findViewById(R.id.call);
videos = (Button) findViewById(R.id.videos);
photos = (Button) findViewById(R.id.photos);
picKu = (Button) findViewById(R.id.pic_ku);
liulan = (Button) findViewById(R.id.liulan);
jietu = (Button) findViewById(R.id.jietu);
videoView = (VideoView) findViewById(R.id.video_view);
imv = (ImageView) findViewById(R.id.imv);
//打电话
call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "1861136050"));
startActivity(intent);
}
});
//打开浏览器
liulan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.iconfont.cn/home/index?spm=a313x.7781069.1998910419.2"));
startActivity(intent);
}
});
//录像
videos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 101);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
Uri data1 = data.getData();
videoView.setVideoURI(data1);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});
} else if (requestCode == 102 && resultCode == Activity.RESULT_OK) {
Uri data1 = data.getData();
imv.setImageURI(data1);
}
}
}
package com.example.day1112_homework;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private float x;
private float y = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
x = getWindowManager().getDefaultDisplay().getWidth();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
new DanMuThread().start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
private void initView() {
surfaceView = (SurfaceView) findViewById(R.id.ss);
}
class DanMuThread extends Thread {
@Override
public void run() {
super.run();
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(30);
paint.setAntiAlias(true);
while (true) {
try {
Thread.sleep(10);
Canvas canvas = surfaceHolder.lockCanvas();
if (canvas == null) {
break;
}
canvas.drawColor(Color.WHITE);
canvas.drawText("来啊,快活啊!", x-=10, y, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}