23
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SurfaceViewの背景を透過する

Posted at

ポイントは以下の2点です。

  • setFormat(PixelFormat.TRANSLUCENT) で背景に半透明を設定する
  • setZOrderOnTop(true) で SurfaceView を一番上に表示させる
OverlayCanvas.java
package com.example;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.text.AttributedCharacterIterator;

public class OverlayCanvas extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private SurfaceHolder mHolder;
    private Thread mLooper;

    public OverlayCanvas(Context context) {
        super(context);
        initialize();
    }

    // カスタムビューとしてXMLに定義するときに必要なコンストラクタ
    public OverlayCanvas(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    private void initialize() {
        // 半透明を設定
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
        // コールバック登録
        getHolder().addCallback(this);
        // フォーカス可
        setFocusable(true);
        // このViewをトップにする
        setZOrderOnTop(true);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        mHolder = holder;
        mLooper = new Thread(this);
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (mLooper != null) {
            mLooper.start();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        mLooper = null;
    }

    public void run() {
        while (mLooper != null) {
            doDraw();
        }
    }

    private void doDraw() {
        Canvas canvas = mHolder.lockCanvas();
        /*
        描画更新処理をかく
         */
        mHolder.unlockCanvasAndPost(canvas);
    }
}

上記を layout ファイルに書くなら、こんな感じ。

main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <!-- background は何か適当な画像 -->
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/xxxx">
    </ImageView>
    <com.example.OverlayCanvas
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
</FrameLayout>
23
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?