Button:今夜化了美美的装——shape&ripple(波纹)

来源:互联网 发布:c语言被调用函数的含义 编辑:程序博客网 时间:2024/06/04 20:03

Buuton作为大部分教程首先或者第二位讲解的控件,有着至高无上的作用和需求,学Android的不管是大神或者小白都曾为实现第一个Button的监听事件而欢喜,在实现监听功能的基础上,用户还是需求更高的体验
国际惯例,上官方图
在新版的Api23(Android6.0)或Api24里的Button是这个样子的。
Android6

然后今天想说的是:Android新版本的button都具备了最新的技能
ripple effect(水波纹效果),今天不说怎么实现的,我们来分析一下,怎么使用才优雅。

以前做按钮的样式,一般是用shape加上背景就行了,或者有些时候加上selector来做选中或未选中区分,今天我们要做的是一个带有shape样式的水波纹按钮。效果图:
(这里没上传gif动画,凑合看呗,点击是有很炫的水波纹的)
按钮

现在分析一下紫色框的button,
实现步骤:1.在drawable下新建ripple文件,这里我命名为ripple_alf.xml,内容如下

<?xml version="1.0" encoding="utf-8"?><ripple xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:sizeandroid="http://schemas.android.com/apk/res-auto"    xmlns:solidandroid="http://schemas.android.com/tools"    android:color="#484b4a">    <item>        <shape xmlns:android="http://schemas.android.com/apk/res/android">            <solid android:color="#3ffcf9fc"/>            <corners android:radius="8px"/>            <stroke android:color="#9f32cd" android:width="2px" />        </shape>    </item></ripple>
<!--ripple标签下的color表示波纹颜色--><!--ripple标签下嵌入一个item标签,在item里面加入shape形状-->

2.然后在layout文件里面引用

 <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="选择时间"        android:id="@+id/button2"        android:background="@drawable/ripple_alf"        android:layout_alignParentBottom="true"        android:layout_alignParentStart="true"        android:layout_marginBottom="47dp"        android:layout_alignParentEnd="true" />

完成效果,透明光环水波纹!
当然你可以修改颜色,透明来达到不同的效果

下面就是圆形的这枚家伙了,在这里我的ripple命名为ripple.xml,内容如下:

<?xml version="1.0" encoding="utf-8"?><ripple xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:sizeandroid="http://schemas.android.com/apk/res-auto"    xmlns:solidandroid="http://schemas.android.com/tools"    android:color="#969998">    <item>        <shape xmlns:android="http://schemas.android.com/apk/res/android"            android:shape="oval">            <solid                android:color="#35e679"/>            <size                android:width="12dp"                android:height="12dp"/>        </shape>    </item></ripple>

引用方式还是一样的,下面是动画效果
在res下新建anim文件夹,新建了zoom.xml文件

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false" >    <scale        android:duration="500"        android:fillEnabled="true"        android:fillAfter="true"        android:pivotY="50%"        android:fromXScale="1.0"        android:fromYScale="1.0"        android:pivotX="50%"        android:toXScale="1.02"        android:toYScale="1.02" >    </scale></set>

上面是放大缩小的动画

最后在MainActivity里面引用

public class MainActivity extends AppCompatActivity {    Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button=(Button)findViewById(R.id.time);       button.setOnClickListener(new View.OnClickListener() {           @Override           public void onClick(View view) {               Animation animation= AnimationUtils.loadAnimation(MainActivity.this,R.anim.zoom);           //加载动画               button.clearAnimation();                                     //清理动画               button.startAnimation(animation);                  //传入动画,启动           }       });    }}

好了,绿胖子也完成了,额,说到绿胖子想起来,我们可以用selector来做一些可爱背景的button,这里就不列举了。

我想开发简单的东西,这些按钮已经够用了,毕竟模式是一样的,具体客户的要求还要别的形式实现。

0 0