android 工具類,及方法


1.產生shape類型的drawable,再也不用寫一堆的shape了
 /**
     * 產生shape類型的drawable
     * @param solidColor    填充的顏色
     * @param strokeColor   描邊的顏色
     * @param strokeWidth   描邊的大小
     * @param radius        圓角的度數
     * @return
     */
    public static GradientDrawable getBackgroundDrawable(int solidColor, int strokeColor, int strokeWidth, float radius) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(solidColor);
        drawable.setStroke(strokeWidth, strokeColor);
        drawable.setCornerRadius(radius);
        return drawable;
    }
//這個方法功能更加強大
 /**
     * 1、2兩個參數表示左上角,3、4表示右上角,5、6表示右下角,7、8表示左下角
     * @param solidColor
     * @param strokeColor
     * @param strokeWidth
     * @param topLeftRadius
     * @param topRightRadius
     * @param bottomRightRadius
     * @param bottomLeftRadius
     * @return
     */
    public static GradientDrawable getBackgroundDrawable1(int solidColor, int strokeColor, int strokeWidth, float topLeftRadius, float topRightRadius
            , float bottomRightRadius, float bottomLeftRadius) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(solidColor);
        drawable.setStroke(strokeWidth, strokeColor);
        drawable.setCornerRadii(new float[]{
                topLeftRadius,
                topLeftRadius,
                topRightRadius,
                topRightRadius,
                bottomRightRadius,
                bottomRightRadius,
                bottomLeftRadius,
                bottomLeftRadius});
        return drawable;
    }


2.字體顏色選擇器Selector
 /**
     *  顏色點擊選擇器 selector 
     * @param intnormal   默認顏色
     * @param intchecked  點擊時的顏色
     * @return
     */
    public static ColorStateList getTextColorSelector(int intnormal, int intchecked){
        int[][] states = new int[2][];
        states[0] =  new int[]{android.R.attr.state_pressed};
        states[1] = new int[]{};
        int[] colors = new int[]{intchecked,intnormal};
        ColorStateList csl = new ColorStateList(states,colors);
        return csl;
    }

3.動態改變textTexview Hint顯示的大小,但不影響字體大小
    /**
     * 動態改變textTexview Hint顯示的大小,但不影響字體大小
     * @param hintMsg
     * @return
     */

    public static CharSequence getTextHint(String hintMsg) {
        SpannableString ss = new SpannableString(hintMsg);//定義hint的值
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(15, true);//設置字體大小 true表示單位是sp
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return new SpannedString(ss);
    }
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容