登录 立即注册
安币:

查看: 252|回复: 6

Android实现毛玻璃效果的对话框,android 自定义对话框

[复制链接]

308

主题

753

帖子

706

安币

手工艺人

发表于 2018-5-16 14:55:24 | 显示全部楼层 |阅读模式

            

        一个popwindow,在弹出的时候背景是原界面的截图加高斯模糊效果:

        

        先给出popwindow的布局文件

[Java] 查看源文件 复制代码
<

        里面那个自定义imageview控件在我上一篇博客里,下面是activity的布局

[Java] 查看源文件 复制代码
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:id="@+id/window" 
 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" 
 tools:context="com.npi.blureffect.testactivity" > 
 
 <textview 
 android:id="@+id/textview1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="@string/hello_world" /> 
 
 <ratingbar 
 android:id="@+id/ratingbar1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignleft="@+id/textview1" 
 android:layout_below="@+id/textview1" 
 android:layout_margintop="124dp" /> 
 
 <switch 
 android:id="@+id/switch1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignleft="@+id/ratingbar1" 
 android:layout_below="@+id/ratingbar1" 
 android:layout_marginleft="24dp" 
 android:layout_margintop="81dp" 
 android:text="switch" /> 
 
 <button 
 android:id="@+id/button1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignright="@+id/ratingbar1" 
 android:layout_below="@+id/ratingbar1" 
 android:text="button" /> 
 
 <button 
 android:id="@+id/button2" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignbottom="@+id/ratingbar1" 
 android:layout_alignleft="@+id/switch1" 
 android:layout_marginbottom="52dp" 
 android:text="button" /> 
 
 <textview 
 android:id="@+id/textview2" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignbottom="@+id/button1" 
 android:layout_alignleft="@+id/ratingbar1" 
 android:text="large text" 
 android:textappearance="

        用于圆角的背景xml,放在drawable文件夹中

[Java] 查看源文件 复制代码
<

        activity的源码

[Java] 查看源文件 复制代码
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; 
 
 /** 
 * 创建popupwindow 
 */ 
 protected void initpopuptwindow(view layout) { 
 // todo auto-generated method stub 
 //对当前页面进行截屏 
 layout.setdrawingcacheenabled(true); 
 layout.builddrawingcache(); //启用drawingcache并创建位图 
 bitmap screen = bitmap.createbitmap(layout.getdrawingcache()); //创建一个drawingcache的拷贝,因为drawingcache得到的位图在禁用后会被回收 
 layout.setdrawingcacheenabled(false); //禁用drawingcahce否则会影响性能 
  
 //将截屏进行模糊 
 screen = blur.fastblur(this, screen, 15); 
  
 // 获取自定义布局文件activity_popupwindow_left.xml的视图 
 final view popupwindow_view = getlayoutinflater().inflate(r.layout.ioswindow, null, 
  false); 
 // 创建popupwindow实例,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); //设置popwindow的大小 
 popupwindow.showatlocation(textview1, gravity.center, 0, 0);//设置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); 
  } 
 }); 
  
 // 设置动画效果 
 // 点击其他地方消失 
 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(); 
 } 
} 

        第二种样式,比第一种简单但是效果更明显也更大众化

        

        这个是在原来布局的最上层加上了一个不可见的imageview,在弹出popwindow之前用这个imageview盖住底下的东西

        布局如下

