第六章:Android的Drawable

  Drawable表示的是一种可以在Canvas上进行绘制的抽象的概念.

  6.1 Drawable简介

      Drawable常被用来作为View的背景使用.

      Drawable一般都是通过XML来定义的.

      Drawable是一个抽象类,它是所有Drawable对象的基类,每个具体的Drawable都是它的子类.

      Drawable的内部宽/高这个参数比较重要,通过getIntrinsicWidth和getIntrinsicHeight这两个方法可以获取到它们.

  6.2 Drawable的分类

      6.2.1 BitmapDrawable

        表示的就是一张照片<bitmap/>

      6.2.2 ShapeDrawable

        可以理解为通过颜色来构造的图形,它既可以是纯色的图形,也可以是具有渐变效果的图形.

        <shape>标签,子属性android:shape="四个选项,rectangle,oval,line,ring"

        子标签

          <coners>,只适用于矩形shape,这里的角度指圆角的程度,用px表示.

          <gradient>,与<solid>标签互斥,其中solid表示纯色填充,二gradient则表示渐变效果

          <solid>,纯色填充,通过android:color即可指定shape中填充的颜色

          <stroke>,Shape的描边

          <padding>,表示空白,但是它表示的不是shape的空白,而是包含它的View的空白

          <size>,shape 的大小

      6.2.3 LayerDrawable

          对应的XML标签是<layer-list>,它表示一种层次化的Drawable集合,通过将不同的Drawable纺织在不同的层上面从而达到一种叠加后的效果.

          一个layer-list中可以包含多个item,每个item表示一个Drawable

      6.2.4 StateListDrawable

          对应的XML标签是<selector>,也是表示Drawable集合,每个Drawable都对应着View的一种状态.

          常见状态:android:state_pressed/state_focused/state_selected/state_checked/state_enabled.

      6.2.5 LevelListDrawable

          对应的XML标签是<level-list>,每个Drawable都有一个等级(levle)的概念.

          根据不同的等级,LevelListDrawable会切换为对应的Drawable

      6.2.6 TransitionDrawable

          对应的XML标签是<transition>,它用于实现两个Drawable之间的淡入淡出效果

      6.2.7 InsetDrawable

          对应的XML标签是<inset>,它可以将其他Drawable内嵌到自己当中,并可以在四周留出一定的间隔.

      6.2.8 ScaleDrawable

           对应的XML标签是<scale>,它可以根据自己的等级(level)将指定的Drawable缩放到一定的比例

      6.2.9 ClipDrawable

          对应的XML标签是<clip>,他可以根据自己当前的等级(level)来裁减另一个Drawable.

    6.3 自定义Drawable

      Drawable的工作原理简单,其核心就是draw方法.

      系统会调用Drawable的draw方法来绘制view的背景,可以通过重写Drawable的draw方法来自定义Drawable

      draw,setAlpha,setColorFilter和getOpacity是必须重载实现的方法.

      

转载于:https://www.cnblogs.com/JingQ/p/6184321.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.example.myapplication.GameView android:id="@+id/gameView" android:layout_width="match_parent" android:layout_height="match_parent"/> <TextView android:id="@+id/score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:paddingTop="10dp" android:text="0" android:textColor="@color/black" android:textSize="18dp" /> <RelativeLayout android:visibility="gone" android:id="@+id/relative" android:layout_width="300dp" android:layout_height="200dp" android:layout_centerInParent="true" android:background="@drawable/introdution" > <TextView android:id="@+id/scoreText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:backgroundTint="@color/black" android:text="分数:" android:textSize="18dp" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:backgroundTint="#0068B5" android:text="重新开始" android:textSize="18dp" /> </RelativeLayout> <RelativeLayout android:id="@+id/introdution" android:layout_width="300dp" android:layout_height="200dp" android:layout_centerInParent="true" android:background="@drawable/shuoming"> <Button android:id="@+id/btn_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:backgroundTint="#0068B6" android:text="开始游戏" android:textSize="18dp" /> </RelativeLayout> </RelativeLayout>解释这段布局文件代码
06-09

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值