2013-05-12 29 views
59

İçinde ImageViews ile bir GridView var. Her sıra için 3 tane var. WRAP_CONTENT ve scaleType = CENTER_CROP ile genişliği doğru olarak ayarlayabilirim, ancak ImageView'ın boyutunu bir kare olarak nasıl ayarlayacağımı bilmiyorum. İşte o "statik" olduğunu, bu yükseklikte hariç ok gibi görünüyor ne şimdiye kadar yaptığımız var: Ben bir adaptör içine yapıyorumImageView, dinamik genişliğe sahip bir kare mi?

imageView = new ImageView(context);  
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, 300)); 

.

cevap

140

iyi seçenek tedbir geçiş geçersiz kılma, ImageView kendini alt sınıfı için geçerli:

public class SquareImageView extends ImageView { 

    ... 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int width = getMeasuredWidth(); 
    setMeasuredDimension(width, width); 
    } 

    ... 

} 
+0

Awesome! Çok teşekkür ederim, bu olasılık hakkında düşünmedim –

+0

Eğer padding // marjı ayarlıyorum, beyaz bir kenarlık olsun .. Bunu nasıl düzeltebilirim? –

+2

android: adjustViewBounds = "true" android: scaleType = "centerCrop" @Waza_Be –

90

diğer cevap gayet iyi çalışıyor. Bu, xml şişirilmiş düzenine göre kare genişliği ve yüksekliği ile bir ImageView yapmak için bertucci çözümünün bir uzantısıdır. dinamik programda oluşturulan gereken bir ImageView gerekiyorsa

bir sınıf oluşturma

 <com.packagepath.tothis.SquareImageView 
      android:id="@+id/Imageview" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

, sizin xml bunu, şimdi böyle ImageView uzanan bir SquareImageView,

public class SquareImageView extends ImageView { 

    public SquareImageView(Context context) { 
     super(context); 
    } 

    public SquareImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int width = getMeasuredWidth(); 
     setMeasuredDimension(width, width); 
    } 

} 

demek bunun yerine, xml olarak düzeltilmek üzere, bu uygulama yararlı olacaktır. Daha basit

+1

Harika, teşekkürler Andro. Tam olarak aradığım şey bu. Yine, tam bir cevap vermek için zaman ayırdığınız için teşekkürler. – Taliadon

+1

Bu, doğru ve eksiksiz bir çözüm olarak işaretlenmelidir.sdk> = 21 @TargetApi (Build.VERSION_CODES.LOLLIPOP) kamu SquareImageView (Bağlam Bağlam, öznitelik attrs, int defStyleAttr, int defStyleRes) { super (bağlam, attrs için unutma – JRod

+0

, defStyleAttr, defStyleRes); } –

23

: Önceki cevapların

public class SquareImageView extends ImageView { 
    ... 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
    }  
} 
+0

benim için çalıştı –

+1

@JanRabe Thx, güncellendi. – mixel

10

Çeşitli mükemmel yeterlidir. Sadece @Andro Selva ve @ a.bertucci'nin çözümlerine küçük bir optimizasyon ekliyorum:

Küçük bir optimizasyon, ancak genişlik ve yüksekliğin farklı olduğunu kontrol etmek başka bir ölçüm geçişini engelleyebilir.

public class SquareImageView extends ImageView { 

    public SquareImageView(Context context) { 
     super(context); 
    } 

    public SquareImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, widthMeasureSpec); 

     int width = getMeasuredWidth(); 
     int height = getMeasuredHeight(); 

     // Optimization so we don't measure twice unless we need to 
     if (width != height) { 
      setMeasuredDimension(width, width); 
     } 
    } 

} 
-1

İşte bütün onMeasure için üst sınıf çağıran gereksizdir. İşte benim uygulamam

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int width = MeasureSpec.getSize(widthMeasureSpec); 
     int height = MeasureSpec.getSize(heightMeasureSpec); 
     int size = Math.min(width, height); 
     setMeasuredDimension(size, size); 
    } 
İlgili konular