如何使用从图像动态选择调色板API来更改工具栏和文本背景颜色

时间:2016-02-21 17:05:39

标签: android android-palette

嗨,我是新来的android我想改变我的工具栏和文本背景颜色动态采用颜色使用调色板颜色选择器从图像,任何人请帮我如何更改我的工具栏颜色我有多个图像从json对象如何更改来自JSON的工具栏和文本背景,当我点击时,我想在每个项目上更改我的工具栏颜色,任何人都请帮助我如何得到这个 以下是我的代码

Meanswear.java

int pos = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_means_wear);

    image = (ImageView)findViewById(R.id.imageView);
    tv1 = (TextView)findViewById(R.id.textView2);

    tv3 = (TextView)findViewById(R.id.textView3);
    tv4 = (TextView)findViewById(R.id.textView6);
    tv5 = (TextView)findViewById(R.id.textView5);
    tex1 = (TextView)findViewById(R.id.textView1);
    text2 = (TextView)findViewById(R.id.textView4);

    btn = (Button)findViewById(R.id.button);
    pos = Integer.parseInt((getIntent().getExtras()).getString("pos"));

    tv1.setText(Women_clothing.gridData.get(pos).getDocumentName());

    tv3.setText(Women_clothing.gridData.get(pos).getDocumentContent());
    tv4.setText(Women_clothing.gridData.get(pos).getOffer());
    tv5.setText(Women_clothing.gridData.get(pos).getAddress());

    Picasso.with(getApplicationContext()).load((Women_clothing.gridData.get(pos)).getDocumentFile()).into(image);
}

1 个答案:

答案 0 :(得分:1)

试试以下

public static void colorizeToolbar(Toolbar toolbarView, int toolbarIconsColor, Activity activity) {
    final PorterDuffColorFilter colorFilter
            = new PorterDuffColorFilter(toolbarIconsColor, PorterDuff.Mode.MULTIPLY);

    for(int i = 0; i < toolbarView.getChildCount(); i++) {
        final View v = toolbarView.getChildAt(i);

        //Step 1 : Changing the color of back button (or open drawer button).
        if(v instanceof ImageButton) {
            //Action Bar back button
            ((ImageButton)v).getDrawable().setColorFilter(colorFilter);
        }

        if(v instanceof ActionMenuView) {
            for(int j = 0; j < ((ActionMenuView)v).getChildCount(); j++) {

                //Step 2: Changing the color of any ActionMenuViews - icons that
                //are not back button, nor text, nor overflow menu icon.
                final View innerView = ((ActionMenuView)v).getChildAt(j);

                if(innerView instanceof ActionMenuItemView) {
                    int drawablesCount = ((ActionMenuItemView)innerView).getCompoundDrawables().length;
                    for(int k = 0; k < drawablesCount; k++) {
                        if(((ActionMenuItemView)innerView).getCompoundDrawables()[k] != null) {
                            final int finalK = k;

                            //Important to set the color filter in seperate thread, 
                            //by adding it to the message queue
                            //Won't work otherwise.
                            innerView.post(new Runnable() {
                                @Override
                                public void run() {
                                    ((ActionMenuItemView) innerView).getCompoundDrawables()[finalK].setColorFilter(colorFilter);
                                }
                            });
                        }
                    }
                }
            }
        }

        //Step 3: Changing the color of title and subtitle.
        toolbarView.setTitleTextColor(toolbarIconsColor);
        toolbarView.setSubtitleTextColor(toolbarIconsColor);

        //Step 4: Changing the color of the Overflow Menu icon.
        setOverflowButtonColor(activity, colorFilter);
    }
}

请参阅此Example 1 Example 2