Android的Drawable

本文详细解析了Drawable的各种类型及其使用方法,包括BitmapDrawable、ShapeDrawable、LayerDrawable等,涵盖了它们的作用、语法及注意事项,如LevelListDrawable的等级设定和TranslationDrawable的淡入淡出效果实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.获取Drawable的内部宽\高:getIntrinsicHeight/Width。但是并不是所有Drawable都有内部宽高(比如说一个颜色形成的宽高,Drawable的宽高不等于大小,大小是根据VIew的大小而定的)

2.BitmapDrawable

作用:引用原始图片,但是附加各种渲染属性。

3.ShapeDrawable

使用:以前有分析过,就不分析了。

注意:<gradient>与<solid>冲突无法同时使用。<stroke>中只要dashWidth和dashGap有一个为0则虚线效果就不生效。

4.LayerDrawable

使用:以前有分析过,就不分析了

5.StateDrawable

使用:以前有分析过,就不分析了

6.LevelListDrawable

作用:当每个等级不同,使用不同的drawable。

语法:

<level-list>
    <item
          android:drawable="@drawable/resource"
          android:maxLevel="integer"
          android:minLevel="integer"/>
</level-list>
View Code

详解:每个<item>中装载drawable并设置最小和最大等级,当它作为View的背景时,通过Drawable的setLevel()方法设置不同等级,当等级在某个<item>的最小最大范围内的时候则选定该<item>并切换drawable(所以说等级最好不好重叠)。作为ImageView的src的时候则使用imageView.setImageLevel()来设置。

注:Drawable的等级时候范围的在0~10000之内

7.TranslationDrawable

作用:可以通过调用startTransition()和reverseTransition()实现两张图片的切换。

使用:实现淡入淡出的效果

<!--只能是两张图,两张图以上的图不显示-->
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/ic_launcher"/>
    <item android:drawable="@mipmap/test1"/>
</transition>
translation_test
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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="com.maikefengchao.circleview.MainActivity">

    <ImageView
        android:id="@+id/main_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/translation_img"/>
</LinearLayout>
activity_main
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIvShow = (ImageView)findViewById(R.id.main_iv);
        TransitionDrawable transition = (TransitionDrawable)mIvShow.getDrawable();
        transition.startTransition(1000);
        //根据测试说明,第二章图以淡入的形式显示,只是覆盖第一张图,第一张图并未消失
    }    
MainActivity

8.InsetDrawable

作用:将其他Drawable内嵌到自己当中,当一个View希望自己的背景比自己的实际区域小的时候。

使用:设置缩小上下左右距离

9.ScaleDrawable

作用:根据等级缩放比例

使用:通过百分比设置scaleHeight/Width,但是同时还需要在java代码中用drawble.setLevel()设置其Level属性,因为drawable的level为0的时候不显示,根据P258页的源码可知。

同时level越大则内部的drawble看起来就越大,level最大等级是10000。

注意:根据自己的Level指定Drawable的缩放比例

10.ClipDrawable

作用:裁剪

使用:以前有

 

 

参考:http://blog.youkuaiyun.com/wode_dream/article/details/38584693

11.自定义Drawable

 ①、继承Drawable  ②、重写抽象方法

 自定义View:http://blog.youkuaiyun.com/lmj623565791/article/details/43752383

转载于:https://www.cnblogs.com/rookiechen/p/5438427.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值