仿IOS底部弹出效果
来源:互联网 发布:淘宝开店基础 编辑:程序博客网 时间:2024/05/07 15:40
最近在做项目时看到旁边ios的孩子实现的从手机底部弹出一个对话框的效果很不赖,自己也想着实现一下,然后回来查了下资料,基本实现了,不过这个只能每次使用时都写一遍,最想实现的还是可以动态添加数据的,而不是写死的。现在把过程记录下来,以便以后时常拿出来看看,加深记忆。
步骤如下:
- 布局文件的准备
- shape资源
- 动画的准备
- style.xml中引用动画
- Java代码
布局文件,popupwindow.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:background="@drawable/popup_shape" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="16dp" android:text="请选择照片" android:textColor="#666" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="0.1px" android:background="#888" /> <TextView android:id="@+id/tv_pick_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="16dp" android:text="从手机相册选择" android:textColor="#118" android:textSize="18sp" /> <View android:layout_width="match_parent" android:layout_height="0.1px" android:background="#888" /> <TextView android:id="@+id/tv_pick_zone" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="16dp" android:text="拍照" android:textColor="#118" android:textSize="18sp" /></LinearLayout><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:background="@drawable/popup_shape"> <TextView android:id="@+id/tv_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="16dp" android:text="取消" android:textColor="#118" android:textSize="18sp" android:textStyle="bold" /></LinearLayout></LinearLayout>
shape资源的生产popup_shape.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><corners android:radius="5dp" /><solid android:color="#88ffffff" /></shape>
进入动画和退出动画的准备,popup_in.xml和popup_out.xml
`<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"android:fromYDelta="100%" android:toYDelta="0"android:duration="200"/>`<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"android:fromYDelta="0" android:toYDelta="100%"android:duration="200"/>
在style.xml中引用动画
<!--弹窗动画--><style name="PopupWindow"> <item name="android:windowEnterAnimation">@anim/popup_in</item> <item name="android:windowExitAnimation">@anim/popup_out</item></style>
Java代码的书写
`public class MainActivity extends AppCompatActivity implements View.OnClickListener, PopupWindow.OnDismissListener {private PopupWindow popupWindow;private int navigationHeight;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn_open_action_sheet = (Button) findViewById(R.id.btn_open_action_sheet); btn_open_action_sheet.setOnClickListener(this); int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android"); navigationHeight = getResources().getDimensionPixelSize(resourceId);}@Overridepublic void onClick(View v) { switch (v.getId()) { case R.id.btn_open_action_sheet: openPopupWindow(v); break; case R.id.tv_pick_phone: openPictures(); popupWindow.dismiss(); break; case R.id.tv_pick_zone: openCammer(); popupWindow.dismiss(); break; case R.id.tv_cancel: popupWindow.dismiss(); break; }}@Overridepublic void onDismiss() { setBackgroundAlpha(1);}//打开ActionSheet的方法private void openPopupWindow(View v) { //防止重复按按钮 if (popupWindow != null && popupWindow.isShowing()) { return; } //设置PopupWindow的View View view = LayoutInflater.from(this).inflate(R.layout.popupwindow, null); popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); //设置背景 popupWindow.setBackgroundDrawable(new BitmapDrawable()); //设置点击弹窗外退出 popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); //设置动画 popupWindow.setAnimationStyle(R.style.PopupWindow); //设置显示的位置 popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, navigationHeight); //设置消失监听 popupWindow.setOnDismissListener(this); //设置PopupWindow的View点击事件 setOnPopupViewClick(view); //设置背景透明度 setBackgroundAlpha(0.5f);}private void setOnPopupViewClick(View view) { TextView tv_pick_phone, tv_pick_zone, tv_cancel; tv_pick_phone = (TextView) view.findViewById(R.id.tv_pick_phone); tv_pick_zone = (TextView) view.findViewById(R.id.tv_pick_zone); tv_cancel = (TextView) view.findViewById(R.id.tv_cancel); tv_pick_phone.setOnClickListener(this); tv_pick_zone.setOnClickListener(this); tv_cancel.setOnClickListener(this);}//设置屏幕背景透明效果public void setBackgroundAlpha(float alpha) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = alpha; getWindow().setAttributes(lp);}//打开系统照相机private void openCammer() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File outDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if (!outDir.exists()) { outDir.mkdirs(); } outFile = new File(outDir, System.currentTimeMillis() + ".jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outFile)); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO); } else { Log.e("CAMERA", "请确认已经插入SD卡"); }}//打开系统相册private void openPictures() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); startActivityForResult(intent,2);}}
`
0 0
- 仿IOS底部弹出效果
- Android仿ios底部弹出框效果
- 仿QQ底部弹出效果
- 仿IOS底部选项效果--底部弹出PopupWindow,背景变为半透明效果
- 自定义仿IOS底部弹出Dialog
- Android仿IOS底部弹出选择菜单ActionSheet
- [Android]仿IOS选择拍照相册底部弹出
- Android——仿ios底部弹出选择框
- (工具)自定义Dialog仿ios弹出底部菜单
- 底部弹出框拍照,从相册选取,取消 仿 ios
- PopupWindow底部弹出效果
- 仿苹果底部弹出Dialog
- 一个底部弹出Popwindow的效果(类似Ios)
- iOS 手动实现modal效果,从底部弹出一个控制器
- 仿ios底部菜单栏
- 仿网易下拉菜单,UC底部弹出菜单(带动画效果,讲解原理)
- [demo]仿IOS底部弹框效果及Android各类对话框和弹框效果
- Android自定义Dialogの仿ios系统底部菜单(自下而上弹出)
- 面试题20:顺时针打印矩阵
- 陈寅恪的最后20年 - 笔记
- Linux 高级IO
- Oracle11g rac常用命令
- 面试题21:包含min函数的栈
- 仿IOS底部弹出效果
- HTTP状态码详解
- 关于super 关键字的坑
- AngularJS2——使用Angular Cli 快速搭建工程
- 00002 贪婪洞窟.004:解密存档文件
- 面试题22:栈的压入、弹出序列
- cassandra vs mongo (1)存储引擎
- 栈 (Stack) 的定义和实现
- Java线程池