Android Drawable Resource学习(二)、BitmapDrawable和Bitmap

一、如何创建一个BitmapDrawable对象

Bitmap,代表一个位图图像,Android支持三种格式的位图图像:.png (preferred),.jpg (acceptable), .gif (discouraged)。

括号里的说明,代表这三种格式的图片在Android中的支持情况,.png格式图片优先,.jpg格式也可以,但是效果没有.png好,.gif支持最差。

可以直接使用图片的名称作为资源ID,来直接引用一个位图图片。也可以再XML文件中创建一个资源别名的ID。

在构建应用的时候,Bitmap文件可能会被appt工具压缩自动优化为无损图像。例如,一个真彩色PNG,不需要超过256的颜色可以被转换成一个8位PNG和调色板。这将导致一个图像质量相同,但这需要更少的内存。所以要意识到,在drawable目录中图像的二进制文件在构建程序时可以改变。如果你打算读一个图像作为字节流并将它转换成一个位图,把你的图片放在在res /raw/文件夹里,在那里他们不会被优化。

1、通过Bitmap File

一个bitmap文件就是一个.png、.jpg,.gif格式的文件。Android会对存储在res/drawable/目录下的这些文件创建一个Drawable资源。

文件位置:

       res/drawable/filename.png (.png, .jpg, or .gif)    文件名即资源的ID。

编译资源数据类型:
             指向BitmapDrawable类型的指针。
资源引用:

In Java: R.drawable.filename
In XML: @[package:]drawable/filename
示例:
有这样存储的图片 res/drawable/myimage.png,在layout xml文件中将他显示在视图上。
<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/myimage" />

在java代码中检索出为一个Drawable对象。

            
        Resources res=getResources();
        Drawable drawable = res.getDrawable(R.drawable.myimage);
        //实际上这是一个BitmapDrawable对象
        BitmapDrawable bitmapDrawable=(BitmapDrawable)drawable;
        //可以在调用getBitmap方法,得到这个位图
        Bitmap bitmap=bitmapDrawable.getBitmap();

参考:
  • 2D Graphics
  • BitmapDrawable

2、通过XML Bitmap

一个XML bitmap是一个在XML文件中定义的指向一个bitmap文件的资源。其效果是作为一个原始位图文件的别名,并且可以指定一些额外的属性。

注意:你可以在<item>节点中使用<bitmap>作为它的子节点。比如,当你定义一个state list或者layer list的时候,可以包括一个 android:drawable属性

Note: You can use a <bitmap> element as a child of an<item> element. Forexample, when creating astate list orlayer list,you can exclude the android:drawableattribute from an<item> element and nest a<bitmap> inside it that defines the drawable item.

文件位置:
res/drawable/filename.xml
filename作为资源的ID
编译资源类型
指向BitmapDrawable类型的指针
资源引用
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
语法:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    android:antialias=["true" | "false"]
    android:dither=["true" | "false"]
    android:filter=["true" | "false"]
    android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                      "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                      "center" | "fill" | "clip_vertical" | "clip_horizontal"]
    android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
节点介绍:
<bitmap>
定义位图的来源和属性

属性:

xmlns:android
类型:String。定义了XML的命名空间,必须是"http://schemas.android.com/apk/res/android"。如果<bitmap>是根元素,那么他是必须的,如果是嵌套在<itme>里面,那么就不是必须的。
android:src
类型:Drawable resource。必需。 引用一个drawableresource.
android:antialias
类型:Boolean。是否开启抗锯齿。
android:dither
类型:Boolean。如果位图与屏幕的像素配置不同时,是否允许抖动.(例如:一个位图的像素设置是 ARGB 8888,但屏幕的设置是RGB 565)
android:filter
类型:Boolean。是否允许对位图进行滤波。对位图进行收缩或者延展使用滤波可以获得平滑的外观效果。
android:gravity
类型:关键字。定义位图的重力(gravity),如果位图小于其容器,使用重力指明在何处绘制

必需是下面的属性,多个之间用  |  分隔

