第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

以編程方式設(shè)置android形狀顏色

以編程方式設(shè)置android形狀顏色

三國紛爭 2019-08-06 15:53:16
以編程方式設(shè)置android形狀顏色我正在編輯以使問題更簡單,希望這有助于獲得準(zhǔn)確的答案。說我有以下oval形狀:<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">     <solid android:angle="270"            android:color="#FFFF0000"/>     <stroke android:width="3dp"             android:color="#FFAA0055"/></shape>如何在活動(dòng)類中以編程方式設(shè)置顏色?
查看完整描述

3 回答

?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊

drawable是一個(gè)橢圓形,是ImageView的背景


Drawable從imageView使用中獲取getBackground():


Drawable background = imageView.getBackground();

檢查通常的嫌疑人:


if (background instanceof ShapeDrawable) {

    // cast to 'ShapeDrawable'

    ShapeDrawable shapeDrawable = (ShapeDrawable) background;

    shapeDrawable.getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet));

} else if (background instanceof GradientDrawable) {

    // cast to 'GradientDrawable'

    GradientDrawable gradientDrawable = (GradientDrawable) background;

    gradientDrawable.setColor(ContextCompat.getColor(mContext,R.color.colorToSet));

} else if (background instanceof ColorDrawable) {

    // alpha value may need to be set again after this call

    ColorDrawable colorDrawable = (ColorDrawable) background;

    colorDrawable.setColor(ContextCompat.getColor(mContext,R.color.colorToSet));

}

緊湊版:


Drawable background = imageView.getBackground();

if (background instanceof ShapeDrawable) {

    ((ShapeDrawable)background).getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet));

} else if (background instanceof GradientDrawable) {

    ((GradientDrawable)background).setColor(ContextCompat.getColor(mContext,R.color.colorToSet));

} else if (background instanceof ColorDrawable) {

    ((ColorDrawable)background).setColor(ContextCompat.getColor(mContext,R.color.colorToSet));

}

請注意,不需要進(jìn)行空檢查。


但是,mutate()如果在其他地方使用它們,則應(yīng)在修改之前使用drawables。(默認(rèn)情況下,從XML加載的drawable共享相同的狀態(tài)。)


查看完整回答
反對 回復(fù) 2019-08-06
?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊

這樣做:

    ImageView imgIcon = findViewById(R.id.imgIcon);
    GradientDrawable backgroundGradient = (GradientDrawable)imgIcon.getBackground();
    backgroundGradient.setColor(getResources().getColor(R.color.yellow));



查看完整回答
反對 回復(fù) 2019-08-06
?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

現(xiàn)在更簡單的解決方案是使用您的形狀作為背景,然后通過編程方式更改其顏色

view.getBackground().setColorFilter(Color.parseColor("#343434"),?PorterDuff.Mode.SRC_OVER)

編輯:

就像評論中正確指出的人一樣PorterDuff.Mode.SRC_ATOP,如果您的背景有圓角等,您可能會(huì)想要使用。


查看完整回答
反對 回復(fù) 2019-08-06
  • 3 回答
  • 0 關(guān)注
  • 541 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)