【问题标题】:How to blur the background image of the main layout in android?如何在android中模糊主布局的背景图像?
【发布时间】:2015-05-26 00:46:01
【问题描述】:

我只是想让背景图像看起来模糊(如散焦),我使用了 alpha,但它不仅将 alpha 设置为我的背景图像,还设置了整个内容...有什么方法可以设置仅对我的背景图像产生模糊效果!!!..

需要帮助提前谢谢!!..

【问题讨论】:

标签: android alpha


【解决方案1】:

请使用下面的教程来模糊背景

  1. 导航抽屉: https://github.com/charbgr/BlurNavigationDrawer

  2. 片段: https://github.com/tvbarthel/BlurDialogFragment

  3. 图像:如果要在布局中模糊图像: https://github.com/kikoso/android-stackblur

  4. 布局: https://github.com/PomepuyN/BlurEffectForAndroidDesign

    公共类 MainActivity 扩展 Activity {

    private static final String BLURRED_IMG_PATH = "blurred_image.png";
    private static final int TOP_HEIGHT = 700;
    private ListView mList;
    private ImageView mBlurredImage;
    private View headerView;
    private ImageView mNormalImage;
    private ScrollableImageView mBlurredImageHeader;
    private Switch mSwitch;
    private float alpha;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
    
        // Get the screen width
        final int screenWidth = ImageUtils.getScreenWidth(this);
    
        // Find the view
        mBlurredImage = (ImageView) findViewById(R.id.blurred_image);
        mNormalImage = (ImageView) findViewById(R.id.normal_image);
        mBlurredImageHeader = (ScrollableImageView) findViewById(R.id.blurred_image_header);
        mSwitch = (Switch) findViewById(R.id.background_switch);
        mList = (ListView) findViewById(R.id.list);
    
        // prepare the header ScrollableImageView
        mBlurredImageHeader.setScreenWidth(screenWidth);
    
        // Action for the switch
        mSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mBlurredImage.setAlpha(alpha);
                } else {
                    mBlurredImage.setAlpha(0f);
    
                }
    
            }
        });
    
        // Try to find the blurred image
        final File blurredImage = new File(getFilesDir() + BLURRED_IMG_PATH);
        if (!blurredImage.exists()) {
    
            // launch the progressbar in ActionBar
            setProgressBarIndeterminateVisibility(true);
    
            new Thread(new Runnable() {
    
                @Override
                public void run() {
    
                    // No image found => let's generate it!
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 2;
                    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.image, options);
                    Bitmap newImg = Blur.fastblur(MainActivity.this, image, 12);
                    ImageUtils.storeImage(newImg, blurredImage);
                    runOnUiThread(new Runnable() {
    
                        @Override
                        public void run() {
                            updateView(screenWidth);
    
                            // And finally stop the progressbar
                            setProgressBarIndeterminateVisibility(false);
                        }
                    });
    
                }
            }).start();
    
        } else {
    
            // The image has been found. Let's update the view
            updateView(screenWidth);
    
        }
    
        String[] strings = getResources().getStringArray(R.array.list_content);
    
        // Prepare the header view for our list
        headerView = new View(this);
        headerView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, TOP_HEIGHT));
        mList.addHeaderView(headerView);
        mList.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, strings));
        mList.setOnScrollListener(new OnScrollListener() {
    
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
    
            }
    
            /**
             * Listen to the list scroll. This is where magic happens ;)
             */
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    
                // Calculate the ratio between the scroll amount and the list
                // header weight to determinate the top picture alpha
                alpha = (float) -headerView.getTop() / (float) TOP_HEIGHT;
                // Apply a ceil
                if (alpha > 1) {
                    alpha = 1;
                }
    
                // Apply on the ImageView if needed
                if (mSwitch.isChecked()) {
                    mBlurredImage.setAlpha(alpha);
                }
    
                // Parallax effect : we apply half the scroll amount to our
                // three views
                mBlurredImage.setTop(headerView.getTop() / 2);
                mNormalImage.setTop(headerView.getTop() / 2);
                mBlurredImageHeader.handleScroll(headerView.getTop() / 2);
    
            }
        });
    }
    
    private void updateView(final int screenWidth) {
        Bitmap bmpBlurred = BitmapFactory.decodeFile(getFilesDir() + BLURRED_IMG_PATH);
        bmpBlurred = Bitmap.createScaledBitmap(bmpBlurred, screenWidth, (int) (bmpBlurred.getHeight()
                * ((float) screenWidth) / (float) bmpBlurred.getWidth()), false);
    
        mBlurredImage.setImageBitmap(bmpBlurred);
    
        mBlurredImageHeader.setoriginalImage(bmpBlurred);
    }
    

    }

【讨论】:

  • 我的图标和文字是白色的.....如果我使用白色图像......那么一切都变得不可见......我想在图像上设置一些灰色效果,这样我可以看到我的图标和文字..
【解决方案2】:

Kotlin 代码,使用视图效果库:

1- 在 build.gradle 中添加库:

    implementation 'com.github.mirrajabi:view-effects:e355a1bac4'

2- 模糊根视图或视图的背景,这里Constraint Layout模糊了20%

 ViewFilter.getInstance(this)
        .setRenderer(  BlurRenderer(20))
        .applyFilterOnView(  root_constraintLayout,
            root_constraintLayout    )

我用于模糊背景的 github 存储库:link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多