ShapeTextView

自定義ShapeTextView 其實就是代碼代替xml實現(xiàn)shape的過程

ShapeTextView

屬性的定義

每個View都有一些它的特殊屬性难礼,在創(chuàng)建新的View的時候脑又,應(yīng)該考慮到它所具有的屬性并在在res-values-styles文件中定義View需要的屬性。關(guān)于屬性的介紹可參考繪制鐘表

 <declare-styleable name="ShapeTextView">
        <attr name="shape" format="enum">
            <enum name="rectangle" value="0" />
            <enum name="oval" value="1" />
        </attr>
        <attr name="solidNormal" format="color" />
        <attr name="solidPressed" format="color" />
        <attr name="cornersRadius" format="dimension" />
        <attr name="cornerTopLeft" format="dimension" />
        <attr name="cornerTopRight" format="dimension" />
        <attr name="cornerBottomLeft" format="dimension" />
        <attr name="cornerBottomRight" format="dimension" />
        <attr name="strokeWidth" format="dimension" />
        <attr name="strokeColor" format="color" />
    </declare-styleable>
屬性 類型 作用
shape enum 枚舉類型扬霜,定義了ovalrectangle常用的兩種
solidNormal color 填充色(正常顯示)
solidPressed color 填充色(點擊顯示)
cornersRadius dimension 圓角半徑(shape:rectangle)可用
cornerTopLeft、cornerTopRight均牢、cornerBottomLeft涩禀、cornerBottomRight dimension 自定義每個角的半徑,不能同時設(shè)置cornersRadius屬性咆繁,或設(shè)置cornersRadius為0
strokeWidth dimension 描邊的寬度
strokeColor color 描邊的顏色

創(chuàng)建ShapeTextView 繼承TextView

在構(gòu)造方法中獲取自定義的屬性

  public ShapeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ShapeTextView);
        shape = array.getInteger(R.styleable.ShapeTextView_shape, SHAPE_RECTANGEL);


        solidNormalColor = array.getColor(R.styleable.ShapeTextView_solidNormal, Color.parseColor("#00000000"));
        solidPressedColor = array.getColor(R.styleable.ShapeTextView_solidPressed, Color.parseColor("#00000000"));


        cornersRadius = array.getDimension(R.styleable.ShapeTextView_cornersRadius, 0);

        cornersTopLeft = array.getDimension(R.styleable.ShapeTextView_cornerTopLeft, 0);
        cornersTopRight = array.getDimension(R.styleable.ShapeTextView_cornerTopRight, 0);
        cornersBottomLeft = array.getDimension(R.styleable.ShapeTextView_cornerBottomLeft, 0);
        cornersBottomRight = array.getDimension(R.styleable.ShapeTextView_cornerBottomRight, 0);

        strokeWidth = array.getDimension(R.styleable.ShapeTextView_strokeWidth, 0);

        strokeColor = array.getColor(R.styleable.ShapeTextView_strokeColor, Color.parseColor("#00000000"));
        array.recycle();
    }

實現(xiàn)shape標簽

使用GradientDrawable類在代碼中實現(xiàn)shape標簽中的屬性

   // normal state
        GradientDrawable drawableNormal = new GradientDrawable();
        // 設(shè)置Shape
        drawableNormal.setShape(shape);
        // 設(shè)置圓角半徑
        drawableNormal.setCornerRadius(cornersRadius);
        // 圓角半徑(每個圓角半徑的值)
        if (cornersRadius == 0) {
            drawableNormal.setCornerRadii(new float[]{
                    cornersTopLeft, cornersTopLeft,
                    cornersTopRight, cornersTopRight,
                    cornersBottomRight, cornersBottomRight,
                    cornersBottomLeft, cornersBottomLeft});
        }
        //描邊的寬度和顏色
        drawableNormal.setStroke((int) strokeWidth, strokeColor);
        //設(shè)置填充色
        drawableNormal.setColor(solidNormalColor);

實現(xiàn)selector 標簽

使用StateListDrawable在代碼中實現(xiàn)selector標簽中的屬性

    // 設(shè)置背景選擇器
        StateListDrawable stateListDrawable = new StateListDrawable();

        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed);

        stateListDrawable.addState(new int[]{}, drawableNormal);

        // 設(shè)置視圖的背景
        setBackground(stateListDrawable);

重寫onDraw()方法

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setShape();//方法內(nèi)主要內(nèi)容為上面代碼段
    }

效果預(yù)覽

enter image description here
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末讳推,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子玩般,更是在濱河造成了極大的恐慌银觅,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件坏为,死亡現(xiàn)場離奇詭異究驴,居然都是意外死亡,警方通過查閱死者的電腦和手機匀伏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門洒忧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人够颠,你說我怎么就攤上這事熙侍。” “怎么了履磨?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵蛉抓,是天一觀的道長。 經(jīng)常有香客問我剃诅,道長芝雪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任综苔,我火速辦了婚禮,結(jié)果婚禮上位岔,老公的妹妹穿的比我還像新娘如筛。我一直安慰自己,他們只是感情好抒抬,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布杨刨。 她就那樣靜靜地躺著,像睡著了一般擦剑。 火紅的嫁衣襯著肌膚如雪妖胀。 梳的紋絲不亂的頭發(fā)上芥颈,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機與錄音赚抡,去河邊找鬼爬坑。 笑死,一個胖子當著我的面吹牛涂臣,可吹牛的內(nèi)容都是我干的盾计。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼赁遗,長吁一口氣:“原來是場噩夢啊……” “哼署辉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起岩四,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤哭尝,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后剖煌,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體材鹦,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年末捣,在試婚紗的時候發(fā)現(xiàn)自己被綠了侠姑。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡箩做,死狀恐怖莽红,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情邦邦,我是刑警寧澤安吁,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站燃辖,受9級特大地震影響鬼店,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜黔龟,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一妇智、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧氏身,春花似錦巍棱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至陷虎,卻和暖如春到踏,著一層夾襖步出監(jiān)牢的瞬間杠袱,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工窝稿, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留楣富,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓讹躯,卻偏偏與公主長得像菩彬,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子潮梯,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,163評論 25 707
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理骗灶,服務(wù)發(fā)現(xiàn),斷路器秉馏,智...
    卡卡羅2017閱讀 134,659評論 18 139
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫耙旦、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,105評論 4 62
  • NumPy 是 Python 中科學(xué)計算的基礎(chǔ)包萝究,很多其他的科學(xué)計算庫都是構(gòu)建在這個庫之上免都,在 Numpy 官網(wǎng)上...
    gxyz閱讀 205評論 0 1
  • 今天下午,帶孩子去秋林書城讀書帆竹。見到一個中年婦女绕娘,帶著一個三歲的小男孩。這個男孩非常調(diào)皮栽连,在非常安靜的讀書環(huán)境中险领,...
    心如美玉閱讀 367評論 0 1