Android 털 유리 효과 구현 대화 상자

팝 윈도 우 는 팝 업 할 때 배경 이 원래 인터페이스의 캡 처 가우스 모호 효과 입 니 다.

먼저 popwindow 의 레이아웃 파일 을 보 여 줍 니 다.

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/FrameLayout1" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="@drawable/roundcorner" > 
 
 <com.npi.blureffect.ScrollableImageView 
 android:id="@+id/imageView1" 
 android:background="@drawable/roundcorner" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:src="@drawable/roundcorner" /> 
 
 <RelativeLayout 
 android:id="@+id/RelativeLayout1" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" > 
 
 <TextView 
  android:id="@+id/textView1" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_alignParentLeft="false" 
  android:layout_alignParentTop="false" 
  android:layout_centerHorizontal="true" 
  android:layout_centerInParent="false" 
  android:layout_centerVertical="false" 
  android:layout_marginLeft="33dp" 
  android:layout_marginTop="44dp" 
  android:text="     " /> 
 
 <TextView 
  android:id="@+id/textView2" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_alignParentLeft="false" 
  android:layout_below="@+id/textView1" 
  android:layout_centerHorizontal="true" 
  android:layout_marginTop="49dp" 
  android:text="  " 
  android:textAppearance="?android:attr/textAppearanceLarge" /> 
 
 </RelativeLayout> 
 
</FrameLayout> 
그 사용자 정의 imageView 컨트롤 은 지난 블 로그 에 있 습 니 다.다음은 activity 의 레이아웃 입 니 다.

<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="?android:attr/textAppearanceLarge" /> 
 
</RelativeLayout> 

원 각 배경 xml 를 drawable 폴 더 에 넣 습 니 다.

<?xml version="1.0" encoding="UTF-8" ?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item> 
  <shape> 
  <solid android:color="#efefef" /><!--       --> 
   
  <!--        --> 
   <corners 
   android:topLeftRadius="20dp"  
   android:topRightRadius="20dp"  
   android:bottomRightRadius="20dp"  
   android:bottomLeftRadius="20dp" 
   /> 
  </shape> 
 </item> 
 <!--         --> 
 <item android:bottom="20dp" android:top="20dp" android:left="20dp" android:right="20dp"> <!--           , 0      --> 
  <shape> 
  <solid android:color="#efefef" /> <!--      --> 
  </shape> 
 </item> 
 </layer>
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; 
 
 /** 
 *   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 를 추가 하여 팝 업 창 이 뜨 기 전에 이 imageView 로 아래 를 덮 는 것 입 니 다.
레이아웃 은 다음 과 같 습 니 다.

<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="?android:attr/textAppearanceLarge" /> 
 
 <ProgressBar 
 android:id="@+id/progressBar1" 
 style="?android:attr/progressBarStyleHorizontal" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignParentBottom="true" 
 android:layout_marginBottom="49dp" 
 android:layout_toLeftOf="@+id/button1" /> 
 
 <ProgressBar 
 android:id="@+id/progressBar2" 
 style="?android:attr/progressBarStyleLarge" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_below="@+id/switch1" 
 android:layout_toRightOf="@+id/switch1" /> 
 
 <RadioButton 
 android:id="@+id/radioButton1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_above="@+id/progressBar1" 
 android:layout_alignLeft="@+id/switch1" 
 android:text="RadioButton" /> 
 
 <Button 
 android:id="@+id/button3" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignLeft="@+id/progressBar2" 
 android:layout_below="@+id/progressBar2" 
 android:text="Button" /> 
 
 <ImageView 
 android:id="@+id/background" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#fff" 
 android:visibility="gone" /> 
 
</RelativeLayout> 
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; 
 
 
 /** 
 *   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; 
 } 
 } 
} 
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기