set ImageView Drawable Size Base On Height - Android Graphics

Android examples for Graphics:Drawable Size

Description

set ImageView Drawable Size Base On Height

Demo Code

/**// w  w w  .j a  v  a2 s .  co  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.ImageView;

public class Main {
    /**
     * Base on request new height we compute new width
     */
    public static void setImageViewDrawableSizeBaseOnHeight(
            ImageView imageView, int newHeight) {
        Drawable drawable = imageView.getDrawable();
        if (drawable == null)
            return;
        int curWidth = drawable.getIntrinsicWidth();
        int curHeight = drawable.getIntrinsicHeight();

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

        drawable.setBounds(0, 0, newWidth, newHeight);
        imageView.setImageDrawable(drawable);
    }
}

Related Tutorials