Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android各種各樣的Drawable-更新中

Android各種各樣的Drawable-更新中

編輯:關於Android編程

概述

通常情況下,我們很少直接使用Drawable的實現類,大部分的情況下還是使用xml的方式進行開發。只有在程序中需要動態的修改drawable的屬性時,才需要使用具體的drawable類型提供的方法。

事實上xml中配置的節點和Drawable的實現類是一一對應的。


Android 在android.graphics.drawable包中提供了了很多Drawable抽象類的實現類:ColorDrawable、GradientDrawable、BitmapDrawable、 NinePatchDrawable、InsetDrawable、ClipDrawable、ScaleDrawable、RotateDrawable、AnimationDrawable、LayerDrawable、LevelListDrawable、StateListDrawable、TransitionDrawable等等

這裡寫圖片描述


Element Class Description AnimatedRotateDrawable A Drawable that can animate a rotation of another Drawable. AnimationDrawable An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object’s background. BitmapDrawable A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. ClipDrawable A Drawable that clips another Drawable based on this Drawable’s current level value. You can control how much the child Drawable gets clipped in width and height based on the level, as well as a gravity to control where it is placed in its overall container. Most often used to implement things like progress bars. ColorDrawable A specialized Drawable that fills the Canvas with a specified color, with respect to the clip region. InsetDrawable A Drawable that insets another Drawable by a specified distance. This is used when a View needs a background that is smaller than the View’s actual bounds. LayerDrawable A Drawable that manages an array of other Drawables. These are drawn in array order. LevelListDrawable A resource that manages a number of alternate Drawables, each assigned a maximum numerical value. NinePatchDrawable A resizeable bitmap, with stretchable areas that you define. RotateDrawable A Drawable that can rotate another Drawable based on the current level value. The start and end angles of rotation can be controlled to map any circular arc to the level values range. ScaleDrawable A Drawable that changes the size of another Drawable based on its current level value. StateListDrawable Lets you assign a number of graphic images to a single Drawable and swap out the visible item based on state. GradientDrawable Basic method for drawing shapes via XML. TransitionDrawable An extension of that is intended to cross-fade between the first and second layer. The format is exactly the same as .

 


ColorDrawable

ColorDrawable 實際上是代表了單色可繪制區域,它包裝了一種固定的顏色,當ColorDrawable被繪制到畫布的時候會使用顏色填充Paint,在畫布上繪制一塊單色的區域。

Xml的方式

這裡寫圖片描述

android:background=”@drawable/colordrawable_xml”<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjwvcD4NCjxwcmUgY2xhc3M9"brush:java;">

顏色定義在color.xml中,使用時直接引用即可。


JavaCode的方式

代碼中必須要明確指出透明度(Alpha)代表的數據,即,要加上0x,而且不能把透明度漏掉,否則就是透明的了。


public class ColorDrawableActivity extends AppCompatActivity {


    private TextView mIdTvColorDrawableDemo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_drawable);

        mIdTvColorDrawableDemo = (TextView) findViewById(R.id.id_tv_colorDrawableDemo_javaCode);

        /**
         * setBackground 在 4.1(API16)之後增加
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            /**
             * 使用Java代碼創建ColorDrawable,
             * 需要注意的是Android中使用一個int類型的數據表示顏色值,
             * 通常習慣使用十六進制格式的數據表示顏色值。
             * 一個int類型包含四個字節,
             * 分別代表顏色的4個組成部分:透明度(Alpha)、紅(RED)、綠(GREEN)、藍(BLUE),
             * 每個部分由一個字節(8個bit)表示,取值范圍為0~255。
             * 在xml中使用顏色時可以省略透明度(Alpha)部分,
             * 如#ff0000表示紅色。
             * 但是在代碼中必須要明確指出透明度(Alpha)代表的數據,
             * 如果省略了就表示完全透明的顏色,也就是說當繪制到畫布上時,看不出有任何效果。
             */
            mIdTvColorDrawableDemo.setBackground(new ColorDrawable(0xffff0000));
        }
    }
}

運行圖

這裡寫圖片描述


GradientDrawable

GradientDrawable 表示一個漸變區域,可以實現線性漸變、發散漸變和平鋪漸變效果。

在xml文件中使用shape作為根節點來創建GradientDrawable,很多屬性和子節點。

 

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

Xml的方式

線性漸變linear

<!--?xml version="1.0" encoding="utf-8"?-->
<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
    一個具有漸變區域的Drawable,可以實現線性漸變,發散漸變和平鋪漸變效果
      核心節點:<gradient/>,有如下可選屬性:
        startColor:漸變的起始顏色
        centerColor:漸變的中間顏色
        endColor:漸變的結束顏色
        type:漸變類型,可選(linear,radial,sweep), 線性漸變(可設置漸變角度),發散漸變(中間向四周發散),平鋪漸變
        centerX:漸變中間亞瑟的x坐標,取值范圍為:0~1
        centerY:漸變中間顏色的Y坐標,取值范圍為:0~1
        angle:只有linear類型的漸變才有效,表示漸變角度,必須為45的倍數哦
        gradientRadius:只有radial和sweep類型的漸變才有效,radial必須設置,表示漸變效果的半徑
        useLevel:判斷是否根據level繪制漸變效果
    -->

    <gradient android:angle="90" android:centercolor="#00ff00" android:endcolor="#0000ff" android:startcolor="#ff0000">

    <!--描邊-->
    <stroke android:color="#fff" android:dashgap="5dip" android:dashwidth="4dip" android:width="3dip">
