Java Code Examples for android.view.SurfaceHolder#unlockCanvasAndPost()

The following examples show how to use android.view.SurfaceHolder#unlockCanvasAndPost() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
private synchronized void drawFrame() {
  final SurfaceHolder holder = getSurfaceHolder();

  if (holder != null && holder.getSurface().isValid()) {
    Canvas canvas = null;
    try {
      canvas = holder.lockCanvas();
      if (canvas != null) {
        // Draw on the Canvas!
      }
    } finally {
      if (canvas != null && holder != null)
        holder.unlockCanvasAndPost(canvas);
    }

    // Schedule the next frame
    handler.removeCallbacks(drawSurface);
  }
  handler.postDelayed(drawSurface, 1000 / FPS);
}
 
Example 2
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    super.onSurfaceChanged(holder, format, width, height);
    if (DOLOG)
        Log.d(TAG, "onSurfaceChanged: width = [" + width + "], height = [" + height + "]");

    display = new Display(BlockDropWallpaper.this);

    if (height > width) {
        scale = (float) width / (float) height;
        yCenter = height / 2f;
    } else {
        scale = 0.8f;
        yCenter = (height / scale) / (2f * scale);
    }
    xCenter = width / 2f;

    if (getState() != STATE_PAUSED) {
        // Manually redraw with the new dimensions (it won't redraw automatically when paused)
        Canvas canvas = holder.lockCanvas();
        draw(canvas);
        holder.unlockCanvasAndPost(canvas);
    }
}
 
Example 3
Source Project: Asteroid   File: BackgroundService.java    License: Apache License 2.0 6 votes vote down vote up
private void draw(SurfaceHolder holder) {
    if (holder == null || !holder.getSurface().isValid())
        return;

    Canvas canvas;
    try {
        canvas = holder.lockCanvas();
        if (canvas == null) return;
    } catch (Exception e) {
        return;
    }

    canvas.drawColor(Color.BLACK);

    background.draw(canvas, particleSpeed);

    holder.unlockCanvasAndPost(canvas);

    handler.removeCallbacks(runnable);
    handler.postDelayed(runnable, 10);
}
 
