android如何做一个按钮,如何制作一个Android凸起按钮?

我想你实际上想要按钮升高.不要使用卡,因为它需要更多资源.用于棒棒糖设备使用

...

android:stateListAnimator="@anim/my_animator" />

并在资源的anim文件夹中创建my_animator.xml

android:duration="@integer/button_pressed_animation_duration"

android:valueTo="@dimen/button_pressed_z_material"

android:valueType="floatType"/>

android:duration="0"

android:valueTo="@dimen/button_elevation_material"

android:valueType="floatType"/>

android:duration="@integer/button_pressed_animation_duration"

android:valueTo="0"

android:startDelay="@integer/button_pressed_animation_delay"

android:valueType="floatType"/>

android:duration="0"

android:valueTo="@dimen/button_elevation_material"

android:valueType="floatType" />

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们可以使用 `Path` 类来绘制贝塞尔曲线。具体来说,我们可以通过控制点来定义贝塞尔曲线的形状。以下是一个示例: ```kotlin val path = Path() path.moveTo(0f, 0f) path.lineTo(0f, height.toFloat()) path.quadTo(width / 2f, height + curveHeight, width.toFloat(), height.toFloat()) path.lineTo(width.toFloat(), 0f) path.close() ``` 以上代码创建了一个路径,从左上角开始,向下绘制一条直线,然后使用二次贝塞尔曲线连接到右上角,形成一个中间凸起的形状。 接下来,我们可以在 `onDraw` 方法中使用上述路径绘制底部导航栏的背景。例如: ```kotlin override fun onDraw(canvas: Canvas) { super.onDraw(canvas) // 绘制背景 canvas.drawPath(path, backgroundPaint) } ``` 其中,`backgroundPaint` 是一个定义了底部导航栏背景颜色的 `Paint` 对象。 最后,我们需要添加切换逻辑。这可以通过监听底部导航栏的点击事件来实现。例如: ```kotlin override fun onTouchEvent(event: MotionEvent): Boolean { when (event.action) { MotionEvent.ACTION_DOWN -> { // 检查是否点击了某个图标,并执行相应的切换逻辑 } } return super.onTouchEvent(event) } ``` 在上述代码中,我们可以使用 `event.x` 和 `event.y` 来获取用户点击的坐标,然后检查该坐标是否在底部导航栏的某个图标上。如果是,则执行相应的切换逻辑。 完整的代码示例可以参考以下内容: ```kotlin class CustomBottomNavigationBar(context: Context, attrs: AttributeSet) : View(context, attrs) { private val backgroundPaint = Paint().apply { color = Color.WHITE style = Paint.Style.FILL } private val curveHeight = 100f private val path = Path() private var selectedIndex = 0 init { setOnClickListener { performClick() } val iconCount = 4 val iconWidth = width / iconCount path.moveTo(0f, 0f) path.lineTo(0f, height.toFloat()) path.quadTo(width / 2f, height + curveHeight, width.toFloat(), height.toFloat()) path.lineTo(width.toFloat(), 0f) path.close() } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) // 绘制背景 canvas.drawPath(path, backgroundPaint) } override fun onTouchEvent(event: MotionEvent): Boolean { when (event.action) { MotionEvent.ACTION_DOWN -> { val iconCount = 4 val iconWidth = width / iconCount val clickedIndex = (event.x / iconWidth).toInt() if (clickedIndex != selectedIndex) { // 切换到新的图标 selectedIndex = clickedIndex invalidate() } } } return super.onTouchEvent(event) } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值