ValueDescription
topPut the object at the top of its container, not changing its size.
bottomPut the object at the bottom of its container, not changing its size.
leftPut the object at the left edge of its container, not changing its size.
rightPut the object at the right edge of its container, not changing its size.
center_verticalPlace object in the vertical center of its container, not changing its size.
fill_verticalGrow the vertical size of the object if needed so it completely fills its container.
center_horizontalPlace object in the horizontal center of its container, not changing its size.
fill_horizontalGrow the horizontal size of the object if needed so it completely fills its container.
centerPlace the object in the center of its container in both the vertical and horizontal axis, notchanging its size.
fillGrow the horizontal and vertical size of the object if needed so it completely fills itscontainer. This is the default.
clip_verticalAdditional option that can be set to have the top and/or bottom edges of the child clipped toits container's bounds. The clip is based on the vertical gravity: a top gravity clips thebottom edge, a bottom gravity clips the top edge, and neither clips both edges.
clip_horizontalAdditional option that can be set to have the left and/or right edges of the child clipped toits container's bounds. The clip is based on the horizontal gravity: a left gravity clipsthe right edge, a right gravity clips the left edge, and neither clips both edges.
android:tileMode
类型:Keyword
定义了tile模式。当tile模式被启用,位图是重复的,并且gravity属性将被忽略。

必须是下列之一常量值:
ValueDescription
disabledDo not tile the bitmap. This is the default value.
clampReplicates the edge color if the shader draws outside of its original bounds
repeatRepeats the shader's image horizontally and vertically.
mirrorRepeats the shader's image horizontally and vertically, alternating mirror images so thatadjacent images always seam.
示例:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/icon"
    android:tileMode="repeat" />
参考:
  • BitmapDrawable
  • Creatingalias resources

二、BitmapDrawable的使用

一个BitmapDrawable就是封装了一个位图。直接以文件的方式,就是封装了一个原始的位图。以Xml方式,可以对原始的位图进行一系列的处理,比如说抗锯齿,拉伸,对齐等等。

要了解BitmapDrawable的使用,还需要明白Bitmap、BitmapFactory等类。Bitmap代表了一个原始的位图,并且可以对位图进行一系列的变换操作。BitmapFactory提供一系列的方法用于产生一个Bitmap对象。多用在Canvas中。

关于绘图和位图变换以后再学习。BitmapDrawable的使用比较简单,就是在其他的xml文件中直接引用就可以了,不过要注意在xml中定义BitmapDrawable各个属性使用和含义。






  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在MATLAB中,NURBS(非均匀有理B样条)是一种强大的数学工具,用于表示和处理复杂的曲线和曲面。NURBS在计算机图形学、CAD(计算机辅助设计)、CAM(计算机辅助制造)等领域有着广泛的应用。下面将详细探讨MATLAB中NURBS的绘制方法以及相关知识点。 我们需要理解NURBS的基本概念。NURBS是B样条(B-Spline)的一种扩展,其特殊之处在于引入了权重因子,使得曲线和曲面可以在不均匀的参数空间中进行平滑插值。这种灵活性使得NURBS在处理非均匀数据时尤为有效。 在MATLAB中,可以使用`nurbs`函数创建NURBS对象,它接受控制点、权值、 knot向量等参数。控制点定义了NURBS曲线的基本形状,而knot向量决定了曲线的平滑度和分布。权值则影响曲线通过控制点的方式,大的权值会使曲线更靠近该点。 例如,我们可以使用以下代码创建一个简单的NURBS曲线: ```matlab % 定义控制点 controlPoints = [1 1; 2 2; 3 1; 4 2]; % 定义knot向量 knotVector = [0 0 0 1 1 1]; % 定义权值(默认为1,如果未指定) weights = ones(size(controlPoints,1),1); % 创建NURBS对象 nurbsObj = nurbs(controlPoints, weights, knotVector); ``` 然后,我们可以用`plot`函数来绘制NURBS曲线: ```matlab plot(nurbsObj); grid on; ``` `data_example.mat`可能包含了一个示例的NURBS数据集,其中可能包含了控制点坐标、权值和knot向量。我们可以通过加载这个数据文件来进一步研究NURBS的绘制: ```matlab load('data_example.mat'); % 加载数据 nurbsData = struct2cell(data_example); % 转换为cell数组 % 解析数据 controlPoints = nurbsData{1}; weights = nurbsData{2}; knotVector = nurbsData{3}; % 创建并绘制NURBS曲线 nurbsObj = nurbs(controlPoints, weights, knotVector); plot(nurbsObj); grid on; ``` MATLAB还提供了其他与NURBS相关的函数,如`evalnurbs`用于评估NURBS曲线上的点,`isoparm`用于生成NURBS曲面上的等参线,以及`isocurve`用于在NURBS曲面上提取特定参数值的曲线。这些工具对于分析和操作NURBS对象非常有用。 MATLAB中的NURBS功能允许用户方便地创建、编辑和可视化复杂的曲线和曲面。通过对控制点、knot向量和权值的调整,可以精确地控制NURBS的形状和行为,从而满足各种工程和设计需求。通过深入理解和熟练掌握这些工具,可以在MATLAB环境中实现高效的NURBS建模和分析。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值