Android使用預設樣式創建View的幾個姿勢

来源:http://www.cnblogs.com/linzenews/archive/2016/08/28/5814269.html
-Advertisement-
Play Games

以下內容是分析安卓源碼所得: 1: 使用預設樣式創建View的方式, 源碼文件 Button.Java 註:此文參考http://www.linzenews.com/ 中的內容所寫,如侵刪! 2: 需要聲明預設樣式的屬性, 源碼文件 attrs.xml 3:創建預設樣式, 源碼文件 styles.x ...


 

以下內容是分析安卓源碼所得:

1: 使用預設樣式創建View的方式, 源碼文件 Button.Java 
這裡寫圖片描述

註:此文參考http://www.linzenews.com/ 中的內容所寫,如侵刪!

2: 需要聲明預設樣式的屬性, 源碼文件 attrs.xml 
這裡寫圖片描述

3:創建預設樣式, 源碼文件 styles.xml 
這裡寫圖片描述

4:在APP主題中,引用預設樣式 themes.xml (註意這步不能忘記) 
這裡寫圖片描述

源碼分析結束.


以下是我個人的使用經驗:

1:主題中引用 radioButton樣式 
這裡寫圖片描述

2:聲明預設樣式屬性 
這裡寫圖片描述

3:創建預設樣式 
這裡寫圖片描述

4:使用預設樣式創建View 
這裡寫圖片描述

這篇博客我們來介紹一下策略模式(Strategy Pattern,或者叫 Policy Pattern),也是行為型模式之一。通常在軟體開發中,我們為了一個功能可能會設計多種演算法和策略,然後根據實際使用情況動態選擇對應的演算法和策略,比如排序演算法中的快速排序,冒泡排序等等,根據時間和空間的綜合考慮進行運行時選擇。 
  針對這種情況,一個常規的方法是將多種演算法寫在一個類中,每一個方法對應一個具體的排序演算法,或者寫在一個方法中,通過 if…else 或者 switch…case 條件來選擇具體的排序演算法。這兩種方法我們都成為硬編碼,雖然很簡單,但是隨著演算法數量的增加,這個類就會變得越來越臃腫,維護的成本就會變高,修改一處容易導致其他地方的錯誤,增加一種演算法就需要修改封裝演算法類的源代碼,即違背了開閉原則單一職責原則。 
  如果將這些演算法或者策略抽象出來,提供一個統一的介面,不同的演算法或者策略有不同的實現類,這樣在程式客戶端就可以通過註入不同的實現對象來實現演算法或者策略的動態替換,這種模式的可擴展性、可維護性也就更高,這就是下麵講到的策略模式。 
  

設計模式總目錄

  java/android 設計模式學習筆記目錄

特點

  策略模式定義了一系列的演算法,並將每一個演算法封裝起來,而且使他們可以相互替換,讓演算法獨立於使用它的客戶而獨立變化。 
  策略模式的使用場景:

  • 針對同一類型問題的多種處理方式,僅僅是具體行為有差別時;
  • 需要安全地封裝多種同一類型的操作時;
  • 出現同一抽象類有多個子類,而又需要使用 if-else 或者 switch-case 來選擇具體子類時。

 

UML類圖

  我們來看看策略模式的 uml 類圖: 
  這裡寫圖片描述 
策略模式有三個角色:

  • Context:用來操作策略的上下文環境;
  • Stragety:策略的抽象;
  • ConcreteStrategy:具體的策略實現。

  據此我們可以寫出策略模式的通用代碼: 
Stragety.class

 

public interface Stragety {
    void algorithm();
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

ConcreteStragetyA.class 和 ConcreteStragetyB.class

public class ConcreteStragetyA implements Stragety{
    @Override
    public void algorithm() {
        System.out.print("ConcreteStragetyA\n");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
public class ConcreteStragetyB implements Stragety{
    @Override
    public void algorithm() {
        System.out.print("ConcreteStragetyB\n");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

預設策略類ConcreteStragetyDefault.class

public class ConcreteStragetyDefault implements Stragety{
    @Override
    public void algorithm() {
        System.out.print("null stragety");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Context.class 和測試代碼

public class Context {

    private Stragety stragety;

    public Context() {
        stragety = new ConcreteStragetyDefault();
    }

    public void algorithm() {
        stragety.algorithm();
    }

    public void setStragety(Stragety stragety) {
        if (stragety == null) {
            throw new IllegalArgumentException("argument must not be null!!!");
        }
        this.stragety = stragety;
    }

    public static void main(String args[]) {
        Context context = new Context();
        context.setStragety(new ConcreteStragetyA());
        context.algorithm();
        context.setStragety(new ConcreteStragetyB());
        context.algorithm();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

代碼很簡單,一目瞭然,沒有if-else,沒有 switch-case。核心就是建立抽象,將不同的策略構建成一個個具體的策略實現,通過不同的策略實現演算法替換,在簡化邏輯、結構的同時,增強系統的可讀性、穩定性和可擴展性,這對於較為複雜的業務邏輯顯得更為直觀,擴展也更加方便。

示例與源碼

Android 源碼中策略模式

  其實在 Android 源碼中策略模式使用的次數也是很多,大家常見的動畫中就有使用到策略模式:

public abstract class Animation implements Cloneable {
    /**
     * The interpolator used by the animation to smooth the movement.
     */
    Interpolator mInterpolator;
    ....
    /**
     * Sets the acceleration curve for this animation. Defaults to a linear
     * interpolation.
     *
     * @param i The interpolator which defines the acceleration curve
     * @attr ref android.R.styleable#Animation_interpolator
     */
    public void setInterpolator(Interpolator i) {
        mInterpolator = i;
    }
    ....
    /**
     * Gets the transformation to apply at a specified point in time. Implementations of this
     * method should always replace the specified Transformation or document they are doing
     * otherwise.
     *
     * @param currentTime Where we are in the animation. This is wall clock time.
     * @param outTransformation A transformation object that is provided by the
     *        caller and will be filled in by the animation.
     * @return True if the animation is still running
     */
    public boolean getTransformation(long currentTime, Transformation outTransformation) {
        ......

            final float interpolatedTime = mInterpolator.getInterpolation(normalizedTime);
            applyTransformation(interpolatedTime, outTransformation);
        ......
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

Animation 類就是很典型用到策略模式的類,它裡面會有一個 Interpolator 插值器對象,用來在執行動畫的時候達到所需要的速度變化效果,系統提供的插值器有 LinearInterpolator(線性插值器,動畫的執行速度相等),AccelerateDecelerateInterpolator (加速減速插值器,動畫的執行起始加速,結尾減速),DecelerateInterpolator(減速插值器,速度隨著動畫的執行變慢),以及回彈插值器等等,感興趣的上網查閱一下相關資料即可(我曾經在android下拉刷新框架用過插值器的相關類,是一個很有用的類)。

wiki example

  這裡我就仍然以 wiki 上的代碼為例,商場在不同時段會有打折促銷活動,顧客在不同的時段分別進行購買,最後得出一個價格: 
BillingStragety.class

interface BillingStrategy {
    public double getActPrice(double rawPrice);
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

NormalStrategy.class 和 HappyHourStragety.class

// Normal billing strategy (unchanged price)
class NormalStrategy implements BillingStrategy {
    @Override
    public double getActPrice(double rawPrice) {
        return rawPrice;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
// Strategy for Happy hour (50% discount)
class HappyHourStrategy implements BillingStrategy {
    @Override
    public double getActPrice(double rawPrice) {
        return rawPrice * 0.5;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Customer.class

class Customer {

    private List<Double> drinks;
    private BillingStrategy strategy;

    public Customer(BillingStrategy strategy) {
        this.drinks = new ArrayList<Double>();
        this.strategy = strategy;
    }

    public void add(double price, int quantity) {
        drinks.add(strategy.getActPrice(price * quantity));
    }

    // Payment of bill
    public void printBill() {
        double sum = 0;
        for (Double i : drinks) {
            sum += i;
        }
        System.out.println("Total due: " + sum);
        drinks.clear();
    }

    // Set Strategy
    public void setStrategy(BillingStrategy strategy) {
        this.strategy = strategy;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

main

public class StrategyPatternWiki {

    public static void main(String[] args) {
        Customer firstCustomer = new Customer(new NormalStrategy());

        // Normal billing
        firstCustomer.add(1.0, 1);

        // Start Happy Hour
        firstCustomer.setStrategy(new HappyHourStrategy());
        firstCustomer.add(1.0, 2);

        // New Customer
        Customer secondCustomer = new Customer(new HappyHourStrategy());
        secondCustomer.add(0.8, 1);
        // The Customer pays
        firstCustomer.printBill();

        // End Happy Hour
        secondCustomer.setStrategy(new NormalStrategy());
        secondCustomer.add(1.3, 2);
        secondCustomer.add(2.5, 1);
        secondCustomer.printBill();

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

不同時段買的東西,最後得出一個價錢,多種演算法動態切換,用戶不用去關心具體的內部細節。

總結

  策略模式主要是用來分離演算法,在相同的行為抽象下有不同的具體實現策略。這個模式很好的演示了開閉原則:定義抽象,增加新的策略只需要增加新的類,然後在運行中動態更換即可,沒有影響到原來的邏輯,從而達到了很好的可擴展性。 
  優點:

  • 結構清晰明瞭、使用簡單直觀;
  • 耦合度相對而言較低,擴展方便;
  • 操作封裝也更為徹底,數據更為安全。

  缺點也很明顯,這當然也是很多設計模式的通病:類的增多,隨著策略的增加,子類也會變得繁多。


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

-Advertisement-
Play Games
更多相關文章
  • 版權聲明:作者原創,轉載請註明出處! 編寫插件的兩種方式: 1.類級別開發插件(1%) 2.對象級別開發(99%) 類級別的靜態開發就是給jquery添加靜態方法,三種方式 1.添加新的全局函數 2.使用$.extend(obj) 3.使用命名空間 類級別開發插件(用的非常少,1%) 分別舉例: 調 ...
  • 或者 ...
  • 本篇主要探討的是iOS應用中各種狀態的躍遷過程,建議大家通過修改AppDelegate.swift,在每個過程中添加日誌輸出代碼,從而觀察其變化。 ...
  • 一、PackageManager的resolveActivity 返回能夠匹配的Intent,如果不能匹配就返回NULL, 二、PackageManager的queryIntentActivities 返回所有成功匹配的Activity信息 三、Intent的resolveActivity 第一個參 ...
  • Android 事件分發機制 ViewGroup dispatchTouchEvent 返回true ViewGroup自己在dispatchTouchEvent中消費這個事件 dispatchTouchEvent 返回false 回傳到Activity中onTouchEvent消費事件 dispa ...
  • 我們在《Android學習之Handler消息傳遞機制》(http://www.cnblogs.com/zhouhb/p/5812447.html)已提到過,Android只允許UI線程修改Activity里的UI組件,這樣會導致新啟動的線程無法動態改變界面組件的屬性值。為瞭解決新線程不能更新UI組 ...
  • 這裡用了 MainActivity佈局 主要代碼 源碼: https://github.com/ln0491/RadioButtonAndFragment ...
  • Swift - 從字典(或者Alamofire)直接創建Model文件的工具 效果 1. 常規生成model的方式 2. 通過debug創建model的方式 特性 1. 可以處理JSON格式的字典數據 2. 可以處理本地的json數據 3. 可以處理Alamofire生成的json格式返回數據 4. ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...