</stroke></gradient></shape></pre>
<h3 id="發散漸變radial">發散漸變radial</h3>
<pre class="brush:java;">
<code class="hljs xml"><!--?xml version="1.0" encoding="utf-8"?-->
<shape android:innerradius="0dp" android:shape="ring" android:thickness="100dp" android:uselevel="false" xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient android:centercolor="#FFEB82" android:endcolor="#35B2DE" android:gradientradius="50" android:startcolor="#DEACAB" android:type="radial" android:uselevel="false">

平鋪漸變sweep

<!--?xml version="1.0" encoding="utf-8"?-->
<shape android:innerradiusratio="8" android:shape="ring" android:thicknessratio="3" android:uselevel="false" xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient android:centercolor="#FFEB82" android:endcolor="#35B2DE" android:startcolor="#DEACAB" android:type="sweep" android:uselevel="false">

引用的Activity

<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

    <textview android:background="@drawable/gradient_linear" android:gravity="center" android:id="@+id/id_tv_gradientDrawableDemo_Xml1" android:layout_gravity="center_horizontal" android:layout_height="200dp" android:layout_margin="10dp" android:layout_width="200dp" android:text="線性漸變linear-ByXml" android:textcolor="@color/bg_black" android:textsize="20sp">


    <textview android:background="@drawable/gradient_radial" android:gravity="center" android:id="@+id/id_tv_gradientDrawableDemo_Xml2" android:layout_gravity="center_horizontal" android:layout_height="200dp" android:layout_margin="10dp" android:layout_width="200dp" android:text="發散漸變radial-ByXml" android:textcolor="@color/bg_black" android:textsize="20sp">

    <textview android:background="@drawable/gradient_sweep" android:gravity="center" android:id="@+id/id_tv_gradientDrawableDemo_Xml3" android:layout_gravity="center_horizontal" android:layout_height="200dp" android:layout_margin="10dp" android:layout_width="200dp" android:text="平鋪漸變sweep-ByXml" android:textcolor="@color/bg_black" android:textsize="20sp">

運行圖

這裡寫圖片描述


BitmapDrawable

BitmapDrawable 是對bitmap的一種包裝,可以設置它包裝的bitmap在BitmapDrawable區域內的繪制方式,如平鋪填充、拉伸填充或者保持圖片原始大小,也可以在BitmapDrawable區域內部使用gravity指定的對齊方式。

在xml文件中使用bitmap作為根節點來定義BitmapDrawable。

這裡寫圖片描述

可選屬性說明:

src:圖片資源~ antialias:是否支持抗鋸齒 filter:是否支持位圖過濾,支持的話可以是圖批判顯示時比較光滑 dither:是否對位圖進行抖動處理 gravity:若位圖比容器小,可以設置位圖在容器中的相對位置 tileMode:指定圖片平鋪填充容器的模式,設置這個的話,gravity屬性會被忽略,有以下可選值: disabled(整個圖案拉伸平鋪),clamp(原圖大小), repeat(平鋪),mirror(鏡像平鋪)

Xml的方式


  

JavaCode的方式



import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.turing.base.R;
import com.turing.base.utils.AlertUtil;

public class BitmapDrawableActivity extends AppCompatActivity {

    private ImageView mIdBitmapDrawableXml;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap_drawable);

        mIdBitmapDrawableXml = (ImageView) findViewById(R.id.id_bitmapDrawable_xml);

        // 通過代碼的方式創建BitmapDrawable
        Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.zjl);
        BitmapDrawable mBitmapDrawable = new BitmapDrawable(mBitmap);
        mBitmapDrawable.setTileModeXY(Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
        mBitmapDrawable.setAntiAlias(true);
        mBitmapDrawable.setDither(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mIdBitmapDrawableXml.setBackground(mBitmapDrawable);
//            mIdBitmapDrawableXml.setImageDrawable(mBitmapDrawable);

        } else {
            AlertUtil.showDialogWithClose(this, "API<16 不支持setBackground屬性設置");
        }
    }
}

這種方式基本上很啰嗦,很少會用到,了解下就好了

運行圖

這裡寫圖片描述


InsetDrawable

表示把一個Drawable嵌入到另外一個Drawable的內部,並且在內部留一些間距, 類似與Drawable的padding屬性,但padding表示的是Drawable的內容與Drawable本身的邊距! 而InsetDrawable表示的是兩個Drawable與容器之間的邊距,*當控件需要的背景比實際的邊框 小的時候,比較適合使用InsetDrawable*

比如使用這個可以解決我們自定義Dialog與屏幕之間 的一個間距問題,相信做過的朋友都知道,即使我們設置了layout_margin的話也是沒用的,這個 時候就可以用到這個InsetDrawable了!只需為InsetDrawable設置一個insetXxx設置不同 方向的邊距,然後為設置為Dialog的背景即可!

這裡寫圖片描述
相關屬性如下:

drawable:引用的Drawable,如果為空,必須有一個Drawable類型的子節點! visible:設置Drawable是否可見 insetLeft,insetRight,insetTop,insetBottm:設置左右上下的邊距

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved