当前位置: 代码迷 >> Android >> Android高速高斯模糊对话框
  详细解决方案

Android高速高斯模糊对话框

热度:43   发布时间:2016-04-27 23:34:54.0
Android快速高斯模糊对话框

// 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" })
@Override
protected 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() {
@Override
public void onClick(View v) {
button.setVisibility(View.GONE);
SearchDialog dialog = new SearchDialog(MainActivity.this, contentView);
dialog.show();
dialog.setOnDismissListener(new OnDismissListener() {
@Override
public 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() {
@Override
public 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() {
@Override
public 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() {
@Override
public 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中最顶层的view
View 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}" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android: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" >
<ImageView
android:id="@+id/bgImageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:background="#55555555"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android: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>

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

  相关解决方案