0

我有一个特定的问题,我试图在整个互联网上找到,但没有运气。用户将选择一个男人的模型(图像视图)并通过移动它的位置,模糊效果将从上到下出现/ 从右到左的男人。基本上,我需要在图像视图上使用模糊效果,但可以选择设置我想要模糊的图像部分。

像这样的东西

https://www.freepik.com/free-photo/young-man-walking_1214201.htm'>由 Freepik 设计

4

2 回答 2

0

如果我正确理解您的问题,您需要一个在特定部分模糊的移动 ImageView。为此,您可以使用 onTouch 事件将 ImageView 设置为可移动。为了模糊图像,您可以使用一些 hacky 技术或使用非常有效地模糊图像的 Renderscript。马里奥·维维亚尼(Mario Viviani )在这里描述了它。

模糊图像的代码:

public Bitmap blurBitmap(Bitmap bitmap) {

//Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(getApplicationContext());

//Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

//Create the in/out Allocations with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

//Set the radius of the blur
blurScript.setRadius(25.f);

//Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);

//Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);

//recycle the original bitmap
bitmap.recycle();

//After finishing everything, we destroy the Renderscript.
rs.destroy();

return outBitmap;
}

输出图像

2.使用库

您可以使用Dali 库,它在内部完成所有繁重的 RenderScript 并轻松实现模糊。

在此处输入图像描述

于 2018-03-10T16:04:31.517 回答
0

考虑将图像的两个副本放入 aStackPane中,对其中一个应用模糊处理。然后只需将剪辑应用于每个剪辑,以便您看到一张图像的一半和另一张图像的一半:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class PartiallyBlurredImage extends Application {

    private final Image image = new Image("https://www.nasa.gov/sites/default/files/thumbnails/image/pia22226.jpg");
    private final double width = 600 ;
    private final double height = 800 ;


    @Override
    public void start(Stage primaryStage) {
        ImageView plainImage = createImageView();

        ImageView blurredImage = createImageView();
        GaussianBlur blur = new GaussianBlur(20);       
        blurredImage.setEffect(blur);


        plainImage.setClip(new Rectangle(0, 0, width/2, height));
        blurredImage.setClip(new Rectangle(width/2, 0, width/2, height));

        StackPane partiallyBlurredImage = new StackPane(plainImage, blurredImage);
        Scene scene = new Scene(new ScrollPane(partiallyBlurredImage));
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private ImageView createImageView() {
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(width);
        imageView.setFitHeight(height);
        imageView.setPreserveRatio(true);
        return imageView ;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

在此处输入图像描述

于 2018-03-10T18:39:52.297 回答