Example 4
@SuppressLint("WrongCall")
@Override
public void surfaceCreated(SurfaceHolder holder) {
    Canvas canvas = null;
    try {
        canvas = holder.lockCanvas(null);
        synchronized (holder) {
            onDraw(canvas);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (canvas != null) {
            holder.unlockCanvasAndPost(canvas);
        }
    }
}
 
Example 5
Source Project: meter   File: MeterWallpaper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Draw function doing the context locking and rendering
 */
private void draw() {
    if(mDrawer == null) return;

    // Ask the drawer if wants to draw in this frame
    if(mDrawer.shouldDraw()) {
        SurfaceHolder holder = getSurfaceHolder();
        Canvas c = null;
        try {
            // Lock the drawing canvas
            c = holder.lockCanvas();
            if (c != null) {
                // Let the drawer render to the canvas
                mDrawer.draw(c);
            }
        } finally {
            if (c != null) holder.unlockCanvasAndPost(c);
        }
    }
    mHandler.removeCallbacks(mUpdateDisplay);
    if (mVisible) {
        // Wait one frame, and redraw
        mHandler.postDelayed(mUpdateDisplay, 33);
    }
}
 
Example 6
private void draw() {
	SurfaceHolder holder = getSurfaceHolder();
	Canvas canvas = null;
	try {
		canvas = holder.lockCanvas();
		if (canvas != null) {
			if (circles.size() >= maxNumber) {
				circles.clear();
			}
			int x = (int) (width * Math.random());
			int y = (int) (height * Math.random());
			circles.add(new MyPoint(String.valueOf(circles.size() + 1),
					x, y));
			drawCircles(canvas, circles);
		}
	} finally {
		if (canvas != null)
			holder.unlockCanvasAndPost(canvas);
	}
	handler.removeCallbacks(drawRunner);
	if (visible) {
		handler.postDelayed(drawRunner, 5000);
	}
}
 
Example 7
Source Project: Android-ScratchOutView   File: ScratchOutView.java    License: MIT License 6 votes vote down vote up
private void invalidSurfaceView() {
    Canvas canvas = null;
    SurfaceHolder surfaceHolder = getHolder();
    try {
        canvas = surfaceHolder.lockCanvas(null);
        if (canvas != null) {
            draw(canvas);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (canvas != null) {
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
}
 
Example 8
private void draw() {
	SurfaceHolder holder = getSurfaceHolder();
	Canvas canvas = null;
	try {
		canvas = holder.lockCanvas();
		if (canvas != null) {
			draw(canvas);
		}
	} finally {
		if (canvas != null)
			holder.unlockCanvasAndPost(canvas);
	}

	handler.removeCallbacks(drawRunner);

	if (visible) {
		handler.postDelayed(drawRunner, 200);
	}
}
 
Example 9
@Override
public void onTouchEvent(MotionEvent event) {
	if (touchEnabled) {

		float x = event.getX();
		float y = event.getY();
		SurfaceHolder holder = getSurfaceHolder();
		Canvas canvas = null;
		try {
			canvas = holder.lockCanvas();
			if (canvas != null) {
				canvas.drawColor(Color.BLACK);
				circles.clear();
				circles.add(new MyPoint(
						String.valueOf(circles.size() + 1), x, y));
				drawCircles(canvas, circles);

			}
		} finally {
			if (canvas != null)
				holder.unlockCanvasAndPost(canvas);
		}
		super.onTouchEvent(event);
	}
}
 
Example 10
Source Project: JPPF   File: FractalSurfaceView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  try {
    while (!stopped.get()) {
      if (!queue.isEmpty() || firstDraw.get()) {
        SurfaceHolder holder = getHolder();
        if (holder != null) {
          Canvas canvas = null;
          try {
            canvas = holder.lockCanvas();
            synchronized(holder) {
              doDraw(canvas);
            }
          } finally {
            if (canvas != null) holder.unlockCanvasAndPost(canvas);
          }
        }
      } else {
        synchronized(this) {
          wait(100L);
        }
      }
    }
  } catch(Exception e) {
    Log.e(LOG_TAG, "Exception in ViewThread.run()", e);
  }
}
 
Example 11
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    isSurfaceCreated = true;
    Canvas canvas = surfaceHolder.lockCanvas();
    if (canvas != null) {
        DrawHelper.clearCanvas(canvas);
        surfaceHolder.unlockCanvasAndPost(canvas);
    }
}
 
Example 12
Source Project: FastAndroid   File: FaceRecognizeView.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 画控件
 */
private void drawView() {
    synchronized (mSync) {
        Canvas canvas = null;
        SurfaceHolder holder = null;
        try {
            holder = getHolder();
            if (holder == null || !holder.getSurface().isValid()) return;
            //获得canvas对象
            canvas = holder.lockCanvas();
            if (canvas == null) return;
            //清除画布上面里面的内容
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            //绘制画布内容
            drawContent(canvas);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (holder != null && canvas != null) {
                //释放canvas锁,并且显示视图
                try {
                    holder.unlockCanvasAndPost(canvas);
                } catch (IllegalArgumentException e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
}
 
Example 13
Source Project: FastAndroid   File: FaceRegisterView.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 画控件
 */
private void drawView() {
    synchronized (mSync) {
        Canvas canvas = null;
        SurfaceHolder holder = null;
        try {
            holder = getHolder();
            if (holder == null || !holder.getSurface().isValid()) return;
            //获得canvas对象
            canvas = holder.lockCanvas();
            if (canvas == null) return;
            //清除画布上面里面的内容
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            //绘制画布内容
            drawContent(canvas);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (holder != null && canvas != null) {
                //释放canvas锁,并且显示视图
                try {
                    holder.unlockCanvasAndPost(canvas);
                } catch (IllegalArgumentException e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
}
 
Example 14
Source Project: letv   File: DanmakuSurfaceView.java    License: Apache License 2.0 5 votes vote down vote up
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    this.isSurfaceCreated = true;
    Canvas canvas = surfaceHolder.lockCanvas();
    if (canvas != null) {
        DrawHelper.clearCanvas(canvas);
        surfaceHolder.unlockCanvasAndPost(canvas);
    }
}
 
Example 15
Source Project: LiveWallpaper   File: LiveWallpaperView.java    License: Apache License 2.0 5 votes vote down vote up
private void drawSurfaceView(SurfaceHolder holder) {
    if (this.mNextBitmap != null && !this.mNextBitmap.isRecycled()) {
        Canvas localCanvas = holder.lockCanvas();
        if (localCanvas != null) {
            Rect rect = new Rect();
            rect.left = rect.top = 0;
            rect.bottom = localCanvas.getHeight();
            rect.right = localCanvas.getWidth();
            localCanvas.drawBitmap(this.mNextBitmap, null, rect, this.mPaint);
            holder.unlockCanvasAndPost(localCanvas);
        }
    }
}
 
Example 16
private void draw(SurfaceHolder holder) {
    this.mDrawRequested = false;
    Canvas canvas = holder.lockCanvas();
    if (canvas == null) {
        return;
    }
    try {
        onDraw(canvas, holder.getSurfaceFrame());
    } finally {
        holder.unlockCanvasAndPost(canvas);
    }
}
 
Example 17
private void drawText(SurfaceView surfaceView, String text) {
    SurfaceHolder surfaceHolder = surfaceView.getHolder();
    Canvas canvas = surfaceHolder.lockCanvas();
    if (canvas == null)
        return;
    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);//清空画布
    Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
    textPaint.setTextSize(25.0f);               //字体大小
    textPaint.setTypeface(Typeface.DEFAULT_BOLD);               //粗体
    textPaint.setColor(Color.YELLOW);                   //黄色
    //添加水印
    canvas.drawText(text, surfaceView.getWidth() / 2 - 90, surfaceView.getHeight() / 2.0f, textPaint);
    surfaceHolder.unlockCanvasAndPost(canvas);
}
 
Example 18
Source Project: drip-steps   File: DripWallpaperService.java    License: Apache License 2.0 5 votes vote down vote up
private void draw() {
	SurfaceHolder holder = getSurfaceHolder();
	Canvas canvas = null;
	try {
		// redrawEverything means that the whole screen is dirty, reset all params and redraw everything
		if (redrawEverything) {
			initDimensParam(width, height);
			canvas = holder.lockCanvas();
		} else {
			canvas = holder.lockCanvas(textRect);
		}
		if (canvas != null) {
			draw(canvas);
		}
	} finally {
		if (canvas != null) {
			try {
				holder.unlockCanvasAndPost(canvas);
			} catch (Exception ignore) {
			}
		}
	}

	if (visible) {
		doFrame();
	}
}
 
Example 19
Source Project: brailleback   File: SearchView.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void invalidate() {
    super.invalidate();

    final SurfaceHolder holder = mHolder;
    if (holder == null) {
        return;
    }

    final Canvas canvas = holder.lockCanvas();
    if (canvas == null) {
        return;
    }

    // Clear the canvas.
    canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);

    if (getVisibility() != View.VISIBLE) {
        holder.unlockCanvasAndPost(canvas);
        return;
    }

    final int width = getWidth();
    final int height = getHeight();

    // Draw the pretty gradient background.
    mGradientBackground.setBounds(0, 0, width, height);
    mGradientBackground.draw(canvas);

    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setStyle(Style.FILL);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(mContext.getResources().getDimensionPixelSize(
            R.dimen.search_text_font_size));
    canvas.drawText(
            mContext.getString(R.string.search_dialog_label, mQueryText),
            width / 2.0f,
            height / 2.0f,
            paint);

    holder.unlockCanvasAndPost(canvas);
}
 
Example 20
Source Project: IndicatorBox   File: BounceFreeBar.java    License: MIT License 4 votes vote down vote up
@Override
public void surfaceCreated(SurfaceHolder holder) {
    Canvas c = holder.lockCanvas();
    draw(c);
    holder.unlockCanvasAndPost(c);
}