android 卡片放大变详情页,ConstraintLayout动画实现布局卡片式滑动放大缩放

ConstraintLayout(约束布局)是Android Studio 2.2中主要的新增功能之一,Android studio升级到2.3版本之后,不管是新建Activity或fragment,xml默认布局由RelativeLayout更改为ConstraintLayout了。

按照以往惯例先上效果图:

717d2723da11

GIF.gif

上图效果所示 根据手势滑动View 改变View的位置实现方法多种,此处记录下使用ConstraintLayout布局动画实现的过程(此篇笔记仅仅只记录应用demo,不讲解动画实现的原理,如有错误之处还望指正 3Q_):

1.首先保证Android studio升级到2.3版本之后,然后新建项目,编写xml布局(引用android.support.constraint.ConstraintLayout控件):

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/colorPrimary"

android:orientation="vertical"

tools:context="com.dawnling.app.MainActivity">

android:id="@+id/imgBack"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="8dp"

android:layout_marginTop="15dp"

android:layout_marginBottom="15dp"

app:srcCompat="@mipmap/ic_back" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/main">

android:id="@+id/rlTop"

android:layout_width="0dp"

android:layout_height="375dp"

android:background="@drawable/shape_bg_qrcode_transparent"

app:layout_constraintTop_toTopOf="parent"

android:layout_marginEnd="16dp"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

android:layout_marginStart="16dp">

android:id="@+id/tvTitleTop"

android:layout_width="match_parent"

android:layout_height="50dp"

android:background="@drawable/shape_bg_qrcode_top"

android:textColor="@color/colorTextPrimaryLight"

android:textSize="16sp"

android:gravity="center_vertical"

android:paddingLeft="15dp"

android:text="会员卡" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_below="@+id/tvTitleTop"

android:background="@drawable/shape_bg_qrcode_bottom">

android:id="@+id/tvSettingPayPws"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="8dp"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintBottom_toBottomOf="parent"

android:layout_marginBottom="50dp"

android:background="@drawable/shap_password_bg"

android:textColor="@color/white"

android:text="设置支付密码,保护交易安全"

android:visibility="invisible"/>

android:id="@+id/rlBottom"

android:layout_width="0dp"

android:layout_height="400dp"

android:background="@drawable/shape_bg_qrcode_transparent"

android:layout_marginRight="16dp"

app:layout_constraintTop_toBottomOf="@+id/tvSettingPayPws"

android:layout_marginEnd="16dp"

android:layout_marginLeft="16dp"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

android:layout_marginStart="16dp">

android:id="@+id/tvTitleBottom"

android:layout_width="match_parent"

android:layout_height="50dp"

android:background="@drawable/shape_bg_qrcode_top"

android:textColor="@color/colorTextPrimaryLight"

android:textSize="16sp"

android:gravity="center_vertical"

android:paddingLeft="15dp"

android:text="付款码" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_below="@+id/tvTitleBottom"

android:background="@drawable/shape_bg_qrcode_bottom">

2.ConstraintLayout 动画要new一个ConstraintSet变量用于存放view的改变状态

private ConstraintSet applyConstraintSet = new ConstraintSet();

3.接下来看下MainActivity.java代码:

/**

* Created by LXL on 2018/1/17.

* http://my.csdn.net/lxlyhm

* https://github.com/LXLYHM

* http://www.jianshu.com/u/8fd63a0d4c4c

* ConstraintLayout 动画

*/

public class MainActivity extends AppCompatActivity {

private int hight;

private ConstraintLayout constraintLayout;

private ConstraintSet applyConstraintSet = new ConstraintSet();

private View rlTop;//会员卡

private View rlBottom;//付款码

private int rlBottomHeight;

private int rlTopHeight;

private int rlTopWidth;

private TextView tvSettingPayPws;//设置密码

private TextView tvTitleTop;

private int[] location = new int[2] ;//会员卡布局 坐标

private float y1;

private float y2;

private TextView tvTitleBottom;

private boolean isFirstMeasure = true;//是否是第一次测量

private boolean isBottom = false;//付款码是否隐藏 是否在底部 true 是 false 不是

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//状态栏全局默认统一成白底黑字

ImmersionBar.with(this).statusBarColor(R.color.colorPrimary).fitsSystemWindows(true).init();

hight = ScreenUtils.getScreenHeight();//屏幕高度

constraintLayout = (ConstraintLayout) findViewById(R.id.main);

rlTop = findViewById(R.id.rlTop);

rlBottom = findViewById(R.id.rlBottom);

tvTitleTop = (TextView) findViewById(R.id.tvTitleTop);

tvSettingPayPws = (TextView) findViewById(R.id.tvSettingPayPws);

tvTitleBottom = (TextView) findViewById(R.id.tvTitleBottom);

getRlTopMeasure();

}

/**

* 获取会员卡 付款码 布局宽高

*/

