java - Expand ImageView to maximum possible maintaining aspect ratio in Android -


i have found lots of answers in site they not working or single cases. in case:

  • i load image , show imageview.
  • the width of imageview must maximum possible: parent layout width.
  • the height must 1 allows re-scale image maintaining aspect ratio.

how achieve this? here layout imageview inside (currently have been using fitxy image not preserve aspect ratio, if using adjustviewbounds:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/layoutpicturegalleryitem"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:padding="5dp"     android:background="#e0e0de"     android:orientation="vertical" >      <imageview         android:id="@+id/imageview"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:scaletype="fitxy"         tools:ignore="contentdescription" />      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="vertical"         android:background="#ffffff" >          <textview             android:id="@+id/textname"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="esto es una prueba para ver cómo queda el layout"             android:textcolor="#000000"             android:textsize="12sp"             android:typeface="serif"             android:padding="2dp" />          <textview             android:id="@+id/textdescription"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:visibility="gone"             android:padding="2dp" />      </linearlayout>  </linearlayout> 

use widget instead of imageview , let width match parent:

package com.myapp.widgets;  import android.content.context; import android.graphics.drawable.drawable; import android.util.attributeset; import android.widget.imageview;  public class resizableimageview extends imageview {  public resizableimageview(context context) {     super(context); }  public resizableimageview(context context, attributeset attrs) {     super(context, attrs); }  public resizableimageview(context context, attributeset attrs,         int defstyle) {     super(context, attrs, defstyle); }  @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {     drawable drawable = getdrawable();     if (drawable != null)     {         int width =  measurespec.getsize(widthmeasurespec);         int diw = drawable.getintrinsicwidth();         if (diw > 0)         {             int height = width * drawable.getintrinsicheight() / diw;             setmeasureddimension(width, height);         }         else             super.onmeasure(widthmeasurespec, heightmeasurespec);     }     else         super.onmeasure(widthmeasurespec, heightmeasurespec); } } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -