在线建站网页制作网站建设平台,品牌营销策划岗位职责,网站开发的论文引言,兰州企业建设网站ConstraintLayout(约束布局)是Android Studio 2.2中主要的新增功能之一#xff0c;Android studio升级到2.3版本之后#xff0c;不管是新建Activity或fragment#xff0c;xml默认布局由RelativeLayout更改为ConstraintLayout了。按照以往惯例先上效果图#xff1a;GIF.gif上…ConstraintLayout(约束布局)是Android Studio 2.2中主要的新增功能之一Android studio升级到2.3版本之后不管是新建Activity或fragmentxml默认布局由RelativeLayout更改为ConstraintLayout了。按照以往惯例先上效果图GIF.gif上图效果所示 根据手势滑动View 改变View的位置实现方法多种此处记录下使用ConstraintLayout布局动画实现的过程(此篇笔记仅仅只记录应用demo不讲解动画实现的原理如有错误之处还望指正 3Q_)1.首先保证Android studio升级到2.3版本之后然后新建项目编写xml布局(引用android.support.constraint.ConstraintLayout控件)xmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:backgroundcolor/colorPrimaryandroid:orientationverticaltools:contextcom.dawnling.app.MainActivityandroid:idid/imgBackandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginLeft8dpandroid:layout_marginTop15dpandroid:layout_marginBottom15dpapp:srcCompatmipmap/ic_back /android:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:idid/mainandroid:idid/rlTopandroid:layout_width0dpandroid:layout_height375dpandroid:backgrounddrawable/shape_bg_qrcode_transparentapp:layout_constraintTop_toTopOfparentandroid:layout_marginEnd16dpapp:layout_constraintLeft_toLeftOfparentapp:layout_constraintRight_toRightOfparentandroid:layout_marginStart16dpandroid:idid/tvTitleTopandroid:layout_widthmatch_parentandroid:layout_height50dpandroid:backgrounddrawable/shape_bg_qrcode_topandroid:textColorcolor/colorTextPrimaryLightandroid:textSize16spandroid:gravitycenter_verticalandroid:paddingLeft15dpandroid:text会员卡 /android:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:layout_belowid/tvTitleTopandroid:backgrounddrawable/shape_bg_qrcode_bottomandroid:idid/tvSettingPayPwsandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:padding8dpapp:layout_constraintLeft_toLeftOfparentapp:layout_constraintRight_toRightOfparentapp:layout_constraintBottom_toBottomOfparentandroid:layout_marginBottom50dpandroid:backgrounddrawable/shap_password_bgandroid:textColorcolor/whiteandroid:text设置支付密码保护交易安全android:visibilityinvisible/android:idid/rlBottomandroid:layout_width0dpandroid:layout_height400dpandroid:backgrounddrawable/shape_bg_qrcode_transparentandroid:layout_marginRight16dpapp:layout_constraintTop_toBottomOfid/tvSettingPayPwsandroid:layout_marginEnd16dpandroid:layout_marginLeft16dpapp:layout_constraintLeft_toLeftOfparentapp:layout_constraintRight_toRightOfparentandroid:layout_marginStart16dpandroid:idid/tvTitleBottomandroid:layout_widthmatch_parentandroid:layout_height50dpandroid:backgrounddrawable/shape_bg_qrcode_topandroid:textColorcolor/colorTextPrimaryLightandroid:textSize16spandroid:gravitycenter_verticalandroid:paddingLeft15dpandroid:text付款码 /android:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:layout_belowid/tvTitleBottomandroid:backgrounddrawable/shape_bg_qrcode_bottom2.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 不是Overrideprotected 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);//设置marginTopapplyConstraintSet.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));//设置marginTopapplyConstraintSet.centerHorizontally(R.id.rlBottom, R.id.main);//设置支付密码tvSettingPayPws.setVisibility(View.VISIBLE);new Handler().postDelayed(new Runnable() {Overridepublic 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);//设置marginTopapplyConstraintSet.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);//设置marginBottomapplyConstraintSet.centerHorizontally(R.id.rlBottom, R.id.main);//设置支付密码tvSettingPayPws.setVisibility(View.INVISIBLE);new Handler().postDelayed(new Runnable() {Overridepublic void run() {//延时设置标题栏透明度tvTitleTop.setBackgroundResource(R.drawable.shape_bg_qrcode_top);tvTitleBottom.setBackgroundResource(R.drawable.shape_bg_qrcode_top_transparent);}}, 300);applyConstraintSet.applyTo(constraintLayout);}Overridepublic 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在当前窗口内的绝对坐标含toolBarisBottom !isBottom;if(y1 - y2 10) {//向上滑动slide();} else if(y2 - y1 10) {//向下滑动inHiding();}}return super.dispatchTouchEvent(event);}}需要kotlin版本的小伙伴可以留言或私信我