[Java] 查看源文件 复制代码
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:id="@+id/window" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context="com.npi.blureffect.dialogactivity" > 
 
 <textview 
 android:id="@+id/textview1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="@string/hello_world" /> 
 <ratingbar 
 android:id="@+id/ratingbar1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignleft="@+id/textview1" 
 android:layout_below="@+id/textview1" 
 android:layout_margintop="124dp" /> 
 
 <switch 
 android:id="@+id/switch1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignleft="@+id/ratingbar1" 
 android:layout_below="@+id/ratingbar1" 
 android:layout_marginleft="24dp" 
 android:layout_margintop="81dp" 
 android:text="switch" /> 
 
 <button 
 android:id="@+id/button1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignright="@+id/ratingbar1" 
 android:layout_below="@+id/ratingbar1" 
 android:text="button" /> 
 
 <button 
 android:id="@+id/button2" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignbottom="@+id/ratingbar1" 
 android:layout_alignleft="@+id/switch1" 
 android:layout_marginbottom="52dp" 
 android:text="button" /> 
 
 <textview 
 android:id="@+id/textview2" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignbottom="@+id/button1" 
 android:layout_alignleft="@+id/ratingbar1" 
 android:text="large text" 
 android:textappearance="

        activity 如下

[Java] 查看源文件 复制代码
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; 
 
 
 /** 
 * 创建popupwindow 
 */ 
 protected void initpopuptwindow(view layout) { 
 // todo auto-generated method stub 
 //对当前页面进行截屏 
 layout.setdrawingcacheenabled(true); 
 layout.builddrawingcache(); //启用drawingcache并创建位图 
 bitmap screen = bitmap.createbitmap(layout.getdrawingcache()); //创建一个drawingcache的拷贝,因为drawingcache得到的位图在禁用后会被回收 
 layout.setdrawingcacheenabled(false); //禁用drawingcahce否则会影响性能 
 log.i("alex", "转换前bitmap的大小是"+screen.getwidth()+" : "+screen.getheight()); 
 screen = scalebitmap(screen, screen.getwidth()/2, screen.getheight()/2);//压缩bitmap到指定大小 
 log.i("alex", "转换后bitmap的大小是"+screen.getwidth()+" : "+screen.getheight()); 
 //将截屏进行模糊 
 screen = blur.fastblur(this, screen, 10); 
 
 // 获取自定义布局文件activity_popupwindow_left.xml的视图 
 final view popupwindow_view = getlayoutinflater().inflate(r.layout.ioswindow, null, 
 false); 
 
 // 创建popupwindow实例,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); //设置popwindow的大小 
 popupwindow.showatlocation(textview1, gravity.center, 0, 0);//设置popwindow的位置 
 popupwindow_view.post(new runnable() { 
 
 @override 
 public void run() { 
 // todo auto-generated method stub 
 int left = screenwidth/10; 
 } 
 }); 
 
 // 设置动画效果 
 // 点击其他地方消失 
 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(); 
 } 
 
 /** 
 * 把一个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();//回收图片内存 
 bm.setdensity(240); 
 return resizebitmap; 
 } 
 } 
} 






        

85

主题

9571

帖子

2202

安币

Android大神

Rank: 6Rank: 6

发表于 2018-5-16 18:01:53 | 显示全部楼层
感谢分享,楼主V5~

0

主题

9002

帖子

2082

安币

Android大神

Rank: 6Rank: 6

发表于 2018-5-16 22:34:08 | 显示全部楼层
感觉楼主很用心,辛苦啦~

434

主题

903

帖子

490

安币

手工艺人

发表于 2018-5-17 03:02:34 | 显示全部楼层
感谢分享,楼主V5~

317

主题

743

帖子

709

安币

手工艺人

发表于 2018-5-17 08:23:17 | 显示全部楼层
不错不错,楼主辛苦了。。。

475

主题

928

帖子

5809

安币

手工艺人

发表于 2018-5-17 15:33:40 | 显示全部楼层
感觉楼主很用心,辛苦啦~

440

主题

902

帖子

1916

安币

手工艺人

发表于 2018-5-17 20:40:10 | 显示全部楼层
感觉楼主很用心,辛苦啦~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

站长推荐

通过邮件订阅最新安卓weekly信息
上一条 /4 下一条

下载安卓巴士客户端

全国最大的安卓开发者社区
联系我们
关闭
合作电话:
15618560077
Email:
805941275@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 下载客户端|申请友链|手机版|站点统计|安卓巴士 ( 粤ICP备15117877号 )

快速回复 返回顶部 返回列表