當前位置:首頁 » 安卓系統 » android背景高斯模糊

android背景高斯模糊

發布時間: 2025-09-08 23:36:03

1. android 背景虛化原理是什麼

1. RenderScript

談到高斯模糊,第一個想到的就是RenderScript。RenderScript是由Android3.0引入,用來在Android上編寫高性能代碼的一種語言(使用C99標准)。 引用官方文檔的描述:
RenderScript runtime will parallelize work across all processors available on a device, such as multi-core CPUs, GPUs, or DSPs, allowing you to focus on expressing algorithms rather than scheling work or load balancing.
為了在Android中使用RenderScript,我們需要(直接貼官方文檔,比直譯更通俗易懂):
High-performance compute kernels are written in a C99-derived language.
A java API is used for managing the lifetime of RenderScript resources and controlling kernel execution.
學習文檔:http://developer.android.com/guide/topics/renderscript/compute.html
上面兩點總結成一句話為:我們需要一組compute kernels(.rs文件中編寫),及一組用於控制renderScript相關的java api(.rs文件自動生成為java類)。 由於compute kernels的編寫需要一定的學習成本,從JELLY_BEAN_MR1開始,Androied內置了一些compute kernels用於常用的操作,其中就包括了Gaussian blur。
下面,通過實操來講解一下RenderScript來實現高斯模糊,最終實現效果(講文字背景進行模糊處理):

布局:

[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/splash"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/text"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gaussian Blur"
android:textColor="@android:color/black"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:textSize="48sp" />

<LinearLayout
android:id="@+id/controls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#7f000000"
android:orientation="vertical"
android:layout_gravity="bottom" />
</FrameLayout>
核心代碼:

[java] view plain
private void applyBlur() {
image.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

@Override
public boolean onPreDraw() {
image.getViewTreeObserver().removeOnPreDrawListener(this);
image.buildDrawingCache();
Bitmap bmp = image.getDrawingCache();
blur(bmp, text, true);
return true;
}
});
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void blur(Bitmap bkg, View view) {
long startMs = System.currentTimeMillis();
float radius = 20;

Bitmap overlay = Bitmap.createBitmap((int)(view.getMeasuredWidth()), (int)(view.getMeasuredHeight()), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.translate(-view.getLeft(), -view.getTop());
canvas.drawBitmap(bkg, 0, 0, null);

RenderScript rs = RenderScript.create(SecondActivity.this);

Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
blur.setInput(overlayAlloc);
blur.setRadius(radius);
blur.forEach(overlayAlloc);
overlayAlloc.To(overlay);
view.setBackground(new BitmapDrawable(getResources(), overlay));
rs.destroy();

statusText.setText("cost " + (System.currentTimeMillis() - startMs) + "ms");
}
當ImageView開始載入背景圖時,取出它的drawableCache,進行blur處理,Gaussian blur的主要邏輯在blur函數中。對於在Java中使用RenderScript,文檔中也有詳細描述,對應到我們的代碼,步驟為:
初始化一個RenderScript Context.
至少創建一個Allocation對象用於存儲需要處理的數據.
創建compute kernel的實例,本例中是內置的ScriptIntrinsicBlur對象.
設置ScriptIntrinsicBlur實例的相關屬性,包括Allocation, radius等.
開始blur操作,對應(forEach).
將blur後的結果拷貝回bitmap中。

熱點內容
java自我評價 發布:2025-09-09 03:38:51 瀏覽:469
房產系統源碼 發布:2025-09-09 03:30:19 瀏覽:753
筆記本從哪裡刪除密碼 發布:2025-09-09 03:14:05 瀏覽:799
並行存儲器 發布:2025-09-09 03:11:53 瀏覽:91
安裝sippython 發布:2025-09-09 03:07:00 瀏覽:957
如何多台機器配置同一個域名 發布:2025-09-09 03:06:06 瀏覽:609
redhat卸載python 發布:2025-09-09 02:45:35 瀏覽:187
thinkphp的資料庫配置 發布:2025-09-09 02:44:05 瀏覽:409
如何選好電腦零件配置 發布:2025-09-09 02:42:29 瀏覽:947
安卓手機為什麼沒有小屏旗艦 發布:2025-09-09 02:38:43 瀏覽:952