Android開發藝術探索學習筆記(六)

来源:http://www.cnblogs.com/shiwei-bai/archive/2016/01/06/5105401.html
-Advertisement-
Play Games

第六章 Android的Drawable Drawable的優點:使用簡單,比自定義view的成本要低;非圖片類型的Drawable占用空間小,有利於減小APK安裝包的大小。 6.1Drawable簡介 Drawable有很多種,他們都表示一種圖像的概念,Drawable常被用來...


第六章 Android的Drawable

      Drawable的優點:使用簡單,比自定義view的成本要低;非圖片類型的Drawable占用空間小,有利於減小APK安裝包的大小。

    6.1Drawable簡介

    Drawable有很多種,他們都表示一種圖像的概念,Drawable常被用來作為view的背景使用。Drawable是一個抽象類。Drawable的內部寬高這個參數比較重要,通過getIntrinsicWidth和getIntrinsicHeight這兩個方法可以獲取到他們。但並不是所有的Drawable都有內部寬高。

    6.2Drawable的分類

      6.2.1 BitmapDrawable

      BitmapDrawableb表示一張圖片,下麵是其常用的屬性(xml格式的描述方式)     

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@mipmap/ic_launcher"
    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:mipMap="true|false"
    android:tileMode="disabled|clamp|repeat|mirror"/>

      android:src  圖片的資源ID,必有的屬性。

      android:antialias  抗鋸齒功能

      android:dither 抖動效果(優化圖片顯示效果的作用)

      android:filter 過濾效果(優化圖片顯示效果的作用)

      android:gravity 定點陣圖片

      android:mipMap 紋理映射(不常用)

      android:tileMode 平鋪模式(當開啟這個模式時,gravity屬性會被忽略)共有四種模式:disabled——關閉平鋪模式;repeat——水平和垂直方向上的平鋪效果;clamp——圖片四周的像素會擴展到周圍區域。mirror——水平和垂直方向上的鏡面投影效果。

     NinePatchDrawable表示.9格式的圖片,xml格式描述如下:

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@mipmap/ic_launcher"
    android:dither="true|false"/>

      6.2.2 ShapeDrawable

        ShapeDrawable代表通過顏色來構造的圖形既可以是純色也可以有漸變效果。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle|oval|line|ring">
    <corners
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer"
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerColor="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type="linear|radial|sweep"
        android:useLevel="true|false" />
    <padding
        android:bottom="integer"
        android:left="integer"
        android:right="integer"
        android:top="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashGap="integer"
        android:dashWidth="integer" />

</shape>

      android:shape  圖形形狀:rectangle(矩形)、oval(橢圓)、line(橫線)、ring(圓環),預設矩形,line和ring必須有<stroke>標簽

      <corners>  四個角的角度,只適用於矩形

      <gradient>  漸變效果,與<solid>標簽互斥

      <solid>  純色填充

      <stroke>  描邊

      <padding>  包含它的View的空白

      <size>  shape的大小,表示shape的固有大小,並不代表最終顯示大小。

 

      6.2.3 LayerDrawable

       LayerDrawable代表層次化的Drawable集合,用來實現疊加效果。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@mipmap/ic_launcher"
        android:id="@+id/drawable"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension">
    </item>
</layer-list>

 

      一個<layer-list>可以包含多個item,每個item表示一個drawable,下麵的item會覆蓋上面的item,通過合理的分層,可以實現一些特殊的疊加效果。下麵的例子實現了一種微信中的文本輸入框效果

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#0ac39e" />
        </shape>
    </item>
    <item android:bottom="6dip">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </item>
    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </item>

</layer-list>

 

      6.2.4 StateListDrawable            

      主要用於設置可單擊的View的背景,最常見的是Button,對應於<selector>標簽。XML格式如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize="true|false"
    android:dither="true|false"
    android:variablePadding="true|false">
    <item
        android:drawable="@mipmap/ic_launcher"
        android:state_pressed="true|false"
        android:state_focused="true|false"
        android:state_selected="true|false"
        android:state_checked="true|false"
        android:state_enabled="true|false"
        />

</selector>

 

        item有很多狀態,以上只列取了幾種常見的狀態。

      android:constantSize StateListDrawable的固有大小是否不隨著其狀態的改變而改變。

      android:dither 是否開啟抖動效果

      android:variablePadding StateListDrawable的padding是否隨著其狀態的改變而改變。

      android:state_pressed 按下狀態

      android:state_focused 獲取焦點

      android:state_selected 選擇

      android:state_checked 選中

 

 

      android:state_enabled 可用     

 

        預設的item一般放在selector的最後一條並且不附帶任何狀態。

 

      6.2.5 LevelListDrawable

      Drawable集合,每個Drawable都有一個等級,根據不同的等級,LevelListDrawable會切換對應的Drawable。xml格式的語法如下:

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@mipmap/ic_launcher"
        android:maxLevel="integer"
        android:minLevel="integer" />

