Android Drawable知识学习

1. Drawable

Drawable表示一种可以在Canvas上进行绘制抽象的概念,种类很多,最常见的颜色和图片都可以是一个Drawable

Drawable的优点:

  • 使用简单,比自定义View要简单一些
  • 非图片类型的Drawable占用空间小,对apk的体积也会有点改善

Drawable虽有很多种,但都表示一种图像的概念,但又不仅仅都是图片,也可以通过颜色构造出各式各样的图像的效果。Drawable往往会被用作View的背景,一般是通过xml来定义,作为一个基类,Drawable有20多个子类

Drawable的内部宽和高,通过getIntrinsicWidthgetIntrinsicHeight这两个方法可以获取,这两个参数比较重要。但并不是所有的Drawable都有内部宽和高。图片形成的Drawable内部宽高就是图片的宽和高,一个颜色所形成的Drawable没有宽和高。

注意:
Drawbale的内部宽高并不等同于它的大小,一般来说,Drawable没有大小的概念,当被用作View的背景时,Drawable会被拉伸至View的等同大小


2. Drawable的分类

分类有很多,但常用的也不是很多,使用也都不算难,在xml中指定属性就可以实现一些看起来炫酷的效果


2.1 BitmapDrawable

使用蛮方便,只需要在xml文件中设置想要的属性,在activity布局文件View控件,使用android:background引用

简单使用:

BitmapDrawablebitmapdrawable.xml文件

注‘Android技术交流群878873098,欢迎大家加入交流,畅谈!本群有免费学习资料视频且免费分享

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:antialias="true"
    android:src="@drawable/ct"
    android:dither="true"
    android:filter="true"
    android:gravity="center"
    android:mipMap="false"
    android:tileMode="mirror" />

MaintActivity的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/iv_main_activity"
        android:background="@drawable/bitmapdrawable"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

BitmapDrawble

其中有个属性android:tileMode="mirror",最终的效果和BitmapShaderTileMode.MIRROR 的镜像效果感觉一样


  • android:antialas

是否开启抗锯齿。开启后,图片变得平滑,但会降低一点点图片的清晰度

  • android:dither

是否开启图像抖动。当图片的像素配置和手机屏幕的像素配置不一致时,设置为true,高质量的图片在低像素的手机屏幕上也能比较好的显示。

  • android:filter

是否开启过滤效果。当图片的尺寸被拉伸时,开启过滤效果,可以保持较好的显示比例效果

  • android:mipMap

纹理映射。默认设置为false

  • android:tileMode

平铺模式,有4个值。
disabled,默认值,表示关闭平铺模式。开启平铺效果后,gravity属性会无效
repeat,水平方向和竖直方向上的平铺效果
mirror,水平和竖直方向上镜像
clamp,图片四周的像素会被拉伸到整个区域

注‘Android技术交流群878873098,欢迎大家加入交流,畅谈!本群有免费学习资料视频且免费分享


2.2 ShapeDrawable

通过颜色来构造图形,可以为纯色,也可以为渐变效果的图形

简单使用:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="5dp" />
    <solid android:color="@color/colorAccent" />
    <gradient
        android:angle="90"
        android:centerColor="@android:color/white"
        android:endColor="@android:color/holo_orange_light"
        android:gradientRadius="10dp"
        android:startColor="@color/colorAccent"
        android:type="linear"
        android:useLevel="false" />
    <stroke
        android:width="5dp"
        android:color="@color/colorPrimary"
        android:dashGap="1dp"
        android:dashWidth="2dp" />
    <size
        android:width="200dp"
        android:height="200dp" />
</shape>

ShapeDrawabe

  • android:shape

指定图形的形状。rectangle 矩形,oval椭圆,line线,ring圆环。linering需要<stroke>来指定线的宽度和颜色


<corners> 标签

表示矩形shape的四个角,用来控制圆角的角度

  • android:radius 同时为四个角设置圆角的角度,优先级低,会被其他的属性覆盖
  • android:topLeftRadius 左上角
  • android:topRightRadius 右上角
  • android:bottomLeftRadius 左下角
  • android:bottomRightRadius 右下角

<solid> 标签

纯色填充,指定shape填充的颜色


<gradient> 标签

<solid>标签互相排斥,表示渐变的效果

  • android:angle 渐变的角度。默认为0,有效值必须为45°的倍数;0°表示从左到右,90°从下向上。有种顺时针旋转的感觉

  • android:startColor 渐变起始颜色

  • android:centerColor 渐变中间颜色

  • android:endColor 渐变终止颜色

  • android:type 渐变类型,linear线性,radial径向渐变,sweep扫描

  • android:gradientRadius 渐变半径 android:type= radial有效

  • android:useLevel 一般为false,当Drawable作为StateListDrawabletrue

  • android:centerX 渐变中心点x坐标

  • android:centerY 渐变中心点y坐标


