blog.octoling.ink
Table of Contents
  • 概要
  • ベースとなるLibrary
  • コード
  • 解説
Androidで円形のリアルタイムBlur効果を付ける
投稿日: 2024-04-27
カテゴリ: Android
タグ: AndroidCustomView
RealtimeBlurViewを改造して円形にした

概要

リアルタイムに円形のViewでBlur効果を実現したので、備忘録としてまとめておきます。

ベースとなるLibrary

mmin18氏の「RealtimeBlurView」を改造します。
jcenterがEOLとなり、READMEの通りに導入しようとするとインポート出来ないのでjitpackから取ります。

    dependencies {
            implementation 'com.github.mmin18:RealtimeBlurView:-SNAPSHOT'
    }

コード

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.util.AttributeSet;

import androidx.annotation.NonNull;

import com.github.mmin18.widget.RealtimeBlurView;

public class BlurCircleView extends RealtimeBlurView {

    private Path clipPath;

    public BlurCircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        clipPath = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        // Update the clip path when the size changes
        updateClipPath(w, h);
    }

    private void updateClipPath(int width, int height) {
        // Calculate radius
        float radius = Math.min(width, height) / 2f;

        // Create a circular path
        clipPath.reset();
        clipPath.addCircle(width / 2f, height / 2f, radius, Path.Direction.CW);
        clipPath.close();
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        // Clip the canvas to the circular path
        canvas.clipPath(clipPath);

        super.onDraw(canvas);
    }
}

解説

特殊な処理などは無く、単純に円形にClipするだけです。

カテゴリ一覧

Android

その他

バックエンド

タグ一覧

Android

CustomView

other

network

loadbalance

vpn

l4

dsr

© 2023 octoling.ink. All Rights Reserved.
  • About
  • X(Twitter)
  • Misskey
  • GitHub
  • Booyah!