Drawable日常使用简识(1)----BitmapDrawable的基本使用

       Drawable是对可绘制资源的一种抽象,他和view不同,它不具有可交互性。在我们的项目结构中,通常在res下面有一个名为drawable的文件夹,里面的资源我们是可以通过以下两种方式获取:

(1)R.drawable.xxx

(2)getResources().getDrawable(R.drawable.xxx)

      接下来我就会对常用的Drawable(本身是一个抽象类)子类的使用做一个简单的介绍,今天主要介绍一下BitmapDrawable,在看之前也可以看一下我的另一篇关于Bitmap(位图)和Drawable之间相互转换的博客

1,BitmapDrawable

之前也说了,Drawable是对可绘制资源的一种封装,这里很显然是对Bitmap(位图)的封装,我们可以看一看BitmapDrawable的API,官方给出的解释:

A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object

一个封装了位图的可以设置平铺方式和对齐方式以及可以拉伸的Drawable对象,你可以通过一个文件路径,输入流,xml文件或者是一个位图对象构造一个BItmapDrawable对象;

可想而知,其有四个构造方法:

BitmapDrawable()
           
BitmapDrawable(Bitmap bitmap)
           
BitmapDrawable(InputStream is)
           
BitmapDrawable(String filepath)

内部主要的接口方法有:

 void draw(Canvas canvas)
          Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).
 Bitmap getBitmap()
           
 int getChangingConfigurations()
          Return a mask of the configuration parameters for which this drawable mau change, requiring that it be re-created.
 Drawable.ConstantState getConstantState()
           
 int getGravity()
          Get the gravity used to position/stretch the bitmap within its bounds.
 int getIntrinsicHeight()
          Return the intrinsic height of the underlying drawable object.
 int getIntrinsicWidth()
          Return the intrinsic width of the underlying drawable object.
 int getOpacity()
          Return the opacity/transparency of this Drawable.
 Paint getPaint()
           
 Shader.TileMode getTileModeX()
           
 Shader.TileMode getTileModeY()
           
 void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
           
 Drawable mutate()
          A mutable BitmapDrawable still shares its Bitmap with any other Drawable that comes from the same resource.
 void setAlpha(int alpha)
          Specify an alpha value for the drawable. 0 means fully transparent, and 255 means fully opaque.
 void setAntiAlias(boolean aa)
           
 void setColorFilter(ColorFilter cf)
          Specify an optional colorFilter for the drawable.
 void setDensityScale(Canvas canvas)
          Set the density scale at which this drawable will be rendered.
 void setDensityScale(DisplayMetrics metrics)
          Set the density scale at which this drawable will be rendered.
 void setDensityScale(float density)
          Set the density scale at which this drawable will be rendered.
 void setDither(boolean dither)
          Set to true to have the drawable dither its colors when drawn to a device with fewer than 8-bits per color component.
 void setFilterBitmap(boolean filter)
          Set to true to have the drawable filter its bitmap when scaled or rotated (for drawables that use bitmaps).
 void setGravity(int gravity)
          Set the gravity used to position/stretch the bitmap within its bounds.
 void setTileModeX(Shader.TileMode mode)
           
 void setTileModeXY(Shader.TileMode xmode, Shader.TileMode ymode)
           
 void setTileModeY(Shader.TileMode mode)
           

其中我们经常需要使用到的也不多,接下来会在使用中简单介绍。还有就是要说一下,在设置平铺方式的时候我们使用了枚举类型Shader.TileMode,其主要有以下三个值可选:

CLAMP
          replicate the edge color if the shader draws outside of its original bounds

          如果着色器绘制到其原始边界之外,则复制边缘颜色

MIRROR
          repeat the shader's image horizontally and vertically, alternating mirror images so that adjacent images always seam

          重复水平和垂直的着色器图像,交替镜像,以便相邻图像总是接缝

REPEAT
          repeat the shader's image horizontally and vertically

          水平和垂直重复着色器的图像

2,简单使用

第一步:准备一张图片,小一点的,这里推荐一个不错的关于icon的网站;然后点击下载我们可爱的小狗图片

第二步:编写构建BitmapDrawable的xml文件(在drawable目录下):

bitmap_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap  xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/doggie"
    android:tileModeX="mirror"
    android:tileModeY="repeat"
    android:antialias="true"
    android:alpha="2"
    android:dither="true"
    >
</bitmap>

第三步:编写测试代码

BitmapDrawableActivity:

package com.hfut.operationdrawable;

import android.graphics.Bitmap;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * @author why
 * @date 2018-8-13 17:17:18
 */
public class BitmapDrawableActivity extends AppCompatActivity {

    LinearLayout bitmapLayout;
    int backStatus=0;
    BitmapDrawable bitmapDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap_drawable);
        bitmapLayout=findViewById(R.id.bitmap_layout);
        bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.bitmap_drawable);

    }

    public void changeBackground(View view) {

        if(backStatus==0) {
            //bitmapLayout.setBackground(null);
            backStatus=1;
            //调整平铺方式
            bitmapDrawable.setTileModeX(Shader.TileMode.REPEAT);
            //设置透明度,0-255,透明-全显
            bitmapDrawable.setAlpha(100);
            bitmapLayout.setBackground(bitmapDrawable);
        }
        else if(backStatus==1){
            //bitmapLayout.setBackground(null);
            backStatus=0;
            //调整平铺方式
            bitmapDrawable.setTileModeX(Shader.TileMode.MIRROR);
            //设置透明度,0-255,透明-全显
            bitmapDrawable.setAlpha(250);
            bitmapLayout.setBackground(bitmapDrawable);
        }
    }
}

activity_bitmap_drawable.xml:

<?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"
    android:orientation="vertical"
    tools:context="com.hfut.operationdrawable.BitmapDrawableActivity">

    <LinearLayout
        android:id="@+id/bitmap_layout"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/bitmap_drawable"
        android:orientation="horizontal"></LinearLayout>

    <LinearLayout
        android:gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="changeBackground"
            android:text="切换背景" />

    </LinearLayout>

</LinearLayout>

第四步:运行查看结果

点击“切换背景”(透明度和平铺方式都重新设置):

至此,关于BitmapDrawable的基本用法就介绍完了,我在测试的时候有一个疑问,就是通过xml配置android:alpha属性的时候并不是数值越小透明度越高,而是相反的。有兴趣的朋友可以试一试。

猜你喜欢

转载自blog.csdn.net/hfut_why/article/details/81784307
今日推荐