java代码设置drawable,Android使用Java代碼設置selector或drawable,以及使用自定義控件方式使用它...

鎮樓圖~~!

125774a95feb765b347948864e6c996f.gif

TextView再給個selecotor 這種東西不要太簡單,但是這種東西我不想重復去寫N個Selector ! so~/**

* 獲取Selector

*@param normalDraw

*@param pressedDraw

*@return

*/

public static StateListDrawable getSelector(Drawable normalDraw, Drawable pressedDraw) {

StateListDrawable stateListDrawable = new StateListDrawable();

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

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

return stateListDrawable ;

}

/**

* 設置shape(設置單獨圓角)

*@param topLeftCA

*@param topRigthCA

*@param buttomLeftCA

*@param buttomRightCA

*@param bgColor

*@param storkeWidth

*@param strokeColor

*@return

*/

public GradientDrawable getDrawable(float topLeftCA, float topRigthCA, float buttomLeftCA,

float buttomRightCA, int bgColor, int storkeWidth, int strokeColor) {

//把邊框值設置成dp對應的px

storkeWidth = dp2px(this.mContext, storkeWidth);

float[] circleAngleArr = {topLeftCA, topLeftCA, topRigthCA, topRigthCA,

buttomLeftCA, buttomLeftCA, buttomRightCA, buttomRightCA};

//把圓角設置成dp對應的px

for (int i = 0; i < circleAngleArr.length; i++){

circleAngleArr[i] = dp2px(this.mContext, circleAngleArr[i]);

}

GradientDrawable gradientDrawable = new GradientDrawable();

gradientDrawable.setCornerRadii(circleAngleArr);//圓角

gradientDrawable.setColor(bgColor); //背景色

gradientDrawable.setStroke(storkeWidth, strokeColor); //邊框寬度,邊框顏色

return gradientDrawable;

}

/**

* 設置shape(圓角)

*

*@param bgCircleAngle

*@param bgColor

*@param width

*@param strokeColor

*@return

*/

public GradientDrawable getDrawable(int bgCircleAngle, int bgColor, int width, int strokeColor) {

GradientDrawable gradientDrawable = new GradientDrawable();

gradientDrawable.setCornerRadius(bgCircleAngle);

gradientDrawable.setColor(bgColor);

gradientDrawable.setStroke(width, strokeColor);

return gradientDrawable;

}

這里就不多說了,跟用xml設置Selector的步驟是一樣的

值得注意的是:

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

這段在設置值的時候前后要給空格,否則這個按下的狀態會設置不上去,具體原因這里就不深究了。

在getDrawable有兩個方法,一個是設置矩形所有角的弧度(radius),一個是單獨設置每一個角的弧度(topLeftRadius,topRightRadius,bottomLeftRadius,bottomRightRadius)gradientDrawable.setCornerRadii(circleAngleArr);//圓角

這個方法需要傳入一個有8個值的float數組,(開始沒想明白,后來翻了翻源碼,每一個角對應2個值,一個x一個y,好吧~高中數學都還給老師了,一個值怎么畫弧線呢 - -!~)/**

*

Specify radii for each of the 4 corners. For each corner, the array

* contains 2 values, [X_radius, Y_radius]. The corners are ordered

* top-left, top-right, bottom-right, bottom-left. This property

* is honored only when the shape is of type {@link #RECTANGLE}.

*

Note: changing this property will affect all instances

* of a drawable loaded from a resource. It is recommended to invoke

* {@link #mutate()} before changing this property.

*

* @param radii 4 pairs of X and Y radius for each corner, specified in pixels.

* The length of this array must be >= 8

*

* @see #mutate()

* @see #setCornerRadii(float[])

* @see #setShape(int)

*/

public void setCornerRadii(float[] radii) {

mGradientState.setCornerRadii(radii);

mPathIsDirty = true;

invalidateSelf();

}

代碼注釋寫的很清楚了我就不拿我的三級英語來扯了···

使用:

如果只想添加個圓角背景那么可以這樣GradientDrawable gradientDrawable = SelectorUtils.getInstance(this)

.getDrawable(20, ContextCompat.getColor(this, R.color.bisque), 2, ContextCompat.getColor(this, R.color.aqua));

editText.setBackground(gradientDrawable);

如果是SelectorGradientDrawable normalDraw = SelectorUtils.getInstance(this)

.getDrawable(20, ContextCompat.getColor(this, R.color.aliceblue), 2, ContextCompat.getColor(this, R.color.blueviolet));

