Help us understand the problem. What is going on with this article?

SurfaceViewの背景を透過する

More than 5 years have passed since last update.

ポイントは以下の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>
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away