Android第五天

  • 简单了解Button,并为之简单配置一些自定义属性(形状,不同状态下的颜色)
  • 关键帧动画
  • 补间动画
    一.Button的使用
    1.在xml中添加一个Button
    宽高为包裹内容,内容为登录,字体颜色为红色,字体大小为25sp。
 

2.给这个Button添加一个点击事件
最简单的一种
在xml中直接添加onClick属性,并设置名称为login

 

在对应MainActivity添加对应login方法
public void login(View view) {
System.out.println("点击了");
}
按钮的点击事件 通常接收一个参数:View
当按钮被点击,系统接收这个事件并把这个事件回调给监听者
把当前被点击的按钮作为参数传递过去,使用的时候 必须转化为对应的类型
第二种方式,通过代码实现
先给该Button添加一个ID,在MainActivity找到对应的ID来找到这个Button。

android:id="@+id/bt_login"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Edit"
       android:textColor="#ff0000"
       android:textSize="25sp"
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       Button  btn = findViewById(R.id.bt_login);

给按钮添加点击事件
btn.setOnClickListener(this);
该MainActivity来监听,传参数this,并在MainActivity中实现OnClickListener这个接口。

 public void onClick(View view)
    {
        System.out.println("点击了");
    }

精简一下代码,不用参数this,直接创建匿名对象

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("点击了");
            }
        });

或者用Lamda表达式
btn.setOnClickListener(view -> System.out.println("点击了"));

二.配置自定义属性,形状,颜色等
1.配置颜色
当我们在xml中添加了一个Button,不按时字体默认为黑色,当按下时显示另一种颜色。
首先在左边选中drawable,单击右键新建一个Drawable resource file,名称为text_color。
创建好后在其中的selector中添加一个item,选择state_pressed选项,设置颜色为红色



    

默认状态下为黑色




    



配置是有先后顺序的
默认的状态必须放在后面
如果放在前面,优先匹配
一旦匹配上了 后面的设置就无效

还可以设置其无法点击状态时的颜色

 

    


    



在xml中使用
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="分享"
android:textColor="@drawable/text_color"
/>
配置控件的颜色用android:color
也可以设置为一张图片,把这张图片放在drawable中,用android:drawable配置
2.配置控件的形状
同样的,在左边选中drawable,单击右键新建一个Drawable resource file,名称为rectangle。
创建完毕,将selector改为shape,shape中有四个选项:
oval(椭圆形),rectangle(长方形),line(线型),ring(环形)
选择rectangle,其中有一些属性:
corners: 圆角的大小
solid: 填充颜色
stoke: 边框颜色 宽度
gradient: 渐变色 UI-拟物化
padding: 内间距
现在将长方形的边框颜色设为红色,宽度设为1dp


   

在xml中使用

 

还可以用rectangle中的corners属性改变圆角,使得长方形变成一个圆


   
    

这样在使用的时候就是一个圆形了
shape和selector嵌套




    
        
            
        
    


    
        
            
            
        
    


一个item对应一个状态

三.实现火焰燃烧的一个关键帧动画
关键帧动画 FrameAnimation
使用多张图片快速切换 形成一种动画
1.xml实现
首先将实现动画的图片资源拷到drawable下,然后选中drawable,单击右键新建一个Drawable resource file,名称为fire_anim。
创建好后,将selector改为animation-list,其中
一个item对应一帧:一张图片
drawable:配置动画的图片
duration:配置这张图片在整个动画中播放的时间 单位毫秒


    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

animation-list下oneshot属性:是否只执行一遍,设为false将一直循环执行。
在xml中添加ImageView

 

这样它只会显示第一张图片,却不会播放动画。用点击操作它播放动画。
为这个控件加一个ID

 

在MainActivity中添加OnTouchEvent事件

  public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
        
        }
        return true;
    }

开启动画

  public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
             // 1.获取动画的控件
            ImageView iv = findViewById(R.id.iv_anim);

            // 2.通过控件获取动画
            AnimationDrawable anim =(AnimationDrawable) iv.getDrawable();

            // 3.启动动画
            anim.start();
        }
        return true;
    }

2.代码实现
在xml显示第一张图片

 

在MainActivity中创建一个整型数组来保存图片资源

  int[] resID = {R.drawable.campfire01,R.drawable.campfire02,R.drawable.campfire03,R.drawable.campfire04,
                R.drawable.campfire05,R.drawable.campfire06,R.drawable.campfire07,R.drawable.campfire08,R.drawable.campfire09,
                R.drawable.campfire10,R.drawable.campfire11,R.drawable.campfire12,R.drawable.campfire13,R.drawable.campfire14,
                R.drawable.campfire15,R.drawable.campfire16,R.drawable.campfire17};

启动动画

  // 1.创建一个动画资源
        AnimationDrawable anima = new AnimationDrawable();

        for(int id:resID){
        // 2.添加每一帧的动画
        anima.addFrame(getResources().getDrawable(id),100);
        }

        // 3.使用这个资源
        ImageView iv = findViewById(R.id.anima);
        iv.setImageDrawable(anima);

        // 4.启动动画
        anima.start();

四.补间动画
关键帧动画:配置了动画的每一帧
res-drawable-xxx.xml
补间动画:只关心开始和结束两个状态 中间的动画由系统自动补全
res -> anim -> xxx.xml

首先在左边选择res(在project目录下),单机右键新建一个Directory,名称为anim(固定的)。然后选中anim,新建一个Animation resource file,名称为translate。
创建好后,在set下有几个选项:
平移:translate
缩放:scale
旋转:rotate
透明:alpha
选择translate


    

其中duration设置动画时间为1秒,
fillAfter设置保持动画之后的状态

xml中创建一个控件来实现该动画

 

点一下屏幕让动画开始
在MainActivity中添加OnTouchEvent事件

public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            // 1.找到动画的控件
            View v = findViewById(R.id.v_anim);

            // 2.加载xml动画文件 ->得到动画
            Animation translate = AnimationUtils.loadAnimation(this,R.anim.translate);

            translate.setInterpolator(new BounceInterpolator())
            // 3.将这个动画作用到这个控件上去
            v.startAnimation(translate);

        }
        return true;
    }

用 translate.setInterpolator(new BounceInterpolator());语句为动画设置了一个回弹效果。

用代码创建一个动画

 public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            // 1.找到动画的控件
            View v = findViewById(R.id.v_anim);

            // 2.创建动画
            /*
            TranslateAnimation
            AlphaAnimation
            ScaleAnimation
            RotateAnimation
             */
            AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
            alphaAnimation.setDuration(1000);
            alphaAnimation.setFillAfter(true);

            RotateAnimation rotateAnimation = new RotateAnimation(0,360,(float)(0.5*v.getWidth()),(float)(0.5*v.getHeight()));
        

            // 3.将这个动画作用到这个控件上去
              v.startAnimation(alphaAnimation);

          
        }
        return true;
    }

注意:补间动画只是一个效果,没有真正改变属性的值。

你可能感兴趣的:(Android第五天)