set Button Compound Drawable Size Base On Height - Android Graphics

Android examples for Graphics:Drawable

Description

set Button Compound Drawable Size Base On Height

Demo Code

/**//w w w  .  jav  a2  s  .c o  m
 * UmbalaApp
 *
 * Created by Hoang Anh on 8/10/15.
 * Copyright (c) 2015 Umbala. All rights reserved.
 */
//package com.java2s;

import android.graphics.drawable.Drawable;

import android.widget.Button;

public class Main {
    /**
     * Base on request new height we compute new width
     */
    public static void setCompoundDrawableSizeBaseOnHeight(Button button,
            int newHeight) {
        Drawable[] drawables = button.getCompoundDrawables();
        for (Drawable drawable : drawables) {
            if (drawable == null)
                continue;
            int curWidth = drawable.getIntrinsicWidth();
            int curHeight = drawable.getIntrinsicHeight();

            float scale = 1f * newHeight / curHeight;
            int newWidth = (int) (scale * curWidth);

            drawable.setBounds(0, 0, newWidth, newHeight);
        }
        button.setCompoundDrawables(drawables[0], drawables[1],
                drawables[2], drawables[3]);
    }
}

Related Tutorials