private void getRlTopMeasure() {

rlBottomHeight = rlBottom.getLayoutParams().height;//付款码 高

int w = View.MeasureSpec.makeMeasureSpec(0,

View.MeasureSpec.UNSPECIFIED);

int h = View.MeasureSpec.makeMeasureSpec(0,

View.MeasureSpec.UNSPECIFIED);

rlTop.measure(w, h);

rlTopHeight = rlTop.getMeasuredHeight();//会员卡高

}

//上滑覆盖

public void slide(){

TransitionManager.beginDelayedTransition(constraintLayout);

//设置 会员卡布局

applyConstraintSet.constrainWidth(R.id.rlTop, rlTopWidth - 36);//设置变小

applyConstraintSet.constrainHeight(R.id.rlTop, rlTopHeight);//设置高度不变

applyConstraintSet.connect(R.id.rlTop, ConstraintSet.TOP, R.id.main,ConstraintSet.TOP, 20);//设置marginTop

applyConstraintSet.centerHorizontally(R.id.rlTop, R.id.main);

//设置 付款码布局

applyConstraintSet.constrainWidth(R.id.rlBottom, rlTopWidth);//设置变小

applyConstraintSet.constrainHeight(R.id.rlBottom, rlBottomHeight);//设置高度不变

applyConstraintSet.connect(R.id.rlBottom, ConstraintSet.TOP, R.id.main,ConstraintSet.TOP, (location[1]/2 + 25));//设置marginTop

applyConstraintSet.centerHorizontally(R.id.rlBottom, R.id.main);

//设置支付密码

tvSettingPayPws.setVisibility(View.VISIBLE);

new Handler().postDelayed(new Runnable() {

@Override

public void run() {//延时设置标题栏透明度

tvTitleTop.setBackgroundResource(R.drawable.shape_bg_qrcode_top_transparent);

tvTitleBottom.setBackgroundResource(R.drawable.shape_bg_qrcode_top);

}

}, 300);

applyConstraintSet.applyTo(constraintLayout);

}

//下滑隐藏

public void inHiding(){

TransitionManager.beginDelayedTransition(constraintLayout);

//设置 会员卡布局

applyConstraintSet.constrainWidth(R.id.rlTop, rlTopWidth);//设置还原

applyConstraintSet.constrainHeight(R.id.rlTop, rlTopHeight);//设置高度不变

applyConstraintSet.connect(R.id.rlTop, ConstraintSet.TOP, R.id.main,ConstraintSet.TOP, 20);//设置marginTop

applyConstraintSet.centerHorizontally(R.id.rlTop, R.id.main);

//设置 付款码布局

applyConstraintSet.constrainWidth(R.id.rlBottom, rlTopWidth);

applyConstraintSet.constrainHeight(R.id.rlBottom, rlBottomHeight);//设置高度不变

applyConstraintSet.connect(R.id.rlBottom, ConstraintSet.TOP, R.id.main, ConstraintSet.TOP, hight - location[1] - 50);//设置marginBottom

applyConstraintSet.centerHorizontally(R.id.rlBottom, R.id.main);

//设置支付密码

tvSettingPayPws.setVisibility(View.INVISIBLE);

new Handler().postDelayed(new Runnable() {

@Override

public void run() {//延时设置标题栏透明度

tvTitleTop.setBackgroundResource(R.drawable.shape_bg_qrcode_top);

tvTitleBottom.setBackgroundResource(R.drawable.shape_bg_qrcode_top_transparent);

}

}, 300);

applyConstraintSet.applyTo(constraintLayout);

}

@Override

public boolean dispatchTouchEvent(MotionEvent event) {

//继承了Activity的onTouchEvent方法,直接监听点击事件

if(event.getAction() == MotionEvent.ACTION_DOWN) {

//当手指按下的时候

y1 = event.getY();

}

if(event.getAction() == MotionEvent.ACTION_UP) {

//当手指离开的时候

y2 = event.getY();

if (isFirstMeasure){

rlTopWidth = rlTop.getWidth();

}

isFirstMeasure = false;

rlTop.getLocationInWindow(location); //获取rlTop在当前窗口内的绝对坐标,含toolBar

isBottom = !isBottom;

if(y1 - y2 > 10) {//向上滑动

slide();

} else if(y2 - y1 > 10) {//向下滑动

inHiding();

}

}

return super.dispatchTouchEvent(event);

}

}

需要kotlin版本的小伙伴可以留言或私信我

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421976.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

开源路由软件zebra的命令存储原理及使用方法

ZZ FROM: http://www.cnblogs.com/iTsihang/archive/2012/12/05/2783252.html zebra在设计命令格式的时候,将命令节点Node和命令Command分开,整个命令结构是一个树状的,如图: NODEn表示命令节点,CMDn表示具体的命令。在zebra的整…

html文本框样式大全,HTML文本框样式大全

输入框景背景透明:鼠标划过输入框,输入框背景色变色:style"width: 106; height: 21"οnmοuseοut"this.style.borderColorblack;this.style.backgroundColor#ffffff" style"border-width:1px;border-colorblack&qu…

和菜鸟一起深入学习国嵌实验之简单Makefile

