下面列出了android.view.Surface#unlockCanvasAndPost ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Surface surface = holder.getSurface();
if (surface == null)
return;
try {
Method setSharedBufferMode = Surface.class.getMethod("setSharedBufferMode", boolean.class);
setSharedBufferMode.invoke(surface, true);
displayMessage("Using shared buffer mode.");
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
displayMessage("Shared buffer mode is not supported.");
}
Canvas canvas = surface.lockCanvas(null);
canvas.drawColor(Color.GRAY);
surface.unlockCanvasAndPost(canvas);
}
/**
* Clears the surface, then draws a filled circle with a shadow.
* <p>
* The Canvas drawing we're doing may not be fully implemented for hardware-accelerated
* renderers (shadow layers only supported for text). However, Surface#lockCanvas()
* currently only returns an unaccelerated Canvas, so it all comes out looking fine.
*/
private void drawCircleSurface(Surface surface, int x, int y, int radius) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
paint.setShadowLayer(radius / 4 + 1, 0, 0, Color.RED);
Canvas canvas = surface.lockCanvas(null);
try {
Log.v(TAG, "drawCircleSurface: isHwAcc=" + canvas.isHardwareAccelerated());
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawCircle(x, y, radius, paint);
} finally {
surface.unlockCanvasAndPost(canvas);
}
}
/**
* Clears the surface, then draws a filled circle with a shadow.
* <p>
* The Canvas drawing we're doing may not be fully implemented for hardware-accelerated
* renderers (shadow layers only supported for text). However, Surface#lockCanvas()
* currently only returns an unaccelerated Canvas, so it all comes out looking fine.
*/
private void drawCircleSurface(Surface surface, int x, int y, int radius) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
paint.setShadowLayer(radius / 4 + 1, 0, 0, Color.RED);
Canvas canvas = surface.lockCanvas(null);
try {
Log.v(TAG, "drawCircleSurface: isHwAcc=" + canvas.isHardwareAccelerated());
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawCircle(x, y, radius, paint);
} finally {
surface.unlockCanvasAndPost(canvas);
}
}
private void tryLoadBitmap() {
if(mBitmap != null && getInputHolder().getSurface() != null) {
SurfaceTexture st = getInputHolder().getSurfaceTexture();
Surface s = getInputHolder().getSurface();
// First set the texture resolution
st.setDefaultBufferSize(mBitmap.getWidth(), mBitmap.getHeight());
// Then draw the image data
Canvas c = s.lockCanvas(null);
c.drawBitmap(mBitmap, 0, 0, null);
s.unlockCanvasAndPost(c);
}
}
public void setRectColor(int color) {
Surface surface = getHolder().getSurface();
if (surface == null || !isActive)
return;
Rect rect = new Rect(10, 10, 310, 310);
Canvas canvas = surface.lockCanvas(rect);
Paint paint = new Paint();
paint.setColor(color);
canvas.drawRect(rect, paint);
surface.unlockCanvasAndPost(canvas);
}
/**
* Clears the surface, then draws a filled circle with a shadow.
* <p>
* Similar to drawCircleSurface(), but the position changes based on the value of "i".
*/
private void drawBouncingCircle(Surface surface, int i) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
Canvas canvas = surface.lockCanvas(null);
try {
Trace.beginSection("drawBouncingCircle");
Trace.beginSection("drawColor");
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
Trace.endSection(); // drawColor
int width = canvas.getWidth();
int height = canvas.getHeight();
int radius, x, y;
if (width < height) {
// portrait
radius = width / 4;
x = width / 4 + ((width / 2 * i) / BOUNCE_STEPS);
y = height * 3 / 4;
} else {
// landscape
radius = height / 4;
x = width * 3 / 4;
y = height / 4 + ((height / 2 * i) / BOUNCE_STEPS);
}
paint.setShadowLayer(radius / 4 + 1, 0, 0, Color.RED);
canvas.drawCircle(x, y, radius, paint);
Trace.endSection(); // drawBouncingCircle
} finally {
surface.unlockCanvasAndPost(canvas);
}
}
/**
* Clears the surface, then draws a filled circle with a shadow.
* <p>
* Similar to drawCircleSurface(), but the position changes based on the value of "i".
*/
private void drawBouncingCircle(Surface surface, int i) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
Canvas canvas = surface.lockCanvas(null);
try {
Trace.beginSection("drawBouncingCircle");
Trace.beginSection("drawColor");
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
Trace.endSection(); // drawColor
int width = canvas.getWidth();
int height = canvas.getHeight();
int radius, x, y;
if (width < height) {
// portrait
radius = width / 4;
x = width / 4 + ((width / 2 * i) / BOUNCE_STEPS);
y = height * 3 / 4;
} else {
// landscape
radius = height / 4;
x = width * 3 / 4;
y = height / 4 + ((height / 2 * i) / BOUNCE_STEPS);
}
paint.setShadowLayer(radius / 4 + 1, 0, 0, Color.RED);
canvas.drawCircle(x, y, radius, paint);
Trace.endSection(); // drawBouncingCircle
} finally {
surface.unlockCanvasAndPost(canvas);
}
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
final Surface surface = new Surface(surfaceTexture);
final Canvas canvas = surface.lockCanvas(null);
mDrawer.onDrawPlaceholder(canvas);
surface.unlockCanvasAndPost(canvas);
surface.release();
}
/**
* Draw color bars with text labels.
*/
private void drawColorBars(Surface surface) {
Canvas canvas = surface.lockCanvas(null);
try {
// TODO: if the device is in portrait, draw the color bars horizontally. Right
// now this only looks good in landscape.
int width = canvas.getWidth();
int height = canvas.getHeight();
int least = Math.min(width, height);
Log.d(TAG, "Drawing color bars at " + width + "x" + height);
Paint textPaint = new Paint();
Typeface typeface = Typeface.defaultFromStyle(Typeface.NORMAL);
textPaint.setTypeface(typeface);
textPaint.setTextSize(least / 20);
textPaint.setAntiAlias(true);
Paint rectPaint = new Paint();
for (int i = 0; i < 8; i++) {
int color = 0xff000000;
if ((i & 0x01) != 0) {
color |= 0x00ff0000;
}
if ((i & 0x02) != 0) {
color |= 0x0000ff00;
}
if ((i & 0x04) != 0) {
color |= 0x000000ff;
}
rectPaint.setColor(color);
float sliceWidth = width / 8;
canvas.drawRect(sliceWidth * i, 0, sliceWidth * (i+1), height, rectPaint);
}
rectPaint.setColor(0x80808080); // ARGB 50/50 grey (non-premul)
float sliceHeight = height / 8;
int posn = 6;
canvas.drawRect(0, sliceHeight * posn, width, sliceHeight * (posn+1), rectPaint);
// Draw the labels last so they're on top of everything.
for (int i = 0; i < 8; i++) {
drawOutlineText(canvas, textPaint, COLOR_NAMES[i],
(width / 8) * i + 4, (height / 8) * ((i & 1) + 1));
}
} finally {
surface.unlockCanvasAndPost(canvas);
}
}