1.目的:
体验Android开发过程,以悬浮球菜单弹出收回为例子
2.过程
- 删除Hello World!文本,即删掉<TextView ... />
- 将
androidx.constraintlayout.widget.ConstraintLayout
更改为RelativeLayout
- 将需要的图片资源导入Android->res->mipmap中
- 创建四个ImageView并且布局
- 给最外层的ImageView添加点击事件,Alt+Enter快速创建对应方法
- 定义相关变量
- 根据id找到视图,放入List
- 点击触发的方法
- close和open的具体内容
private void close(){
for (int i = 0;i < imageViews.size();i++){
//取出视图
ImageView iv = imageViews.get(i);
// 添加动画
ObjectAnimator oa;
ObjectAnimator ob;
if (i == 0){
oa = ObjectAnimator.ofFloat(iv,"translationX",280f,0f);
}else if(i == 1){
oa = ObjectAnimator.ofFloat(iv,"translationX",250f,0f);
ob = ObjectAnimator.ofFloat(iv,"translationY",250f,0f);
ob.setDuration(500);
ob.start();
}else {
oa = ObjectAnimator.ofFloat(iv,"translationY",280f,0f);
}
oa.setDuration(500);
oa.start();
}
}
private void open(){
for (int i = 0;i < imageViews.size();i++){
//取出视图
ImageView iv = imageViews.get(i);
// 添加动画
ObjectAnimator oa;
ObjectAnimator ob;
if (i == 0){
oa = ObjectAnimator.ofFloat(iv,"translationX",0f,280f);
}else if(i == 1){
oa = ObjectAnimator.ofFloat(iv,"translationX",0f,250f);
ob = ObjectAnimator.ofFloat(iv,"translationY",0f,250f);
ob.setDuration(500);
ob.start();
}else {
oa = ObjectAnimator.ofFloat(iv,"translationY",0f,280f);
}
oa.setDuration(500);
oa.start();
}
}
3.运行结果

网友评论