博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 自定义PopupWindow动画效果
阅读量:6911 次
发布时间:2019-06-27

本文共 4011 字,大约阅读时间需要 13 分钟。

public class RollActivity extends Activity {    private View view;    private Button btn;    private PopupWindow mPopupWindow;    private View[] btns;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);//        LinearLayout layout=(LinearLayout) view.findViewById(R.id.layout_main);//        //设置背景图片旋转180//        Bitmap mBitmap=setRotate(R.drawable.bg_kuang);//        BitmapDrawable drawable=new BitmapDrawable(mBitmap);//        layout.setBackgroundDrawable(drawable);                btn=(Button) this.findViewById(R.id.btn);        btn.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                showPopupWindow(btn);            }                    });                initPopupWindow(R.layout.popwindow);    }        private void initPopupWindow(int resId){        LayoutInflater mLayoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);        view = mLayoutInflater.inflate(resId, null);                    mPopupWindow = new PopupWindow(view, 400,LayoutParams.WRAP_CONTENT);//        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());//必须设置background才能消失        mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_frame));        mPopupWindow.setOutsideTouchable(true);                //自定义动画//        mPopupWindow.setAnimationStyle(R.style.PopupAnimation);        //使用系统动画        mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);        mPopupWindow.update();        mPopupWindow.setTouchable(true);        mPopupWindow.setFocusable(true);                btns=new View[3];        btns[0]=view.findViewById(R.id.btn_0);        btns[1]=view.findViewById(R.id.btn_1);        btns[2]=view.findViewById(R.id.btn_2);        btns[0].setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                // TODO Auto-generated method stub                //doSomething            }        });        btns[1].setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                // TODO Auto-generated method stub                //doSomething            }        });        btns[2].setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                // TODO Auto-generated method stub                //doSomething            }        });    }    private void showPopupWindow(View view) {        if(!mPopupWindow.isShowing()){//            mPopupWindow.showAsDropDown(view,0,0);            mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);        }    }    public Bitmap setRotate(int resId) {        Matrix mFgMatrix = new Matrix();        Bitmap mFgBitmap = BitmapFactory.decodeResource(getResources(), resId);        mFgMatrix.setRotate(180f);        return mFgBitmap=Bitmap.createBitmap(mFgBitmap, 0, 0,                 mFgBitmap.getWidth(), mFgBitmap.getHeight(), mFgMatrix, true);    }}

PopupWindow的布局popwindow.xml 

注意3个LinearLayout里必须设置clickable和background,这样当点击上去的时候才会有点击效果。 
android:clickable="true" 
android:background="@drawable/state_btn_pressed" 

state_btn_pressed.xml,点击的效果:

Android 模仿迅雷的 PopupWindow 出现/消失动画 

出现: 

消失: 

最后用下面的 XML 封装: 

 

 

转载地址:http://cpfcl.baihongyu.com/

你可能感兴趣的文章
W3C 、HTML 、CSS 发展介绍
查看>>
JS实现点击跳转登陆邮箱
查看>>
Alexandra and Prime Numbers(思维)
查看>>
jQuery hover 延时器实现代码
查看>>
ArcGIS JS 学习笔记3 实现百度风格的BubblePopup
查看>>
JavaScript之字符串引号的使用技巧
查看>>
linux如何关闭selinux?
查看>>
初识CSS
查看>>
Android开发常用代码片段
查看>>
微信小程序使用场景及取名“潜”规则
查看>>
Atitit nodejs5 nodejs6 nodejs 7.2.1 新特性attialx总结
查看>>
回顾 git 常用命令
查看>>
第四章 Spring.Net 如何管理您的类___统一资源访问接口
查看>>
最大流+最小费用最大流
查看>>
java-mybaits-00103-入门程序原生的【查、增、删、改】
查看>>
LayoutInflater
查看>>
用sourceTree提交代码时遇到的问题
查看>>
Mysql第一周
查看>>
深入理解 Android 消息机制原理
查看>>
1.一个WEB应用的开发流程
查看>>