首页 > 其他分享 >android drawable

android drawable

时间:2022-12-14 20:02:28浏览次数:53  
标签:color BitmapDrawable drawable Drawable bitmap android


android.graphics.drawable. Drawable 是个虚类。
它的直接子类有
BitmapDrawable, ClipDrawable, ColorDrawable, DrawableContainer, GradientDrawable, 
InsetDrawable, LayerDrawable, NinePatchDrawable, PictureDrawable, RotateDrawable, 
ScaleDrawable, ShapeDrawable
间接子类有
AnimationDrawable, LevelListDrawable, PaintDrawable, StateListDrawable, TransitionDrawable

Class Overview

​View​​, a Drawable does not have any facility to receive events or otherwise interact with the user.

In addition to simple drawing, Drawable provides a number of generic mechanisms for its client to interact with what is being drawn:

  • The 

​setBounds(Rect)​

  •  method must be called to tell the Drawable where it is drawn and how large it should be. All Drawables should respect the requested size, often simply by scaling their imagery. A client can find the preferred size for some Drawables with the 

​getIntrinsicHeight()​

  •  and 

​getIntrinsicWidth()​

  • methods.这个用来设置Drawable要画好大
  • The 

​getPadding(Rect)​

  • 这个接口用于返回padding,这个padding其实表示的是控件的content的padding

​setState(int[])​​​​setLevel(int)​​​​Drawable.Callback​

  •  interface. All clients should support this interface (via

​setCallback(Drawable.Callback)​

  • ) so that animations will work. A simple way to do this is through the system facilities such as

​setBackgroundDrawable(Drawable)​

  •  and 

​ImageView​

  • .

Drawable定义了一个可画的对象一些接口。

它是抽象类。我们只能实例化它的实现类。Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象。 


draw(Canvas canvas)来画Drawable,
一定要在之前通过
void        setBounds(Rect bounds)
来设置其需要画到的区域。
注意2: 可通过setCallback(Drawable.Callback)来实现动画。


构造函数






直接子类
BitmapDrawable     A Drawable that wraps a bitmap. You can create a BitmapDrawable from a file path, 
                                an input stream, through XML inflation, or from a Bitmap object. 
                            它的本质是个bitmap,它提供了针对bitmap的实现。
注意:Android supports bitmap files in a three formats:  .png (preferred),  .jpg (acceptable),  .gif (discouraged).
也是说Android把.png,.jpg,.gif都是看为BitmapDrawable,Android应该是把这三种图片转为bitmap来处理。
其实任何图片绘画时最后都是转为bitmap来处理
        BitmapDrawable bitmapDrawable=(BitmapDrawable)context.getResources().getDrawable(R.drawable.icon);
        Bitmap bitmap=bitmapDrawable.getBitmap();
可以通过上面的形式把BitmapDrawable转化为Bitmap
ClipDrawable     A Drawable that clips another Drawable based on this Drawable's current level value. 
具体参照《 ​​ClipDrawable​​》
ColorDrawable     A specialized Drawable that fills the Canvas with a specified color, with respect to the clip region. 
DrawableContainer
    它应该是专门用来存放Drawable的。
    如何直接使用它还不知道。它不能在XML文件中定义。
    它的子类AnimationDrawable, LevelListDrawable, StateListDrawable        
GradientDrawable     A Drawable with a color gradient for buttons, backgrounds, etc.
    It can be defined in an XML file with the <shape> element. For more information。 
InsetDrawable     A Drawable that insets another Drawable by a specified distance. 
LayerDrawable     A Drawable that manages an array of other Drawables. 
NinePatchDrawable     A resizeable bitmap, with stretchable areas that you define. 
它对应的是.9.png文件。关于此请参考《 ​​个性缩放图片NinePatchDrawable​》


PictureDrawable     Drawable subclass that wraps a Picture, allowing the picture to be used whereever a Drawable is supported. 
RotateDrawable     
A Drawable that can rotate another Drawable based on the current level value. 
ScaleDrawable     A Drawable that changes the size of another Drawable based on its current level value. 
ShapeDrawable     A Drawable object that draws primitive shapes. 关于此请参考《 ​​ShapeDrawable​》


ColorDrawable
    A specialized Drawable that fills the Canvas with a specified color, 
    with respect to the clip region. Note that a ColorDrawable ignores the ColorFilter. 
    It also ignores the Bounds, meaning it will draw everywhere in the current clip, 
    even if setBounds(...) was called with a smaller area.
    It can be defined in an XML file with the <color> element.
注意1:它会忽略掉ColorFilter和Bounds,它会把整个clip区域都填充成指定的颜色。
在XML中定义和引用ColorDrawable
A color resource can also be used as a drawable in XML. For example,
when creating a state list drawable, you can reference a color resource for the android:drawable attribute 
(android:drawable="@color/green
在values下的 Colors.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
< color  name="red">#FFFF0000</color>
</resources>
像引用Drawable一样引用它
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:background=" @color/red "
    />

标签:color,BitmapDrawable,drawable,Drawable,bitmap,android
From: https://blog.51cto.com/u_13657808/5938299

相关文章

  • ShapeDrawable做放大镜效果
    引用一下ShapeDrawable的类的说明: Class Overview    A Drawable object that draws primitive shapes. A ShapeDrawable takes a Shape object a......
  • android ndk之hello world
    前言:AndroidNDKr7及以上的版本已经集成了Cygwin编译环境,也就是说,我们完全可以抛弃庞大的Cygwin了。r6及以下版本,也可以抛弃几个G的完整版,使用精简过的Mini-Cygwin来编译,解......
  • Android Studio Gradle编译输出信息乱码
    有时候AndroidStudioGradle编译时输出的信息是乱码,这是因为输出信息和信息的窗口不统一。首先,我们先修改设置里的文件编码Settings->Editor->FileEncodings打开都改......
  • Android 10 开启WiFi后出现弹窗改为出现一次,后面不关闭WiFi,不要弹出提示
    \frameworks\opt\net\wifi\service\java\com\android\server\wifi\AvailableNetworkNotifier.javapackagecom.android.server.wifi;importstaticcom.android.server.......
  • Android11 实现侧键打印走纸
    /frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.javaelseif(keyCode==KeyEvent.KEYCODE_F12){      //XCSWw......
  • Android11 双击亮屏 上层修改
    packagecom.android.systemui.qs.tiles;importandroid.content.BroadcastReceiver;importandroid.content.ComponentName;importandroid.content.Context;import......
  • Android 移动应用性能优化 之 友盟
    移动应用性能优化之友盟​​前言​​​​正文​​​​一、问题​​​​1.ANR​​​​2.NullPointerException​​​​二、友盟使用​​​​1.创建平台应用​​​​2.使......
  • Android 天气APP(七)城市切换 之 城市数据源
    9.城市选择既然是城市切换,那么首先得有城市的数据,数据来源有两种,本地和网络,但是网络数据对手机的网络要求比较高,看起来会延迟很大,所以这里我用本地的数据。也是从网络上找......
  • Android 12 平台 [FAQ28467]如何在vendor下建立目录
    [DESCRIPTION]有客户需求在vendor下建立目录。[SOLUTION]这是一个示例:在alps-mp-s0.mp1-V8.26版本以在vendor下创建dummy举例,主要是在创建vendor.img的过程中创建dummy文件......
  • Android 手势导航核心实现
    一、如何找到入口Android10推出了全新的手势导航功能,原生的Android系统就提供了此功能,根据这个切入点查询相关实现,Android10和11的源码里面,在SystemUI模块里面可以找到......