// MainActivity.java
package com.example.blurdemo;import android.annotation.SuppressLint;import android.app.Activity;import android.content.DialogInterface;import android.content.DialogInterface.OnDismissListener;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.webkit.WebSettings.PluginState;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Button;public class MainActivity extends Activity {private Button button;private WebView webView;@SuppressWarnings("deprecation")@SuppressLint({ "SetJavaScriptEnabled", "InflateParams" })@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);final View contentView = LayoutInflater.from(this).inflate(R.layout.activity_main, null);setContentView(contentView);button = (Button) findViewById(R.id.button);webView = (WebView) findViewById(R.id.webView);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {button.setVisibility(View.GONE);SearchDialog dialog = new SearchDialog(MainActivity.this, contentView);dialog.show();dialog.setOnDismissListener(new OnDismissListener() {@Overridepublic void onDismiss(DialogInterface dialog) {button.setVisibility(View.VISIBLE);}});}});webView.getSettings().setJavaScriptEnabled(true);webView.getSettings().setPluginState(PluginState.ON);webView.getSettings().setPluginsEnabled(true);webView.getSettings().setAllowFileAccess(true);webView.getSettings().setLoadWithOverviewMode(true);webView.getSettings().setUseWideViewPort(true);webView.loadUrl("http://3g.qq.com");webView.setWebViewClient(new WebViewClient() {@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return true;}});}static {System.loadLibrary("bitmap");}}
// SearchDialog.java
package com.example.blurdemo;import com.component.bitmap.jni.BitmapTools;import com.component.bitmap.jni.ImageBlur;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Dialog;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Matrix;import android.graphics.Rect;import android.os.Handler;import android.os.Message;import android.view.Display;import android.view.View;import android.widget.ImageView;import android.widget.Toast;public class SearchDialog extends Dialog {private ImageView bgImageView;private long startTime;public SearchDialog(final Context context) {super(context, R.style.searchDialog);setContentView(R.layout.dialog_search);bgImageView = (ImageView) findViewById(R.id.bgImageView);bgImageView.post(new Runnable() {@Overridepublic void run() {startTime = System.currentTimeMillis();Bitmap bitmap = myShot((MainActivity) context);duBlue(bitmap);}});}public SearchDialog(Context context, final View view) {super(context, R.style.searchDialog);setContentView(R.layout.dialog_search);bgImageView = (ImageView) findViewById(R.id.bgImageView);bgImageView.post(new Runnable() {@Overridepublic void run() {startTime = System.currentTimeMillis();Bitmap bitmap = BitmapTools.getBitmapFromView(view);duBlue(bitmap);}});}private void duBlue(final Bitmap srcBitmap) {new Thread() {public void run() {Bitmap bitmap = srcBitmap;int width = bitmap.getWidth();int height = bitmap.getHeight();bitmap = zoomImage(bitmap, 50, 50 * height / width);bitmap = ImageBlur.doBlurJniBitMap(bitmap, 5, true);bitmap = zoomImage(bitmap, width, height);Message message = new Message();long time = System.currentTimeMillis() - startTime;message.obj = bitmap;message.arg1 = (int) time;handler.sendMessage(message);};}.start();}@SuppressLint("HandlerLeak")private Handler handler = new Handler() {public void handleMessage(Message msg) {Bitmap bitmap = (Bitmap) msg.obj;bgImageView.setImageBitmap(bitmap);Toast.makeText(getContext(), "耗时 = " + msg.arg1 + "毫秒", Toast.LENGTH_SHORT).show();};};/**** 图片的缩放方法** @param bgimage* :源图片资源* @param newWidth* :缩放后宽度* @param newHeight* :缩放后高度* @return*/public static Bitmap zoomImage(Bitmap bgimage, double newWidth, double newHeight) {// 获取这个图片的宽和高float width = bgimage.getWidth();float height = bgimage.getHeight();// 创建操作图片用的matrix对象Matrix matrix = new Matrix();// 计算宽高缩放率float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;// 缩放图片动作matrix.postScale(scaleWidth, scaleHeight);Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width, (int) height, matrix, true);return bitmap;}@SuppressWarnings("deprecation")public Bitmap myShot(Activity activity) {// 获取windows中最顶层的viewView view = activity.getWindow().getDecorView();view.buildDrawingCache();// 获取状态栏高度Rect rect = new Rect();view.getWindowVisibleDisplayFrame(rect);int statusBarHeights = rect.top;Display display = activity.getWindowManager().getDefaultDisplay();// 获取屏幕宽和高int widths = display.getWidth();int heights = display.getHeight();// 允许当前窗口保存缓存信息view.setDrawingCacheEnabled(true);// 去掉状态栏Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeights, widths, heights- statusBarHeights);// 销毁缓存信息view.destroyDrawingCache();return bmp;}}
// activity_main.xml
<RelativeLayout 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="${relativePackage}.${activityClass}" ><WebViewandroid:id="@+id/webView"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerHorizontal="true"android:layout_centerVertical="true" /><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="????" /></RelativeLayout>
// dialog_search.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><ImageViewandroid:id="@+id/bgImageView"android:layout_width="match_parent"android:layout_height="match_parent" /><ImageViewandroid:background="#55555555"android:layout_width="match_parent"android:layout_height="match_parent" /><EditTextandroid:id="@+id/editText1"android:layout_width="match_parent"android:layout_height="wrap_content"android:ems="10" ><requestFocus /></EditText></RelativeLayout>
// styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="searchDialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
</resources>
版权声明:本文为博主原创文章,未经博主允许不得转载。