一個(gè)popwindow,在彈出的時(shí)候背景是原界面的截圖加高斯模糊效果:
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供巴彥淖爾網(wǎng)站建設(shè)、巴彥淖爾做網(wǎng)站、巴彥淖爾網(wǎng)站設(shè)計(jì)、巴彥淖爾網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、巴彥淖爾企業(yè)網(wǎng)站模板建站服務(wù),10年巴彥淖爾做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
先給出popwindow的布局文件
<?xml version="1.0" encoding="utf-8"?>
里面那個(gè)自定義imageView控件在我上一篇博客里,下面是activity的布局
用于圓角的背景xml,放在drawable文件夾中
<?xml version="1.0" encoding="UTF-8" ?>
activity的源碼
package com.npi.blureffect; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Point; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; public class TestActivity extends Activity { TextView textView1; RelativeLayout window; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); textView1 = (TextView) findViewById(R.id.textView1); window = (RelativeLayout)findViewById(R.id.window); textView1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub initPopuptWindow(window); } }); } PopupWindow popupWindow; /** * 創(chuàng)建PopupWindow */ protected void initPopuptWindow(View layout) { // TODO Auto-generated method stub //對(duì)當(dāng)前頁(yè)面進(jìn)行截屏 layout.setDrawingCacheEnabled(true); layout.buildDrawingCache(); //啟用DrawingCache并創(chuàng)建位圖 Bitmap screen = Bitmap.createBitmap(layout.getDrawingCache()); //創(chuàng)建一個(gè)DrawingCache的拷貝,因?yàn)镈rawingCache得到的位圖在禁用后會(huì)被回收 layout.setDrawingCacheEnabled(false); //禁用DrawingCahce否則會(huì)影響性能 //將截屏進(jìn)行模糊 screen = Blur.fastblur(this, screen, 15); // 獲取自定義布局文件activity_popupwindow_left.xml的視圖 final View popupWindow_view = getLayoutInflater().inflate(R.layout.ioswindow, null, false); // 創(chuàng)建PopupWindow實(shí)例,200,LayoutParams.MATCH_PARENT分別是寬度和高度 final ScrollableImageView background = (ScrollableImageView) popupWindow_view.findViewById(R.id.imageView1); background.setoriginalImage(screen); final int screenWidth = getScreenWidth(this); final int screenHeight = screen.getHeight(); final int heightless = getScreenHeight(this)-screenHeight; Log.i("Alex", "屏幕寬度為"+screenWidth+"高度為"+screenHeight+"偏差為"+heightless); popupWindow = new PopupWindow(popupWindow_view, (int) (screenWidth*0.8), (int) (screenWidth*0.85*0.5), true); //設(shè)置popwindow的大小 popupWindow.showAtLocation(textView1, Gravity.CENTER, 0, 0);//設(shè)置popwindow的位置 popupWindow_view.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub int left = screenWidth/10; Log.i("Alex", screenHeight+"-"+screenWidth*0.85*0.5); int top = (int) ((screenHeight-screenWidth*0.85*0.5)/2-heightless/2); Log.i("Alex", "top是"+top); background.handleScroll(top, left); } }); // 設(shè)置動(dòng)畫(huà)效果 // 點(diǎn)擊其他地方消失 TextView confirm = (TextView) popupWindow_view.findViewById(R.id.textView2); confirm.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub popupWindow.dismiss(); } }); } /** * Get the screen width. * * @param context * @return the screen width */ @SuppressWarnings("deprecation") @SuppressLint("NewApi") public static int getScreenWidth(Activity context) { Display display = context.getWindowManager().getDefaultDisplay(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { Point size = new Point(); display.getSize(size); return size.x; } return display.getWidth(); } /** * Get the screen height. * * @param context * @return the screen height */ @SuppressWarnings("deprecation") @SuppressLint("NewApi") public static int getScreenHeight(Activity context) { Display display = context.getWindowManager().getDefaultDisplay(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { Point size = new Point(); display.getSize(size); return size.y; } return display.getHeight(); } }
第二種樣式,比第一種簡(jiǎn)單但是效果更明顯也更大眾化
這個(gè)是在原來(lái)布局的最上層加上了一個(gè)不可見(jiàn)的imageView,在彈出popwindow之前用這個(gè)imageView蓋住底下的東西
布局如下
activity 如下
package com.npi.blureffect; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.Point; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; public class DialogActivity extends Activity { TextView textView1; RelativeLayout window; ImageView background; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dialog); textView1 = (TextView) findViewById(R.id.textView1); window = (RelativeLayout)findViewById(R.id.window); background = (ImageView) findViewById(R.id.background); textView1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub initPopuptWindow(window); } }); } PopupWindow popupWindow; /** * 創(chuàng)建PopupWindow */ protected void initPopuptWindow(View layout) { // TODO Auto-generated method stub //對(duì)當(dāng)前頁(yè)面進(jìn)行截屏 layout.setDrawingCacheEnabled(true); layout.buildDrawingCache(); //啟用DrawingCache并創(chuàng)建位圖 Bitmap screen = Bitmap.createBitmap(layout.getDrawingCache()); //創(chuàng)建一個(gè)DrawingCache的拷貝,因?yàn)镈rawingCache得到的位圖在禁用后會(huì)被回收 layout.setDrawingCacheEnabled(false); //禁用DrawingCahce否則會(huì)影響性能 Log.i("Alex", "轉(zhuǎn)換前bitmap的大小是"+screen.getWidth()+" : "+screen.getHeight()); screen = scaleBitmap(screen, screen.getWidth()/2, screen.getHeight()/2);//壓縮bitmap到指定大小 Log.i("Alex", "轉(zhuǎn)換后bitmap的大小是"+screen.getWidth()+" : "+screen.getHeight()); //將截屏進(jìn)行模糊 screen = Blur.fastblur(this, screen, 10); // 獲取自定義布局文件activity_popupwindow_left.xml的視圖 final View popupWindow_view = getLayoutInflater().inflate(R.layout.ioswindow, null, false); // 創(chuàng)建PopupWindow實(shí)例,200,LayoutParams.MATCH_PARENT分別是寬度和高度 background.setImageBitmap(screen); background.setVisibility(View.VISIBLE); final int screenWidth = getScreenWidth(this); popupWindow = new PopupWindow(popupWindow_view, (int) (screenWidth*0.8), (int) (screenWidth*0.85*0.5), true); //設(shè)置popwindow的大小 popupWindow.showAtLocation(textView1, Gravity.CENTER, 0, 0);//設(shè)置popwindow的位置 popupWindow_view.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub int left = screenWidth/10; } }); // 設(shè)置動(dòng)畫(huà)效果 // 點(diǎn)擊其他地方消失 TextView confirm = (TextView) popupWindow_view.findViewById(R.id.textView2); confirm.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub background.setVisibility(View.GONE); popupWindow.dismiss(); } }); } /** * Get the screen width. * * @param context * @return the screen width */ @SuppressWarnings("deprecation") @SuppressLint("NewApi") public static int getScreenWidth(Activity context) { Display display = context.getWindowManager().getDefaultDisplay(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { Point size = new Point(); display.getSize(size); return size.x; } return display.getWidth(); } /** * 把一個(gè)bitmap壓縮,壓縮到指定大小 * @param bm * @param width * @param height * @return */ private static Bitmap scaleBitmap(Bitmap bm, float width, float height) { if (bm == null) { return null; } int bmWidth = bm.getWidth(); int bmHeight = bm.getHeight(); float scaleWidth = width / bmWidth; float scaleHeight = height / bmHeight; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); if (scaleWidth == 1 && scaleHeight == 1) { return bm; } else { Bitmap resizeBitmap = Bitmap.createBitmap(bm, 0, 0, bmWidth, bmHeight, matrix, false); bm.recycle();//回收?qǐng)D片內(nèi)存 bm.setDensity(240); return resizeBitmap; } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。