在运行时加载应用程序映像

时间:2013-07-02 08:38:59

标签: android android-layout android-imageview

我希望我的Android应用程序能够处理不同的皮肤。所以我计划改变我在应用程序中使用的所有图像和字体,用于不同的皮肤。

最好的方法是什么?如何使用适当的图像资源初始化所有图像视图?

P.S:我想在运行时更改皮肤,不想重新启动应用程序。

谢谢..

1 个答案:

答案 0 :(得分:0)

您可以创建一个ThemeManager类,您可以在其中管理主题

public class ThemeManager {

    static ThemeManager tmanage;
    int ThemeCode;
    int topBarBackground, bottomBarBackground, buttonBackground, textColor;
    ArrayList<ThemeWrapper> twrapper = new ArrayList<ThemeWrapper>();

    public static ThemeManager getInstance() {
        if (tmanage != null)
            return tmanage;
        else
            tmanage = new ThemeManager();
        return tmanage;
    }



    public void setThemeCode(int themeCode) {
        ThemeCode = themeCode;
        setBottomBarBackground(themeCode);
        setTopBarBackground(themeCode);
        setButtonBackground(themeCode);
        setTextColor(themeCode);
    }

    @SuppressWarnings("deprecation")
    public void setTheme(ArrayList<View> v) {

        for (int i = 0; i < v.size(); i++) {
            if (v.get(i) instanceof RelativeLayout) {
                RelativeLayout layout = (RelativeLayout) v.get(i);
                layout.setBackgroundColor(getTopBarBackground());
            }

            else if (v.get(i) instanceof Button) {
                Button layout = (Button) v.get(i);

                layout.setBackgroundDrawable(getShape(getButtonBackground()));
                layout.setPadding(5, 5, 5, 5);
                layout.setTextSize(12);
            }

            else if (v.get(i) instanceof LinearLayout) {
                LinearLayout layout = (LinearLayout) v.get(i);
                layout.setBackgroundColor(getBottomBarBackground());
            }

            else if (v.get(i) instanceof TextView) {
                TextView layout = (TextView) v.get(i);
                layout.setTextColor(getTextColor());
            }
        }
    }

    private Drawable getShape(int col){
        GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TL_BR, new int[] { col,
                col, col});
        gradientDrawable.setGradientType(GradientDrawable.RECTANGLE);
        gradientDrawable.setCornerRadii(getRandomFloatArray());
        gradientDrawable.setGradientCenter(0.0f, 0.45f);
        gradientDrawable.setStroke(1, Color.WHITE);
        gradientDrawable.setSize(60, 30);

        return gradientDrawable;
    }

    private float [] getRandomFloatArray(){

        float[] floats = new float[8];
        for (int i =0; i < floats.length; i++){
            floats[i] = 2;
        }
        return floats;
    }

    public int getTopBarBackground() {
        return topBarBackground;
    }

    public void setTopBarBackground(int topBarBackground) {
        this.topBarBackground = topBarBackground;
    }

    public int getBottomBarBackground() {
        return bottomBarBackground;
    }

    public void setBottomBarBackground(int bottomBarBackground) {
        this.bottomBarBackground = bottomBarBackground;
    }

    public int getButtonBackground() {
        return buttonBackground;
    }

    public void setButtonBackground(int buttonBackground) {
        this.buttonBackground = buttonBackground;
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }


}

我正在改变你可以设置drawables的视图的颜色,现在在你的活动中使用它onResume函数

private void changeColor() {
        // TODO Auto-generated method stub


ArrayList<View> v = new ArrayList<View>();
    v.add(topBar);
    v.add(bottomBar);
    v.add(updateBio);
    v.add(updateEvents);
    v.add(updateGallery);
    v.add(updatePersonalStyle);
    v.add(updateVip);
    v.add(updateStatus);
    v.add(NameLink);
    v.add(changeProfilePicture);

    ThemeManager theme = ThemeManager.getInstance();
    theme.setThemeCode(PreferenceManager.getDefaultSharedPreferences(
            getApplicationContext()).getInt("Theme_Color",
            Color.parseColor("#7606a5")));
    theme.setTheme(v);
}

只需在onResume

中调用此函数即可