【问题标题】:blurry background of activity, without knowing what avtivity is behind活动背景模糊,不知道后面是什么活动
【发布时间】:2015-11-21 14:09:13
【问题描述】:

我有一个每次打开手机都会跳的活动。

我希望它的背景是模糊的,所以在我之后应该打开的任何活动都会是模糊的。但是我不知道后面应该是什么活动,

因为它不是来自我的应用的活动,可能来自任何应用。

所以我不能使用模糊我从这里使用的最后一个活动的方法

Android, how to blur/glass/frost current activity

这里说明了我创建了一个模糊生成器

Create blurry transparent background effect

喜欢这个

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.view.View;
public class BlurBuilder {
    private static final float BITMAP_SCALE = 0.4f;
    private static final float BLUR_RADIUS = 7.5f;

    public static Bitmap blur(View v) {
        return blur(v.getContext(), getScreenshot(v));
    }

    public static Bitmap blur(Context ctx, Bitmap image) {
        int width = Math.round(image.getWidth() * BITMAP_SCALE);
        int height = Math.round(image.getHeight() * BITMAP_SCALE);

        Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

        RenderScript rs = RenderScript.create(ctx);
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
        theIntrinsic.setRadius(BLUR_RADIUS);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);

        return outputBitmap;
    }

    private static Bitmap getScreenshot(View v) {
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }
}

但我不确定如何在我的活动的 onCreate 上使用它。

谢谢!

【问题讨论】:

    标签: android css android-activity background-image


    【解决方案1】:

    在您的onCreate() 中使用它:

    对于片段:

    final Activity activity = getActivity();
    final View content = activity.findViewById(android.R.id.content).getRootView();
        if (content.getWidth() > 0) {
            Bitmap image = BlurBuilder.blur(content);
            content.setBackground(new BitmapDrawable(activity.getResources(), image));
    }
    

    对于活动:

    final View content = findViewById(android.R.id.content).getRootView();
    if (content.getWidth() > 0) {
            Bitmap image = BlurBuilder.blur(content);
            content.setBackground(new BitmapDrawable(getResources(), image));
    }
    

    编辑:

    onCreate() 中的模糊解决方案,添加:

    new CountDownTimer(1, 1) {
    public void onTick(long millisUntilFinished) {
            }
    
    public void onFinish() {
                final View content = findViewById(android.R.id.content).getRootView();
                if (content.getWidth() > 0) {
                    Bitmap image = BlurBuilder.blur(content);
                    content.setBackground(new BitmapDrawable(getResources(), image));
                }
    
            }
    }.start();
    

    【讨论】:

    • 相信我,它有效,你如何使用代码? @Mumfordwiz
    • 我将它添加到我的活动的 onCreate 中
    • 现在每次它应该跳它都会崩溃
    • 我完全按照你说的方式使用了。
    • 如果你使用 Fragment 进行 Blur 效果很好,现在我在 Activty 上试了试,它并没有按照你喜欢的方式工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    相关资源
    最近更新 更多