1、首先是待make的程序&#xff1a; #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>int main(void) {printf("Hello Makefile!\n");return 0; }2、其次就是其makefile了&#xff1a; CC $(CROSS_COM…

网易邮箱写邮件HTML转换按钮,网易邮箱推出虚拟场景写信功能 身临其境写邮件...

当你用电子邮件给好友发出问候时&#xff0c;是否怀念着曾经繁茂成荫、绿野同行的过往&#xff1f;当你给他(她)写信表达思念时&#xff0c;是否憧憬着秋风初起、阳光清透的意境&#xff1f;近期&#xff0c;网易邮箱独家推出“虚拟场景写信”功能&#xff0c;可以帮助用户在写…

2013年的目标

1 精通c#在clr中的运行机制【没有完成】 2 精通oralce数据库【没有完成】 3 学习java的开发。【没有完成】 4 精通业务系统&#xff0c;主要是流程&#xff0c;以及凭证的要求。【完成一半】 5 精通一种c#的开源框架 csla.net【没有完成】 6 精通办公软件的使用 office excel p…

批量下载baidu音乐主页的歌曲

[代码] 华语 package com;import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.Input http://www.fpzhuhai.com/linked/20130207.do; Stream; i…

做简单的android 软件推荐,Android_适用于Android开发的简单聊天软件,适用于android 开发。是一个简 - phpStudy...

适用于Android开发的简单聊天软件适用于android 开发。是一个简单的聊天软件&#xff0c;包括知识点&#xff0c;各个控件的运用(ExpandableListView&#xff0c;ViewPager&#xff0c;Spinner&#xff0c;LinearLayout,RelativeLayot)&#xff0c;自定义的ViaImageView(自定义…

使用自定义表类型(SQL Server 2008)

在 SQL Server 2008 中&#xff0c;用户定义表类型是指用户所定义的表示表结构定义的类型。您可以使用用户定义表类型为存储过程或函数声明表值参数&#xff0c;或者声明您要在批处理中或在存储过程或函数的主体中使用的表变量。有关如何定义表结构的详细信息&#xff0c;请参阅…

html select不能修改,更改HTML Select元素的选定选项

香草JavaScript使用普通的旧JavaScript&#xff1a;var val "Fish";var sel document.getElementById(sel);document.getElementById(btn).onclick function() {var opts sel.options;for (var opt, j 0; opt opts[j]; j) {if (opt.value val) {sel.selectedI…

用excel打开文本内容

linux中直接从数据库导出数据存为txt文本&#xff0c;用文本编辑器打开是这样的&#xff0c;不适合统计数据&#xff0c;转成excel就万事大吉了,这点事微软怎么会想不到呢直接上图&#xff1a;every thing goes well!! :-)来自为知笔记(Wiz)转载于:https://www.cnblogs.com/fir…

html框架有什么作用,使用HTML5+CSS+JS框架有那些好处

使用HTML5CSSJS框架有那些好处2017-12-08相信很多程序猿朋友都用过框架&#xff0c;不过你是否知道你用的是HTML框架、CSS框架还是JS框架&#xff0c;其实这都不重要&#xff0c;重要的是使用框架的目的是什么&#xff1f;是不是节约了开发项目时间陈本&#xff0c;这事多么伟大…

SharePoint【学习笔记】-- SharePoint 2010 技术参数整理

今天整理一些 SharePoint 2010 的技术参数&#xff0c;其内容都来自 SharePoint-Sandbox 网站。 有些参数值是硬性的&#xff0c;比如列表单条记录的尺寸&#xff1b;而有些是为了使用和性能考虑的推荐值。 技术参数值列表最大记录数500万条列表单条记录上限8KB&#xff08;不含…

目前微型计算机中常用的鼠标器有什么两类,2009年计算机一级考试真题及答案...

2009年计算机一级考试真题及答案。一、选择题1、在计算机领域中通常用M IPS来描述______。A、计算机的运算速度B、计算机的可靠性C、计算机的可运行性D、计算机的可扩充性2、微型计算机存储系统中&#xff0c;PROM是______。A、可读写存储器B、动态随机存取存储器C、只读存储器…

当某个快捷键不能用时很可能是热键冲突

解决办法&#xff1a;上次我的复制 ctrl c不能用就是打开的QQ系统设置&#xff0c;只保留qq的截图快捷键&#xff0c;其他的都是无。 哎&#xff0c;一会又热键冲突了&#xff0c;卸载了搜狗输入法就没事了&#xff0c;原来搜狗的问题。转载于:https://www.cnblogs.com/xiaofa…

用HTML写一首绝句古诗,《绝句二首》_杜甫的诗词_诗词名句网

作品赏析迟日江山丽&#xff0c;春风花草香。泥融飞燕子&#xff0c;沙暖睡鸳鸯。清代的诗论家陶虞开在《说杜》一书中指出&#xff0c;杜集中有不少“以诗为画”的作品。这一首写于成都草堂的五言绝句&#xff0c;就是极富诗情画意的佳作。诗一开始&#xff0c;就从大处着墨&a…