窗口背景随弹窗的出现而渐变
来源:互联网 发布:php人才招聘系统 编辑:程序博客网 时间:2024/06/05 10:09
之前写都是点击弹窗的时候直接将背景变成半透明,但是客户要求进行渐变
一、使用Activity.getWindow().setAttributes()方法来实现
工具代码灵活使用
1、窗口渐变工具代码
实现渐变的代码如下
public class WindowsBgAlpha { private Activity mActivity; Handler handler = new Handler(); public WindowsBgAlpha(Activity activity) { this.mActivity = activity; } /** * 设置添加屏幕的背景透明度, * 在给定的时间内渐变 */ private float currentAlpha ; private boolean isAdd = false; public void backgroundAlpha(float startBgAlpha, final float endBgAlpha, long time) { //计算,实现渐变过程 int end = (int) (endBgAlpha * 10); int start = (int) (startBgAlpha * 10); final int num ; //时间分为几份 if (start>end){ isAdd = false; num = start - end; }else { isAdd = true; num = end - start; } final long mTime = (time / num);//每段变化使用的时长 final WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes(); currentAlpha = startBgAlpha; handler.postDelayed(new Runnable() { @Override public void run() { if (isAdd){ currentAlpha += 0.1; if (currentAlpha>endBgAlpha){ handler.removeCallbacks(this); }else { handler.postDelayed(this,mTime); } }else { currentAlpha -= 0.1; if (currentAlpha<endBgAlpha){ handler.removeCallbacks(this); }else { handler.postDelayed(this,mTime); } } lp.alpha = currentAlpha; //0.0-1.0 mActivity.getWindow().setAttributes(lp); } }, mTime); }}
2、和弹窗一起使用
随弹窗的出现而进行渐变,主要就是这个方法backgroundAlpha(float startBgAlpha, final float endBgAlpha, long time)
private void showPopupWindow() { View showView = LayoutInflater.from(BarrierActivity.this).inflate(R.layout.sub_popup_window, null); popupWindow = new PopupWindow(showView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); popupWindow.setAnimationStyle(R.style.AnimationPopup); //一定要在代码中setBackgroundDrawable,不然点击外面popupWindow是不会dismiss的 popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.choose_sub_shape)); View v = LayoutInflater.from(BarrierActivity.this).inflate(R.layout.activity_brkthough, null); ImageView menuDown = (ImageView) showView.findViewById(R.id.menu_down); menuDown.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); popupWindow.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); new WindowsBgAlpha(BarrierActivity.this).backgroundAlpha(1.0f, 0.6f, 200); popupWindow.setOnDismissListener(new poponDismissListener()); RecyclerView recyclerView = (RecyclerView) showView.findViewById(R.id.recycler_view); TextView popupTime = (TextView) showView.findViewById(R.id.popup_time); long nowTime = clockUtils.getNowTime(); clockUtils.stopTime(); clock = new ClockUtils(nowTime, handler, popupTime); clock.startTime(); ChooseSubjectAdapter adapter = new ChooseSubjectAdapter(BarrierActivity.this, mData, totalCount, new ChooseSubjectAdapter.ItemClickListener() { @Override public void onClick(View v) { int tag = (int) v.getTag(); brkthoughActivityViewpager.setCurrentItem(tag, true); popupWindow.dismiss(); } }); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new GridLayoutManager(this, 5)); } class poponDismissListener implements PopupWindow.OnDismissListener { @Override public void onDismiss() { //这里的时间最好和窗口进出时动画执行时间一致 new WindowsBgAlpha(BarrierActivity.this).backgroundAlpha(0.6f, 1.0f, 300); if (clock!=null){ long nowTime = clock.getNowTime(); clock.stopTime(); if (nowTime==0){ bottomTime.setText("00:00"); }else { clockUtils.refreshTime(nowTime); } } } }
二 、 通过给Dialog设置style来实现渐变
//color <color name="tram">#00000000</color> //animal <style name="DialogAnimation"> <item name="android:windowEnterAnimation">@anim/abc_slide_in_bottom</item> <item name="android:windowExitAnimation">@anim/abc_slide_out_bottom</item> </style> <style name="dialog_style" parent="android:Theme.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@color/tram</item> <item name="android:windowAnimationStyle">@style/DialogAnimation</item> </style>
abc_slide_in_bottom
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:fromYDelta="50%p" android:toYDelta="0" android:duration="@android:integer/config_mediumAnimTime"/>
abc_slide_out_bottom
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromYDelta="0" android:toYDelta="50%p" android:duration="@android:integer/config_mediumAnimTime"/>
阅读全文
0 0
- 窗口背景随弹窗的出现而渐变
- 渐变的窗口背景(C#2005)
- 【C#】[窗体]渐变窗口背景
- 表格背景的渐变
- UIView的背景渐变
- 背景颜色的渐变
- 点击弹出窗口,并且背景变暗渐变
- 使窗口背景有渐变色
- 自定义控件--让背景颜色随ViewPager的滑动而渐变
- 表格背景渐变的实现
- div渐变背景的实现
- 椭圆形、渐变背景的文本框
- 渐变色的背景实现
- 自定义渐变背景的文本框
- 炫酷的渐变背景
- 在窗口上显示图片,渐变色背景贴图
- 点击弹出窗口层,并且背景变暗渐变
- 点击弹出窗口层,并且背景变暗渐变
- oracle两个数据库间对表进行授权
- 关于JSON最实用的代码(二)
- python学习笔记一
- 2017NOIp 普及组第一题
- 论文<Algorithms for non-negative matrix Factorization>
- 窗口背景随弹窗的出现而渐变
- MySQL 数据类型与存储长度
- Struts2学习笔记(3)-如何访问Web资源
- AD DRC检测不能查出断连的线
- [问题-已解决]arm-none-linux-gnueabi-gcc没有那个文件或目录
- 推荐算法
- Mybatis基于注解的方式获取新增记录的自增长id
- 链表栈的基本操作的实现---入栈、出栈、清空
- MyCat是什么?