<stroke> 标签

Shape的描边

  • android:width 描边的厚度
  • android:color 描边的颜色
  • android:dashWidth 组成虚线的线段长度,每个小线段的长度
  • android:dashGap 虚线线段的间隔

<size> 标签

shape的大小,但并不是shape最终的显示大小,shape会自适应View的宽高。DrawablegetIntrinsicWidth()getIntrinsicHeight()方法,可以的带Drawable的固定宽高,对于一些图片来说,固定的宽高就是图片的宽高。但对于Shape来说,默认没有宽高,这两个方法会返回-1,若<size>标签设置了,Shape也就有了固有宽高。但,作为View的背景时,shape还是会被拉伸或者压缩到View的大小


<padding> 标签

留白,作用于View,是View的留白

注‘Android技术交流群878873098,欢迎大家加入交流,畅谈!本群有免费学习资料视频且免费分享


2.3 LayerDrawable

LayerDrawable对应的XML标签为<layer-list>,是一种层次化的Drawable集合通过不同层次的Drawable叠加而成的效果

简单使用:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#0ac39e" />
        </shape>
    </item>
    <item android:bottom="7dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
        </shape>
    </item>
    <item
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp">
       <shape android:shape="rectangle">
           <solid android:color="@android:color/white"/>
       </shape>
    </item>
</layer-list>

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/layerdrawable"
        android:hint="请输入"
        android:padding="5dp"
        android:textColorHint="@color/colorAccent" />
</RelativeLayout>

LayerDrawable

类似微信的输入框,是用3层叠加出来的效果,也可以通过使用Path绘制

top,bottom,left,right是相对于View的偏移量


2.4 StateListDrawable

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

简单使用:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 点击时 -->
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@android:color/white"/>
            <stroke android:width="1dp" android:color="@color/colorAccent" />
            <corners android:radius="4dp" />
        </shape>
    </item>
    <!-- 松开时 -->
    <item android:state_pressed="false">
        <shape>
            <solid android:color="@android:color/white"/>
            <stroke android:width="1dp" android:color="@color/colorPrimary" />
            <corners android:radius="4dp" />
        </shape>
    </item>
    <!-- 默认 -->
    <item >
        <shape>
            <solid android:color="@android:color/white"/>
            <stroke android:width="1dp" android:color="@color/colorPrimary" />
            <corners android:radius="4dp" />
        </shape>
    </item>

</selector>

StateListDrawable点击

点击时,Button边缘为红色,默认和松开为蓝色,一般默认会不加状态,放在最后


  • android:constantSize StateListDrawable固有大小,是否随着状态的改变而大小改变

常见的状态

状态 含义
android:state_pressed 按下的状态
android:state_focused View是否获得焦点
android:state_selected 是否选择了View
android:state_checked 是否选中,一般适用于CheckBox这些
android:state_enabled View当前处于可用状态

Material Design中自带的水波纹效果感觉很好看,可是,都推出这么久了,并不是每个应用都遵循

注‘Android技术交流群878873098,欢迎大家加入交流,畅谈!本群有免费学习资料视频且免费分享


2.5 TransitionDrawable

对应于transition标签,可用于实现两个Drawable之间的淡入淡出效果

简单使用:

transitiondrawable.xml文件

<transition xmlns:android="http://schemas.android.com/apk/res/android"

    android:opacity="translucent">

    <item  android:drawable="@color/colorAccent" />

    <item  android:drawable="@color/colorPrimary"/>

</transition>

MainActivity代码:

private void init() {
    ImageView iv = (ImageView) findViewById(R.id.iv_main_activity);
    TransitionDrawable transitionDrawable = (TransitionDrawable) ContextCompat.getDrawable(MainActivity.this,R.drawable.transitiondrawable);
    iv.setImageDrawable(transitionDrawable);
    transitionDrawable.startTransition(1000);
}

init()方法中,getResources().getDrawable(int id)方法过时,就使用了ContextCompat.getDrawable()方法来替代

效果就是在1秒内,由红色慢慢变成蓝色

如果使用动画,可能造成转换效果生硬,尤其是图片显示出来后,再开启动画,会造成图片闪烁的感觉


3.最后

个人认为的几种常见的Drawable学习

本人很菜,有错误,请指出

话说,今天花大价钱买了两本书,一本是《深入理解计算机操作系统》,也不晓得能不能看懂,哈哈

共勉 : )

注‘Android技术交流群878873098,欢迎大家加入交流,畅谈!本群有免费学习资料视频且免费分享

作者:英勇青铜5
链接:https://www.jianshu.com/p/0d85e4d90e3c
來源:简书

猜你喜欢

转载自blog.csdn.net/qq_43093708/article/details/86706318