在Android应用程序中设置模糊效果的背景图像 [英] Set the background image in blur effect in android app

查看:197
本文介绍了在Android应用程序中设置模糊效果的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让列表视图中的背景图像模糊不清,但我尝试按照教程进行操作并不起作用。有人请指教,谢谢。

Im trying to get my background image on the list view blurred, but I tried following the tutorials and it does not work. Anyone please advise, thanks.

main activity.java

main activity.java

public class IngredientCategoryMain extends Activity {

ListView list;
String[] title;
CategoryImageAdapter adapter;





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ingredient_category_main);

    list=(ListView)findViewById(R.id.listView);
    title = getResources().getStringArray(R.array.titles);
    adapter=new CategoryImageAdapter(this, mStrings, title);
    list.setAdapter(adapter);

}

@Override
public void onDestroy() {
    list.setAdapter(null);
    super.onDestroy();
}

public View.OnClickListener listener=new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        adapter.imageLoader.clearCache();
        adapter.notifyDataSetChanged();


    }
};

public void onItemClick(int mPosition) {
    String tempValues = title[mPosition];
    Toast.makeText(IngredientCategoryMain.this, "Click on image "+tempValues+" to enter", Toast.LENGTH_LONG).show();

}

private String[] mStrings={
        "https://pixabay.com/static/uploads/photo/2014/07/08/14/33/breads-387544_960_720.jpg",
        "http://mtbev.org/wp-content/uploads/2014/02/Bottles.jpg",
        "https://c2.staticflickr.com/8/7023/6548962149_7a9fdd9cf4_b.jpg",
        "https://upload.wikimedia.org/wikipedia/commons/2/2f/Culinary_fruits_front_view.jpg",
        "https://upload.wikimedia.org/wikipedia/commons/e/ea/Indian_Spices.jpg",
        "https://pixabay.com/static/uploads/photo/2016/01/13/17/21/raw-1138562_960_720.jpg",
        "https://pixabay.com/static/uploads/photo/2010/12/13/09/51/seed-1716_960_720.jpg",
        "https://upload.wikimedia.org/wikipedia/commons/b/ba/Rice_grains_(IRRI).jpg",
        "http://www.stock-free.org/images/Thanksgiving-Stock-Free-Image-08112015-image-239.jpg",
        "https://pixabay.com/static/uploads/photo/2012/10/01/18/34/dips-58738_960_720.jpg",
        "https://pixabay.com/static/uploads/photo/2013/07/19/00/18/seafood-165220_960_720.jpg",
        "https://c1.staticflickr.com/3/2877/10866943666_471d9f2845_b.jpg"
};


}

我设计listview的xml文件

xml file where I designed the listview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">


<ImageButton
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:id="@+id/imageButton2"
    android:scaleType="fitXY"
    android:layout_below="@+id/textView2"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/textView2"
    android:layout_marginLeft="20dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textColor="#f7338b"
    android:textStyle="bold"
    android:background="#80ffffff" />

和listview.xml

and listview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.cassieleong.delishcart.IngredientCategoryMain"
tools:showIn="@layout/activity_ingredient_category_main"
android:background="#fde7e7">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:background="@drawable/appicon"/>

推荐答案

我最近遇到过Renderscript API。

I recently came across Renderscript API.

//Set the radius of the Blur. Supported range 0 < radius <= 25
private static final float BLUR_RADIUS = 25f;

public Bitmap blur(Bitmap image) {
if (null == image) return null;

Bitmap outputBitmap = Bitmap.createBitmap(image);
final RenderScript renderScript = RenderScript.create(this);
Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);

//Intrinsic Gausian blur filter
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
} 

在图片视图中使用上面的代码片段,如下所示。

Use the above code snippet in the image view as shown below.

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.nature);
Bitmap blurredBitmap = blur(bitmap);
imageView.setImageBitmap(blurredBitmap);

别忘了在build.gradle文件中添加以下行

Dont forget to add below lines in build.gradle file

renderscriptTargetApi 18
renderscriptSupportModeEnabled true

参考:
http: //javatechig.com/android/how-to-create-bitmap-blur-effect-in-android-using-renderscript

这篇关于在Android应用程序中设置模糊效果的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