Skip to content Skip to sidebar Skip to footer

I Need To Size The Imageview Inside My Table Row Programatically

I've tried to set my ImageView resized but it did not work either by bitmap or other methods I'm going to place my code so if can anyone help me how to size the ImageView to fit in

Solution 1:

try this,

publicclassResizableImageViewextendsImageView {
    publicResizableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    publicResizableImageView(Context context) {
        super(context);
    }

    @OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawabled= getDrawable();
        if (d == null) {
            super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
            return;
        }

        intimageHeight= d.getIntrinsicHeight();
        intimageWidth= d.getIntrinsicWidth();

        intwidthSize= MeasureSpec.getSize(widthMeasureSpec);
        intheightSize= MeasureSpec.getSize(heightMeasureSpec);

        floatimageRatio=0.0F;
        if (imageHeight > 0) {
            imageRatio = imageWidth / imageHeight;
        }
        floatsizeRatio=0.0F;
        if (heightSize > 0) {
            sizeRatio = widthSize / heightSize;
        }

        int width;
        int height;
        if (imageRatio >= sizeRatio) {
            // set width to maximum allowed
            width = widthSize;
            // scale height
            height = width * imageHeight / imageWidth;
        } else {
            // set height to maximum allowed
            height = heightSize;
            // scale width
            width = height * imageWidth / imageHeight;
        }

        setMeasuredDimension(width, height);
    }
}

use it in your layout as you would plain old ImageView, just change the tag, like this,

        <com.example.ResizableImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:padding="1px"
            android:scaleType="fitCenter"
            android:src="..." />

Solution 2:

setLayoutParams() should do the trick, in your case I believe you would need TableRow.LayoutParams, like so:

ImageViewimg=newImageView(this);
img.setLayoutParams(newTableRow.LayoutParams(w, h));

Edit

Try to set the Drawable before the setLayoutParams:

ImageViewimg=newImageView(this);
Drawabled= getResources().getDrawable(ImageArray[i]);
img.setImageDrawable(d);
img.setLayoutParams(newTableRow.LayoutParams(w, h));

Post a Comment for "I Need To Size The Imageview Inside My Table Row Programatically"