</level-list>

 

      等 級範圍:0~10000,作為View的背景時,可以通過Drawable的setLevel方法來設置不同的等級從而切換具體的Drawable;作為 ImageView的前景Drawable,可以通過ImageView的setImageLevel方法來切換Drawable。

 

      6.2.6  TransitionDrawable

      實現兩個Drawable之間的淡入淡出效果,對應於<transition>標簽,XML格式語法如下:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@mipmap/ic_launcher"
        android:id="@+id/resource_name"
        android:top="dimension"
        android:bottom="dimension"
        android:left="dimension"
        android:right="dimension"
        />
</transition>

 

      實例:切換TextView的背景

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/drawable1" />
    <item android:drawable="@mipmap/drawable2" />
</transition>
  <TextView
        android:background="@drawable/test9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
        TextView textView=(TextView)findViewById(R.id.test);
        TransitionDrawable drawable=(TransitionDrawable)textView.getBackground();
        drawable.startTransition(1000);

      6.2.7  InsetDrawable

      可以將其他Drawable內嵌到自己當中,對應於<inset>標簽,當一個View希望自己的背景比自己的實際區域小的時候可以採用InsetDrawable來實現。XML格式語法如下:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@mipmap/ic_launcher"
    android:insetTop="dimension"
    android:insetBottom="dimension"
    android:insetLeft="dimension"
    android:insetRight="dimension">
</inset>

      6.2.8  ScaleDrawable

      根據自己的等級將指定的Drawable縮放到一定比例;對應於<scale>標簽。XML語法如下:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@mipmap/ic_launcher"
    android:scaleGravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal"
    android:scaleHeight="percentage"
    android:scaleWidth="percentage"/>

      使用時還需要設置ScaleDrawable的等級大於0且小於10000的值,否則看不到效果

        ScaleDrawable scaleDrawable=(ScaleDrawable)textView.getBackground();
        scaleDrawable.setLevel(1);

       6.2.9   ClipDrawable

      根據自己當前的等級來裁剪另一個Drawable,裁剪方向通過android:clipOrientation和android:gravity這兩個屬性來共同控制。XML屬性語法如下:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="horizontal|vertical"
    android:drawable="@mipmap/ic_launcher"
    android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal" />

 

 

      等級範圍也是0~10000,0表示完全裁剪,10000表示不裁剪。

   ClipDrawable clipDrawable=(ClipDrawable)textView.getBackground();
   clipDrawable.setLevel(5000);

 

 

    6.3 自定義Drawable     

    只有某些特殊情況下才會用到,註意getIntrinsicWidth和getIntrinsicHeight這兩個方法(p262)。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Kotlin開發語言文檔,第一章:入門
  • 知識點: 1)UINavigationController 2)UINavigationBar 3)UINavigationItem 4)UIToolBar======================================================一、UINavigationCont...
  • /* ViewController.h 文件 */#import @interface ViewController : UIViewController@end/* ViewController.m文件 */#import "ViewController.h"@inte...
  • 1.目錄建立連接 主動調用獲取AccessToken 回調模式管理多媒體文件 上傳媒體文件 獲取媒體文件發送消息 發送介面說明 消息類型及數據格式 text消息 image消息 voice消息 video消息 file消息辦公消息免登服務 使用標準OAUTH2.0 HTTP ...
  • 企業煩惱一:找人的痛苦 調查數據顯示,在一個規模500人以上的企業中,找到跨部門的陌生員工,需要經過3步,高管,人事,前臺。 復興集團是上海市最大的民營企業,管理資產超過了3000億人民幣,集團總部有1000多人,而其下屬企業規模都能達到數千人,並且遍佈在全國全球。 復星集團IT總監趙沛表示,...
  • 普通用戶(個人)團隊企業1.免費額度為每月通話100分鐘、每天發DING 5次。1. 每月通話300分鐘,每天發DING 10次。2. 群組最多可達1500人1. 該公司所有註冊員工的通話分鐘均會上升至1000分鐘/月,每天DING的次數也會增加到30次。2. 群組最多可達1500人3. ...
  • 1.對於過多的控制項,功能類似,數量又多的,可以用include方法。在實現應用中,可以把控制項放入List集合中。private void initView() { // TODO Auto-generated method stub pwd1 = (EditText) findViewByI...
  • 如何獲取當前設備的系統版本!!![[[UIDevice currentDevice] systemVersion] floatValue]獲取App版本NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; // ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...