Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.content.Context;

import android.content.res.ColorStateList;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AnalogClock;
import android.widget.ImageView;
import android.widget.TextView;

import java.lang.reflect.Field;

public class Main {
    public static void traverseAndRecolor(View root, int color, boolean withStates,
            boolean setClickableItemBackgrounds) {
        Context context = root.getContext();

        if (setClickableItemBackgrounds && root.isClickable()) {
            StateListDrawable selectableItemBackground = new StateListDrawable();
            selectableItemBackground.addState(new int[] { android.R.attr.state_pressed },
                    new ColorDrawable((color & 0xffffff) | 0x33000000));
            selectableItemBackground.addState(new int[] { android.R.attr.state_focused },
                    new ColorDrawable((color & 0xffffff) | 0x44000000));
            selectableItemBackground.addState(new int[] {}, null);
            root.setBackground(selectableItemBackground);
        }

        if (root instanceof ViewGroup) {
            ViewGroup parent = (ViewGroup) root;
            for (int i = 0; i < parent.getChildCount(); i++) {
                traverseAndRecolor(parent.getChildAt(i), color, withStates, setClickableItemBackgrounds);
            }

        } else if (root instanceof ImageView) {
            ImageView imageView = (ImageView) root;
            Drawable sourceDrawable = imageView.getDrawable();
            if (withStates && sourceDrawable != null && sourceDrawable instanceof BitmapDrawable) {
                imageView.setImageDrawable(
                        makeRecoloredDrawable(context, (BitmapDrawable) sourceDrawable, color, true));
            } else {
                imageView.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
            }

        } else if (root instanceof TextView) {
            TextView textView = (TextView) root;
            if (withStates) {
                int sourceColor = textView.getCurrentTextColor();
                ColorStateList colorStateList = new ColorStateList(
                        new int[][] { new int[] { android.R.attr.state_pressed },
                                new int[] { android.R.attr.state_focused }, new int[] {} },
                        new int[] { sourceColor, sourceColor, color });
                textView.setTextColor(colorStateList);
            } else {
                textView.setTextColor(color);
            }

        } else if (root instanceof AnalogClock) {
            AnalogClock analogClock = (AnalogClock) root;
            try {
                Field hourHandField = AnalogClock.class.getDeclaredField("mHourHand");
                hourHandField.setAccessible(true);
                Field minuteHandField = AnalogClock.class.getDeclaredField("mMinuteHand");
                minuteHandField.setAccessible(true);
                Field dialField = AnalogClock.class.getDeclaredField("mDial");
                dialField.setAccessible(true);
                BitmapDrawable hourHand = (BitmapDrawable) hourHandField.get(analogClock);
                if (hourHand != null) {
                    Drawable d = makeRecoloredDrawable(context, hourHand, color, withStates);
                    d.setCallback(analogClock);
                    hourHandField.set(analogClock, d);
                }
                BitmapDrawable minuteHand = (BitmapDrawable) minuteHandField.get(analogClock);
                if (minuteHand != null) {
                    Drawable d = makeRecoloredDrawable(context, minuteHand, color, withStates);
                    d.setCallback(analogClock);
                    minuteHandField.set(analogClock, d);
                }
                BitmapDrawable dial = (BitmapDrawable) dialField.get(analogClock);
                if (dial != null) {
                    Drawable d = makeRecoloredDrawable(context, dial, color, withStates);
                    d.setCallback(analogClock);
                    dialField.set(analogClock, d);
                }
            } catch (NoSuchFieldException ignored) {
            } catch (IllegalAccessException ignored) {
            } catch (ClassCastException ignored) {
            } // TODO: catch all exceptions?
        }
    }

    public static Drawable makeRecoloredDrawable(Context context, BitmapDrawable drawable, int color,
            boolean withStates) {
        Bitmap recoloredBitmap = recolorBitmap(drawable, color);
        BitmapDrawable recoloredDrawable = new BitmapDrawable(context.getResources(), recoloredBitmap);

        if (!withStates) {
            return recoloredDrawable;
        }

        StateListDrawable stateDrawable = new StateListDrawable();
        stateDrawable.addState(new int[] { android.R.attr.state_pressed }, drawable);
        stateDrawable.addState(new int[] { android.R.attr.state_focused }, drawable);
        stateDrawable.addState(new int[] {}, recoloredDrawable);
        return stateDrawable;
    }

    public static Bitmap recolorBitmap(Drawable drawable, int color) {
        if (drawable == null) {
            return null;
        }

        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        if (width <= 0 || height <= 0) {
            return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        }

        Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outBitmap);
        drawable.setBounds(0, 0, outBitmap.getWidth(), outBitmap.getHeight());
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        drawable.draw(canvas);
        drawable.setColorFilter(null);
        drawable.setCallback(null); // free up any references
        return outBitmap;
    }
}