Nicolas Schmidt Asked: 2020-06-14 20:38:27 +0800 CST 2020-06-14 20:38:27 +0800 CST 2020-06-14 20:38:27 +0800 CST 如何在Android上模糊背景图像? 772 我想知道如何在android-studio中实现模糊效果。我不使用 Photoshop 或类似软件这样做,因为它是用户的个人资料,而个人资料照片是在背景中模糊的照片。 因此,它必须是动态的。 这是示例图像。 java 2 Answers Voted Best Answer Max Sandoval 2020-06-14T21:11:24+08:002020-06-14T21:11:24+08:00 首先你把它放在背景中,就像你把它作为主要的一样,为了做你可以使用模糊库的效果,你只需通过 Graddle 将它添加到你的项目中。 模糊: 只需将其添加到您的 Gradle 应用程序文件中,您就可以使用该库。 dependencies { compile 'jp.wasabeef:blurry:2.0.2' } 将此示例活动添加到您的应用程序中,您可以在其中看到库的工作方式。 活动示例.xml 向视图添加一个可绘制对象,以便您可以看到效果。 <RelativeLayout android:id="@+id/content" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activities.ExampleActivity"> <View android:id="@+id/spacer" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerInParent="true"/> <ImageView android:id="@+id/right_top" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/spacer" android:layout_alignLeft="@id/spacer" android:contentDescription="@null" android:scaleType="centerCrop" android:src="@drawable/serv"/> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/spacer" android:layout_alignRight="@id/spacer" android:contentDescription="@null" android:scaleType="centerCrop" android:src="@drawable/serv"/> <ImageView android:id="@+id/right_bottom" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignLeft="@id/spacer" android:layout_below="@id/spacer" android:contentDescription="@null" android:scaleType="centerCrop" android:src="@drawable/serv"/> <ImageView android:id="@+id/left_bottom" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignRight="@id/spacer" android:layout_below="@id/spacer" android:contentDescription="@null" android:scaleType="centerCrop" android:src="@drawable/serv"/> <ImageView android:id="@+id/overlay" android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="@null"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:layout_centerInParent="true" android:text="@string/app_name" android:elevation="10dp" android:textColor="@android:color/black" android:textSize="88sp" android:textStyle="bold"/> </RelativeLayout> ExampleActivity.java public class ExampleActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { long startMs = System.currentTimeMillis(); Blurry.with(getApplicationContext()) .radius(25) .sampling(1) .color(Color.argb(66, 0, 255, 255)) .async() .capture(findViewById(R.id.right_top)) .into((ImageView) findViewById(R.id.right_top)); Blurry.with(getApplicationContext()) .radius(10) .sampling(8) .async() .capture(findViewById(R.id.right_bottom)) .into((ImageView) findViewById(R.id.right_bottom)); Blurry.with(getApplicationContext()) .radius(25) .sampling(1) .color(Color.argb(66, 255, 255, 0)) .async() .capture(findViewById(R.id.left_bottom)) .into((ImageView) findViewById(R.id.left_bottom)); Log.d(getString(R.string.app_name), "TIME " + String.valueOf(System.currentTimeMillis() - startMs) + "ms"); } }); findViewById(R.id.button).setOnLongClickListener(new View.OnLongClickListener() { private boolean blurred = false; @Override public boolean onLongClick(View v) { if (blurred) { Blurry.delete((ViewGroup) findViewById(R.id.content)); } else { long startMs = System.currentTimeMillis(); Blurry.with(getApplicationContext()) .radius(25) .sampling(2) .async() .animate(500) .onto((ViewGroup) findViewById(R.id.content)); Log.d(getString(R.string.app_name), "TIME " + String.valueOf(System.currentTimeMillis() - startMs) + "ms"); } blurred = !blurred; return true; } }); } } 在 onCreate 开始: 由于它是运行时的视觉变化,因此有必要在线程内处理转换。 首先,您的图像必须包含在 View(Layout) 中,并且更改将应用于它。 活动示例.xml: <RelativeLayout android:id="@+id/content" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activities.ExampleActivity"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="@null" android:scaleType="centerCrop" android:src="@drawable/serv"/> </RelativeLayout> ExampleActivity.java: public class ExampleActivity extends AppCompatActivity { private View rootView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_blurry); rootView = (View) findViewById(R.id.content); rootView.post(new Runnable() { @Override public void run() { Blurry.with(getApplicationContext()).radius(25).sampling(2).onto((ViewGroup) rootView); } }); } } Fabio Venturi Pastor 2020-06-15T07:51:36+08:002020-06-15T07:51:36+08:00 我把它放在一个答案中,因为它有点大。 在@Max Sandoval 的答案中添加其他内容,我认为除了库之外,您还应该将RenderScript添加到 build.gradle: android { compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig { minSdkVersion 8 targetSdkVersion 19 renderscriptTargetApi 18 renderscriptSupportModeEnabled true } } 这就是我必须做的才能让它发挥作用。一切顺利。
首先你把它放在背景中,就像你把它作为主要的一样,为了做你可以使用模糊库的效果,你只需通过 Graddle 将它添加到你的项目中。
模糊:
只需将其添加到您的 Gradle 应用程序文件中,您就可以使用该库。
将此示例活动添加到您的应用程序中,您可以在其中看到库的工作方式。
活动示例.xml
向视图添加一个可绘制对象,以便您可以看到效果。
ExampleActivity.java
在 onCreate 开始:
由于它是运行时的视觉变化,因此有必要在线程内处理转换。
首先,您的图像必须包含在 View(Layout) 中,并且更改将应用于它。
活动示例.xml:
ExampleActivity.java:
我把它放在一个答案中,因为它有点大。
在@Max Sandoval 的答案中添加其他内容,我认为除了库之外,您还应该将RenderScript添加到 build.gradle:
这就是我必须做的才能让它发挥作用。一切顺利。