android popWindow 弹出时背景透明度渐变

    网上有另外一种方式

      设置背景的透明度 http://blog.csdn.net/ddxxll2008/article/details/49201925

  1.  // 设置屏幕透明度  
  2.     public void backgroundAlpha(float bgAlpha) {  
  3.         WindowManager.LayoutParams lp = getWindow().getAttributes();  
  4.         lp.alpha = bgAlpha; // 0.0~1.0  
  5.         getWindow().setAttributes(lp);  
  6.     }  

在用的时候稍微改版了下:

public class SelectSexPopupWindow extends PopupWindow implements OnClickListener {

   private Button btn_cancel;
   private TextView btn_take_photo, btn_pick_photo;
   private View mMenuView;
   private View trans_view;
   private Context mContext;

   public SelectSexPopupWindow(Activity context, myOnSexClickListener listener) {
      super(context);
      mContext = context;
      myOnSexClickListener = listener;
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      mMenuView = inflater.inflate(R.layout.select_sex_popupwindow, null);
      btn_take_photo = (TextView) mMenuView.findViewById(R.id.btn_myquestion_first);
      btn_pick_photo = (TextView) mMenuView.findViewById(R.id.btn_myquestion_second);
      btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
      trans_view = (View) mMenuView.findViewById(R.id.trans_view);
      // 取消按钮
      btn_cancel.setOnClickListener(new OnClickListener() {

         public void onClick(View v) {
            // 销毁弹出框
            dismiss();
         }
      });

      // 设置按钮监听
      btn_pick_photo.setOnClickListener(this);
      btn_take_photo.setOnClickListener(this);
      // 设置SelectPicPopupWindow的View
      this.setContentView(mMenuView);
      // 设置SelectPicPopupWindow弹出窗体的宽
      this.setWidth(LayoutParams.MATCH_PARENT);
      // 设置SelectPicPopupWindow弹出窗体的高
      this.setHeight(LayoutParams.WRAP_CONTENT);
      // 设置SelectPicPopupWindow弹出窗体可点击
      this.setFocusable(true);
      // 设置SelectPicPopupWindow弹出窗体动画效果
      this.setAnimationStyle(R.style.fade_in_out);
      // 实例化一个ColorDrawable颜色为半透明
      ColorDrawable dw = new ColorDrawable(0x60000000);
      // 设置SelectPicPopupWindow弹出窗体的背景
      this.setBackgroundDrawable(dw);
      // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
      mMenuView.setOnTouchListener(new OnTouchListener() {

         public boolean onTouch(View v, MotionEvent event) {

            int height = mMenuView.findViewById(R.id.pop_layout).getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
               if (y < height) {
                  dismiss();
               }
            }
            return true;
         }
      });

   }

   @Override
   public void onClick(View v) {
      switch (v.getId()) {
         case R.id.btn_myquestion_first:
            myOnSexClickListener.onSexClick("男");
            break;
         case R.id.btn_myquestion_second:
            myOnSexClickListener.onSexClick("女");
            break;
         default:
            break;
      }
      dismiss();
   }

   @Override
   public void dismiss() {
      // 在pop消失之前,给咱们加的view设置背景渐变出场动画(Android3.0以上的开发环境,这里建议使用属性动画,那样很柔和,视觉效果更棒!)
      trans_view.startAnimation(AnimationUtils.loadAnimation(mContext,
            R.anim.anim_bookshelf_folder_editer_exit));
      trans_view.setVisibility(View.GONE);
      super.dismiss();

   }

   @Override
   public void showAtLocation(View parent, int gravity, int x, int y) {
      trans_view.setVisibility(View.VISIBLE);
      trans_view.startAnimation(AnimationUtils.loadAnimation(mContext,
            R.anim.anim_bookshelf_folder_editer_enter));
      super.showAtLocation(parent, gravity, x, y);
   }

   public interface myOnSexClickListener {
      public void onSexClick(String sex);
   }

   public myOnSexClickListener myOnSexClickListener;

}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <View
        android:id="@+id/trans_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="visible"
        android:background="#b0000000"/>
    <!-- #AF000000 70%透明灰 -->
    <LinearLayout
        android:id="@+id/pop_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/layout_cancel"
        android:background="#ffffff"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:gravity="center"
            android:padding="13dip"
            android:text="性别修改"
            android:textColor="@color/text_color_959595"
            android:textSize="@dimen/text_size_22px" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:background="#e0e3e6" />

        <TextView
            android:id="@+id/btn_myquestion_first"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:background="@drawable/freeclinics_btn_click_bg"
            android:gravity="center"
            android:padding="13dip"
            android:text="男"
            android:textColor="@color/content"
            android:textSize="@dimen/text_size_22px" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:background="#e0e3e6" />

        <TextView
            android:id="@+id/btn_myquestion_second"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:background="@drawable/freeclinics_btn_click_bg"
            android:gravity="center"
            android:padding="13dip"
            android:text="女"
            android:textColor="@color/content"
            android:textSize="@dimen/text_size_22px" />

        <View
            android:layout_width="match_parent"
            android:layout_height="10dip"
            android:background="#f2f5f7" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@color/white"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/height50"
            android:background="@drawable/freeclinics_btn_click_bg"
            android:gravity="center"
            android:text="取 消"
            android:textColor="@color/content"
            android:textSize="@dimen/text_size_20px" />
    </LinearLayout>
