【Android+Kotlin】自适应CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout,Palette与FloatingActionButt

参考文档

  1. CoordinatorLayout 完全解析
    1.1 关于NestedScrollingParent2、NestedScrollingChild2接口目的 支持滑动嵌套类的父布局 这篇文的demo挺好的
    1.2 提供一个顶层布局来支持子View的协调交互,就完全解析里的floating样例很经典。
    1.3 CoordinatorLayout和RecyclerView嵌套滑动冲突解决
    1)对内部嵌套的横向滑动的RecyclerView 设置setNestedScrollingEnabled很好用 可以解滑动冲突,包括收起了的CoordinatorLayout margintop也拉不下来 贼好用
    2)如果CollapsingToolbarLayout嵌套ConstrLayout会一起上移,ToolBar会固定住,CoordinatorLayout 能力有限,具体的还得自己做动画
    3)AppLayoutBar和不动的ToolBar结合,偏移量会到ToolBar为止,下级依赖的高度不能比AppBarLayout大(不然收到的偏移量为0),实测相等是可以的
    4)NestedScrollView高度必须要match_parent 不然影响上浮效果;
    5)Activity的手势gesture可以不受影响,onTouchEvent会被拦截;
    6)addOnOffsetChangedListener 设一个匿名对象remove时不好处理,但可以传this 以activity为实现对象,移除移this即可;
    7)学习CoordinatorLayout你需要知道的那些事
    在这里插入图片描述
  2. [CollapsingToolbarLayout说明] 可以实现折叠
    2.1 他没有自带的监听,可以手动写监听,根据距离判断展开/收起
    在这里插入图片描述
    2.1 AppBarLayout:ToolBar一般定在最上面,这个可以使其滚动起来。
    在这里插入图片描述
  3. FloatingActionButton基本使用及踩坑记录

1. 初级样例传送门

  1. CollapsingToolbarLayout
  2. View - AppBarLayout(一)使用
  3. Android 详细分析AppBarLayout的五种ScrollFlags
  4. CollapsingToolbarLayout滑动状态监听
  5. CollapsingToolbarLayout实现自动折叠和展开
    5.1 可设置动画,但是boolean类型
    5.2 AppbarLayout最详细使用说明
  6. 内嵌view
  7. 利用 CollapsingToolbarLayout 完成联动的动画效果 结合距离公式在这里插入图片描述

在这里插入图片描述

2. 细节注意

  1. androidx.coordinatorlayout.widget.CoordinatorLayout(协调者布局)包裹的内容
  2. AppBarLayout如果我们想要实现折叠的ActionBar效果
  3. CollapsingToolbarLayout主要是实现折叠布局的,需要作为AppBarLayout的子View才会有效
  4. AppbarLayout的简单用法
  5. 简单结构
oordinatorLayout orientation="vertical mm
	AppBarLayout mh
		CollapsingToolbarLayout mm app:layout_scrollFlags="scroll|exitUntilCollapsed"
			ConstraintLayout mm

在这里插入图片描述
7. 其余各种属性详解
1)contentScrim
当CollapsingToolbarLayout完全折叠后的背景颜色。
通常设置为:app:contentScrim=”?attr/colorPrimary”,这样当CollapsingToolbarLayout完全折叠后就会显示主题颜色。
2)layout_scrollFlags:
设置滚动表现:
1、 Scroll, 表示手指向上滑动的时候,CollapsingToolbarLayout也会向上滚出屏幕并且消失,这个属性必须要有。
2、 exitUntilCollapsed, 表示这个layout会一直滚动离开屏幕范围,直到它收折成它的最小高度.
8. 补充B站例子

修改Floating颜色

  1. 核心代码:
ColorStateList colorStateList = ContextCompat.getColorStateList(getApplicationContext(), R.color.red;
fabRandomCircle.setBackgroundTintMode(PorterDuff.Mode.SRC_ATOP);
fabRandomCircle.setBackgroundTintList(colorStateList);
  1. 不错的思路补充

加入Palette,一定要看

1. 安卓自带的Palette,需要Bitmap

1.5 补充

2. 操作

1.当一张图色彩区域不是很明显 设置默认颜色是有效的 如飞鼠

  1. 默认红色
    在这里插入图片描述

  2. 默认蓝色
    在这里插入图片描述

  1. 经过多张图片测试 主导色最好用 且测试发现 如果设置默认值的方式 返回的一定是颜色值,不传递默认值 返回的是Palette.Swatch对象。这个对象的方法很好用

class MainActivity : AppCompatActivity() {
    var t1: TextView? = null
    var t2: TextView? = null
    var t3: TextView? = null
    var t4: TextView? = null
    var t5: TextView? = null
    var t6: TextView? = null
    var t7: TextView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        t1 = findViewById<View>(R.id.t1) as TextView
        t2 = findViewById<View>(R.id.t2) as TextView
        t3 = findViewById<View>(R.id.t3) as TextView
        t4 = findViewById<View>(R.id.t4) as TextView
        t5 = findViewById<View>(R.id.t5) as TextView
        t6 = findViewById<View>(R.id.t6) as TextView
        t7 = findViewById<View>(R.id.t7) as TextView

        val bitmap = BitmapFactory.decodeResource(resources, R.drawable.mouse)
        Palette.from(bitmap).generate(object : Palette.PaletteAsyncListener {
            //发生主线程    Palette调色板   总共六种颜色


            override fun onGenerated(palette: Palette?) {
                palette?.let {
                    //柔和而暗的颜色
                    val darkMutedColor: Int = palette.getDarkMutedColor(Color.RED)
                    //鲜艳和暗的颜色
                    val darkVibrantColor: Int = palette.getDarkVibrantColor(Color.RED)
                    //亮和鲜艳的颜色
                    val lightVibrantColor: Int = palette.getLightVibrantColor(Color.RED)
                    //亮和柔和的颜色
                    val lightMutedColor: Int = palette.getLightMutedColor(Color.RED)
                    //柔和颜色
                    val mutedColor: Int = palette.getMutedColor(Color.RED)
                    val vibrantColor: Int = palette.getVibrantColor(Color.RED)
                    //返回Swatch主导颜色
                    val max: Palette.Swatch? = palette.dominantSwatch

                    t1!!.setBackgroundColor(darkMutedColor)
                    t2!!.setBackgroundColor(darkVibrantColor)
                    t3!!.setBackgroundColor(lightVibrantColor)
                    t4!!.setBackgroundColor(lightMutedColor)
                    t5!!.setBackgroundColor(mutedColor)
                    t6!!.setBackgroundColor(vibrantColor)
                    if (max != null) {
                        t7!!.setBackgroundColor(max.rgb)
                        t7!!.setTextColor(max.titleTextColor)
                    }
                }


            }
        })
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值