| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.test.hwui; |
| 18 |
|
| 19 |
import android.app.Activity; |
| 20 |
import android.graphics.Bitmap; |
| 21 |
import android.net.Uri; |
| 22 |
import android.os.Bundle; |
| 23 |
import android.os.Environment; |
| 24 |
import android.view.PixelCopy; |
| 25 |
import android.view.View; |
| 26 |
import android.widget.Button; |
| 27 |
import android.widget.FrameLayout; |
| 28 |
import android.widget.LinearLayout; |
| 29 |
import android.widget.Toast; |
| 30 |
import android.widget.VideoView; |
| 31 |
|
| 32 |
import java.io.FileOutputStream; |
| 33 |
|
| 34 |
public class VideoViewCaptureActivity extends Activity { |
| 35 |
private VideoView mVideoView; |
| 36 |
private int mVideoWidth, mVideoHeight; |
| 37 |
|
| 38 |
@Override |
| 39 |
protected void onCreate(Bundle savedInstanceState) { |
| 40 |
super.onCreate(savedInstanceState); |
| 41 |
mVideoView = new VideoView(this); |
| 42 |
mVideoView.setOnPreparedListener(mp -> { |
| 43 |
mp.setLooping(true); |
| 44 |
mVideoWidth = mp.getVideoWidth(); |
| 45 |
mVideoHeight = mp.getVideoHeight(); |
| 46 |
mVideoView.start(); |
| 47 |
}); |
| 48 |
|
| 49 |
Uri uri = Uri.parse("android.resource://com.android.test.hwui/" + R.raw.colorgrid_video); |
| 50 |
mVideoView.setVideoURI(uri); |
| 51 |
|
| 52 |
Button button = new Button(this); |
| 53 |
button.setText("Copy bitmap to /sdcard/surfaceview.png"); |
| 54 |
button.setOnClickListener((View v) -> { |
| 55 |
final Bitmap b = Bitmap.createBitmap( |
| 56 |
mVideoWidth, mVideoHeight, |
| 57 |
Bitmap.Config.ARGB_8888); |
| 58 |
PixelCopy.request(mVideoView, b, |
| 59 |
(int result) -> { |
| 60 |
if (result != PixelCopy.SUCCESS) { |
| 61 |
Toast.makeText(VideoViewCaptureActivity.this, |
| 62 |
"Failed to copy", Toast.LENGTH_SHORT).show(); |
| 63 |
return; |
| 64 |
} |
| 65 |
try { |
| 66 |
try (FileOutputStream out = new FileOutputStream( |
| 67 |
Environment.getExternalStorageDirectory() + "/surfaceview.png");) { |
| 68 |
b.compress(Bitmap.CompressFormat.PNG, 100, out); |
| 69 |
} |
| 70 |
} catch (Exception e) { |
| 71 |
|
| 72 |
} |
| 73 |
}, mVideoView.getHandler()); |
| 74 |
}); |
| 75 |
|
| 76 |
FrameLayout content = new FrameLayout(this); |
| 77 |
LinearLayout layout = new LinearLayout(this); |
| 78 |
layout.setOrientation(LinearLayout.VERTICAL); |
| 79 |
layout.addView(button, LinearLayout.LayoutParams.MATCH_PARENT, |
| 80 |
LinearLayout.LayoutParams.WRAP_CONTENT); |
| 81 |
layout.addView(mVideoView, LinearLayout.LayoutParams.MATCH_PARENT, |
| 82 |
LinearLayout.LayoutParams.MATCH_PARENT); |
| 83 |
|
| 84 |
content.addView(layout, new FrameLayout.LayoutParams( |
| 85 |
FrameLayout.LayoutParams.MATCH_PARENT, |
| 86 |
FrameLayout.LayoutParams.MATCH_PARENT)); |
| 87 |
setContentView(content); |
| 88 |
} |
| 89 |
} |