</RelativeLayout>

下面是转载的原文


思路:将整个布局外包裹一个相对布局,再在相对布局内添加一个线性或者相对布局,宽高match_parent,然后给相对布局第一个子布局设置背景并添加渐变动画
弹出pop之前,在其父布局中addview,view设置一个70%透明灰背景(Android3.0以上可以直接设置背景色为全透明,使用属性动画,这里就不详说了,有兴趣的朋友可以了解下),弹出pop时,给view设置动画就行,多的不说了,上代码。

添加style

</style>

      <style name="BookShelf_bottom_DialogAnimation">
        <item name="android:windowEnterAnimation">@anim/anim_bookshelf_folder_editer_enter</item>  
        <item name="android:windowExitAnimation">@anim/anim_bookshelf_folder_editer_exit</item> 

    </style>

下面的动画效果依次为


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromYDelta="100%p"
    android:duration="500" />
</set>


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >


    <translate
        android:duration="500"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="100%p" />


</set>


<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:startOffset="200"
    android:fillAfter="true"
    android:duration="500"/>


<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:fillAfter="true"
    android:duration="500"/>


layout文件分别为


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_bookshelf_context_rl"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF" >
 
    <RelativeLayout
        android:id="@+id/dialog_layout_title_btn"
        android:layout_width="fill_parent"
        android:layout_height="362dp"
        android:layout_marginTop="18dip"
        android:background="#FFFFFF" >
 
        <TextView
            android:id="@+id/dialog_top_menu_add_class"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="15dip"
            android:drawablePadding="5dp"
            android:gravity="center_vertical"
            android:paddingBottom="5dip"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:paddingTop="5dip"
            android:textSize="14sp"
            android:text="效果如何,有没有错认为是Dialog?^O^..或许你们发现了,变暗效果会微微晃眼,这就需要自己根据实际情况设置自己的初始透明色值了,也就是在这里,建议Android3.0开发环境的朋友们,使用属性动画的原因,那个是不会存在这样的问题的。" />
    </RelativeLayout>
 
</RelativeLayout>



<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     
<TextView
    android:id="@+id/mytext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    android:gravity="center"
    android:padding="30dp"
    android:text="点我显示Pop窗口"
    android:textColor="@android:color/white"/>
<View
    android:id="@+id/myView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#AF000000"/>
    <!-- #AF000000 70%透明灰 -->
</FrameLayout>



package com.example.android_test.anmi;


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.example.android_test.R;
 
public class PopupBackground extends Activity {
    private TextView mytext;
    private View viewBg;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.poptest_main);
        //初始化遮罩层
        viewBg = findViewById(R.id.myView);
        //默认隐藏遮罩层
        viewBg.setVisibility(View.GONE);
        mytext = (TextView) findViewById(R.id.mytext);
        mytext.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showpop(PopupBackground.this);
            }
        });
    }
    /**
     * 显示Pop窗口
     * @param context
     */
    private void showpop(final Context context) {
        View view = View.inflate(context, R.layout.dialog_bookshelf_common,
                null);
        // 最后一个参数false 代表:不与其余布局发生交互, true代表:可以与其余布局发生交互事件
        final PopupWindow popWindow = new PopupWindow(view,
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, false) {
            // 重写popupWindow消失时事件
            @Override
            public void dismiss() {
                // 在pop消失之前,给咱们加的view设置背景渐变出场动画(Android3.0以上的开发环境,这里建议使用属性动画,那样很柔和,视觉效果更棒!)
                viewBg.startAnimation(AnimationUtils.loadAnimation(context,
                        R.anim.anim_bookshelf_folder_editer_exit));
                viewBg.setVisibility(View.GONE);
                super.dismiss();
            }
        };
        // 设置Pop入场动画效果
        popWindow.setAnimationStyle(R.style.BookShelf_bottom_DialogAnimation);
        // 设置Pop响应内部区域焦点
        popWindow.setFocusable(true);
        // 设置Pop响应外部区域焦点
        popWindow.setOutsideTouchable(true);
        // 设置PopupWindow弹出软键盘模式(此处为覆盖式)
        popWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        // 响应返回键必须的语句
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        // 在显示pop之前,给咱们加的view设置背景渐变入场动画(Android3.0以上的开发环境,这里建议使用属性动画,那样很柔和,视觉效果更棒!)
        viewBg.setVisibility(View.VISIBLE);
        viewBg.startAnimation(AnimationUtils.loadAnimation(context,
                R.anim.anim_bookshelf_folder_editer_enter));
        // 依附的父布局自己设定,我这里为了方便,这样写的。
        popWindow.showAtLocation(viewBg, Gravity.BOTTOM, 0, 0);
    }
     
     
}


原文地址:http://www.oschina.net/code/snippet_2308824_45811











猜你喜欢

转载自blog.csdn.net/fumeidonga/article/details/51012465