GradientDrawable pressDraw = SelectorUtils.getInstance(this)

.getDrawable(20, ContextCompat.getColor(this, R.color.brown), 2, ContextCompat.getColor(this, R.color.colorPrimaryDark));

StateListDrawable stateListDrawable = SelectorUtils.getSelector(normalDraw, pressDraw);

textView.setBackground(stateListDrawable);

如果想單獨設置每個角的弧度可以這樣:GradientDrawable normalDraw1 = SelectorUtils.getInstance(this)

.getDrawable(10,20,0,40, ContextCompat.getColor(this, R.color.chocolate), 2, ContextCompat.getColor(this, R.color.darkcyan));

GradientDrawable pressDraw1 = SelectorUtils.getInstance(this)

.getDrawable(10, 10, 0, 0, ContextCompat.getColor(this, R.color.darkgoldenrod), 2, ContextCompat.getColor(this, R.color.darkorchid));

StateListDrawable stateListDrawable1 = SelectorUtils.getSelector(normalDraw1,pressDraw1);

textView1.setBackground(stateListDrawable1);

如果使用selector最后記得設置點擊事件:tvText.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

}

});

如果不設置點擊事件,按下的drawable不會顯示,原因是點擊事件未分發,這里不過多解釋。

是不是省去了很多selector或者drawable文件~~ 最近一直想寫一個自定義控件,so~~

先來張效果圖:

067d995233e45a620efbb28c9a761e6b.gif

使用:

android:id="@+id/spd5"

android:layout_width="80dp"

android:layout_height="80dp"

android:layout_marginTop="10dp"

android:gravity="center"

android:padding="10dp"

android:text="方某人"

android:textSize="16.0sp"

app:bgClickColor="@color/yellow"

app:bgColor="@color/aqua"

app:buttomLeftCA="30dp"

app:buttomRightCA="40dp"

app:normalStorkeColor="@color/cadetblue"

app:normalStorkeWidth="2dp"

app:pressStorkeColor="@color/cornflowerblue"

app:pressStorkeWidth="2dp"

app:topLeftCA="10dp"

app:topRightCA="20dp" />

這里需要注意下,bgColor這條屬性是必須設置的,否則背景色是透明的~~原諒我的設置了默認值 - -!~ 屬性這里就不多介紹了,源碼都有注釋(雖然是我自己寫的,但是每次用的時候都要去看自己寫的注釋~)。

經常用一些牛逼的第三方庫 免不了想給自己來一波高大上的調用 so~~

你還可以這樣用:((SpdTextView)findViewById(R.id.spd6))

.setBgColor(ContextCompat.getColor(this, R.color.darkorchid))

.setBgClickColor(ContextCompat.getColor(this, R.color.brown))

.setNormalStorkeWidth(5)

.setNormalStorkeColor(ContextCompat.getColor(this, R.color.burlywood))

.setPressStorkeWidth(2)

.setPressStorkeColor(ContextCompat.getColor(this, R.color.antiquewhite))

.setBgCircleAngle(40)

.build();

引入:compile 'com.fmr:speedytextview:1.0.0'

好吧 裝逼結束 最后打上注釋// _ooOoo_

// o8888888o

// 88" . "88

// (| -_- |)

// O\ = /O

// ____/`---'\____

// . ' \\| |// `.

// / \\||| : |||// \

// / _||||| -:- |||||- \

// | | \\\ - /// | |

// | \_| ''\---/'' | |

// \ .-\__ `-` ___/-. /

// ___`. .' /--.--\ `. . __

// ."" '< `.___\__/___.' >'"".

// | | : `- \`.;`\ _ /`;.`/ - ` : | |

// \ \ `-. \_ __\ /__ _/ .-` / /

// ======`-.____`-.___\_____/___.-`____.-'======

// `=---='

//

// .............................................

// 佛祖保佑 永無BUG

// 佛曰:

// 寫字樓里寫字間,寫字間里程序員;

// 程序人員寫程序,又拿程序換酒錢。

// 酒醒只在網上坐,酒醉還來網下眠;

// 酒醉酒醒日復日,網上網下年復年。

// 但願老死電腦間,不願鞠躬老板前;

// 奔馳寶馬貴者趣,公交自行程序員。

// 別人笑我忒瘋癲,我笑自己命太賤;

// 不見滿街漂亮妹,哪個歸得程序員?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值