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

The following examples show how to use android.view.SurfaceHolder#lockCanvas() . 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 check out the related API usage on the sidebar.
Example 1
Source File: MyWallpaperService.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@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 2
Source File: MyWallpaperService.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
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 3
Source File: BlockDropWallpaper.java    From homescreenarcade with GNU General Public License v3.0 6 votes vote down vote up
@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 4
Source File: BackgroundService.java    From Asteroid with 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 5
Source File: ClockWallpaperService.java    From android-clock-livewallpaper with MIT License 6 votes vote down vote up
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 6
Source File: ScratchOutView.java    From Android-ScratchOutView with 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 7
Source File: MeterWallpaper.java    From meter with 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 8
Source File: ImageSurface.java    From camera-ruler with GNU General Public License v2.0 6 votes vote down vote up
@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 9
Source File: MyWallpaperService.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
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 10
Source File: CanvasWatchFaceService.java    From AndroidWear-OpenWear with MIT License 5 votes vote down vote up
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 11
Source File: DanmakuSurfaceView.java    From BlueBoard with Apache License 2.0 5 votes vote down vote up
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    isSurfaceCreated = true;
    Canvas canvas = surfaceHolder.lockCanvas();
    if (canvas != null) {
        DrawHelper.clearCanvas(canvas);
        surfaceHolder.unlockCanvasAndPost(canvas);
    }
}
 
Example 12
Source File: DripWallpaperService.java    From drip-steps with 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 13
Source File: FractalSurfaceView.java    From JPPF with 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 14
Source File: LiveWallpaperView.java    From LiveWallpaper with 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 15
Source File: DanmakuSurfaceView.java    From letv with 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 16
Source File: FaceRegisterView.java    From FastAndroid with 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 17
Source File: FaceRecognizeView.java    From FastAndroid with 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 18
Source File: DanmakuSurfaceView.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    isSurfaceCreated = true;
    Canvas canvas = surfaceHolder.lockCanvas();
    if (canvas != null) {
        DrawHelper.clearCanvas(canvas);
        surfaceHolder.unlockCanvasAndPost(canvas);
    }
}
 
Example 19
Source File: BounceFreeBar.java    From IndicatorBox with MIT License 4 votes vote down vote up
@Override
public void surfaceCreated(SurfaceHolder holder) {
    Canvas c = holder.lockCanvas();
    draw(c);
    holder.unlockCanvasAndPost(c);
}
 
Example 20
Source File: SearchView.java    From brailleback with 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);
}