在 Android 中使用 setBackgroundDrawable 函数设置背景
Setting Background with setBackgroungDrawable function in Android
好的,我在设置背景图片时遇到了问题。起初我只是从 'properities' 框中设置背景。我将 .jpg 文件复制到我的 drawable 文件夹中,只需单击 properities 框中的 background 属性,然后单击 select 中的文件资源文件夹。那很好,它起作用了,但后来我想使背景模糊,我找到了一个为此编写的 class,称为 BlurBuilder,我把它 class 到我的项目。到目前为止一切都很好,我添加了 class 但是当我尝试应用 class 中的函数时,如示例所示:
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
编辑器说 '无法解析符号 'view''。
我尝试将第二行更改为;
View.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
但是这次android工作室说'非静态方法'setBackgroudDrawable(android.graphic.drawables.Drawable)不能从静态上下文中引用
这是我使用的 BlurBuilder class:
public class BlurBuilder {
private static final float BITMAP_SCALE = 0.4f;
private static final float BLUR_RADIUS = 7.5f;
public static Bitmap blur(Context context, 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(context);
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;
}}
提前致谢。
EDİT:我想让我的 activity 的背景变得模糊。
您从非静态上下文中调用了一个非静态函数,即 setDrawableBackground,这意味着您需要创建某个视图的实例,例如:
ImageView view = (ImageView) findViewById(R.id.imageview1);
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
完成后,您现在可以调用 view
支持的任何函数。
知道了,
在 XML(main_activity.xml 或您的文件中添加以下内容):
<ImageView
android:id="@+id/whole_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android" />
在 Main Activity] 中添加以下内容:这将强制缩放图像以显示屏幕大小并使用整个显示器来显示图像。
/* where you set the activity */
setContentView(R.layout.your_main_activity);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/* adapt the image to the size of the display */
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
Bitmap bmp = Bitmap.createScaledBitmap(blurredBitmap,size.x,size.y,true);
/* fill the background ImageView with the resized image */
ImageView iv_background = (ImageView) findViewById(R.id.whole_background);
iv_background.setImageBitmap(bmp);
好的,我在设置背景图片时遇到了问题。起初我只是从 'properities' 框中设置背景。我将 .jpg 文件复制到我的 drawable 文件夹中,只需单击 properities 框中的 background 属性,然后单击 select 中的文件资源文件夹。那很好,它起作用了,但后来我想使背景模糊,我找到了一个为此编写的 class,称为 BlurBuilder,我把它 class 到我的项目。到目前为止一切都很好,我添加了 class 但是当我尝试应用 class 中的函数时,如示例所示:
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
编辑器说 '无法解析符号 'view''。 我尝试将第二行更改为;
View.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
但是这次android工作室说'非静态方法'setBackgroudDrawable(android.graphic.drawables.Drawable)不能从静态上下文中引用
这是我使用的 BlurBuilder class:
public class BlurBuilder {
private static final float BITMAP_SCALE = 0.4f;
private static final float BLUR_RADIUS = 7.5f;
public static Bitmap blur(Context context, 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(context);
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;
}}
提前致谢。
EDİT:我想让我的 activity 的背景变得模糊。
您从非静态上下文中调用了一个非静态函数,即 setDrawableBackground,这意味着您需要创建某个视图的实例,例如:
ImageView view = (ImageView) findViewById(R.id.imageview1);
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
完成后,您现在可以调用 view
支持的任何函数。
知道了,
在 XML(main_activity.xml 或您的文件中添加以下内容):
<ImageView
android:id="@+id/whole_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android" />
在 Main Activity] 中添加以下内容:这将强制缩放图像以显示屏幕大小并使用整个显示器来显示图像。
/* where you set the activity */
setContentView(R.layout.your_main_activity);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/* adapt the image to the size of the display */
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
Bitmap bmp = Bitmap.createScaledBitmap(blurredBitmap,size.x,size.y,true);
/* fill the background ImageView with the resized image */
ImageView iv_background = (ImageView) findViewById(R.id.whole_background);
iv_background.setImageBitmap(bmp);