Android库模糊效果对我不起作用,模糊效果未应用。

3

我正在使用Blurry库进行项目开发,但似乎无法正常工作。

我想在相对布局中拥有模糊背景,是否有人可以帮我提供一些思路呢?

以下是我片段中的代码:

 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    imageView=(ImageView)view.findViewById(R.id.imageView2);
    imageButton=(ImageButton)view.findViewById(R.id.imageButton);
    imageButton2=(ImageButton)view.findViewById(R.id.imageButton2);
    imageButton3=(ImageButton)view.findViewById(R.id.imageButton3);
    textView=(TextView)view.findViewById(R.id.textView);
    seekBar=(SeekBar)view.findViewById(R.id.seekBar);
    rl=(RelativeLayout)view.findViewById(R.id.psrl);
    rl1=(RelativeLayout)view.findViewById(R.id.rl1);

    imageButton.setOnClickListener(this);
    imageButton2.setOnClickListener(this);
    imageButton3.setOnClickListener(this);
    seekBar.setOnSeekBarChangeListener(this);
    createMediaPlayer();
    setAlbumImage();
    runnable=new Runnable() {
        @Override
        public void run() {
            seekBar.setProgress(MainActivity.mediaPlayer.getCurrentPosition());
            handler.postDelayed(runnable,1000);
        }
    };
    handler.postDelayed(runnable,1000);
    rl.setBackground(d);
    Blurry.with(getContext()).radius(25).sampling(2).async().animate(500).onto((ViewGroup)view.findViewById(R.id.psrl));
}

以下是XML代码:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/psrl"
>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#7e838c"
    android:alpha="1.0"
    android:id="@+id/rl1">
<TextView
    android:layout_width="210dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="8dp"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"
    android:marqueeRepeatLimit="marquee_forever"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="Medium Text"
    android:textSize="15dp"
    android:id="@+id/textView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>
<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/imageView2"
    android:layout_marginTop="109dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:scaleType="fitXY"
    android:background="@drawable/ic_launcher"
    android:adjustViewBounds="true"
    android:padding="8dp"
    android:alpha="1"
    android:cropToPadding="true"/>

<SeekBar
    android:layout_width="370dp"
    android:layout_height="30dp"
    android:id="@+id/seekBar"
    android:layout_above="@+id/imageButton"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="38dp" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton"
    android:background="@drawable/custom2"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="60dp"
    android:layout_centerHorizontal="true"/>
<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton2"
    android:background="@drawable/previous"
    android:layout_alignTop="@+id/imageButton"
    android:layout_alignLeft="@+id/imageView2"
    android:layout_alignStart="@+id/imageView2" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton3"
    android:background="@drawable/skip"
    android:layout_alignTop="@+id/imageButton"
    android:layout_alignRight="@+id/imageView2"
    android:layout_alignEnd="@+id/imageView2" />

</RelativeLayout>

只设置了背景,但模糊效果并未应用。有人能帮忙吗?

这是build.gradle文件:

 android {
compileSdkVersion 24
defaultConfig {
    applicationId "com.example.abhinav.mymusicplayer"
    minSdkVersion 16
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
buildToolsVersion '24.0.2'
productFlavors {
}
}
repositories {
maven {
    url "https://jitpack.io"

}
}
dependencies {
compile 'com.android.support:support-v4:24.2.0'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'jp.wasabeef:blurry:2.1.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.github.moondroid.coverflow:library:1.0'
compile 'com.github.davidschreiber:FancyCoverFlow:a54a1e7'
compile files('libs/picasso-2.5.2.jar')
compile files('libs/carousel-api-v1.jar')
}

请发布您的 build.gradle(Module:app) 文件。您是否启用了 RenderScript? - Kostas Drak
@KostasDrak发布了build.gradle。 - Abhinav422
但是您尚未启用RenderScript,因此模糊效果无法正常工作。 - Kostas Drak
怎样做? - Abhinav422
2个回答

2

好的,以下是我在想要模糊图像时所遵循的步骤。

将您的build.gradle修改为如下内容:

android {

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 25

        renderscriptTargetApi 25 <-- must match your targetSdkVersion
        renderscriptSupportModeEnabled true

    }
}

重复使用该库程序。如果这不能解决问题,请尝试使用此库:https://github.com/jrvansuita/GaussianBlur,对我很有帮助!希望对您有所帮助!

1
我想要使相对布局的背景模糊,但却模糊了整个屏幕。如何仅模糊背景? - Abhinav422
高斯模糊在 build.gradle 中出现错误:“无法解析 com.android.support:appcompat-v7:25.0.0”。 - Abhinav422
1
无论您的Target SDK版本是什么,构建工具和RenderScript也必须相应更新。请尝试通过SDK Manager进行更新。 - Kostas Drak

0

这个库使用getMeasuredWidth and getMeasuredHeight

当你调用它时,在onViewCreated中,getMeasuredWidth/getMeasuredHeight的值将为0,所以你应该将它移到View.post()中,然后你可以得到getMeasuredHeight()/getMeasuredWidth()的实际值,然后你就可以使用Blury库。

例如:

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    root.post(new Runnable() {
        @Override
        public void run() {
            // for instance
            Blurry.with(getContext()).radius(25).sampling(2).async().animate(500).onto((ViewGroup)view.findViewById(R.id.psrl));
        }
    });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接