SORU
5 NİSAN 2011, Salı


Ayarlama üst Android İmageView's yükseklik ve genişlik uygun

Güncelleme: Yöntem this answer açıklanan kullanarak bu sorunu çözdüm

Biraz oldukça basit olmalı bence bu konu ile kaldım.

Benim app bir resim indirme ve bir İmageView, bir RelativeLayout bir alt öğesi bit eşlem oluşturur. Bu İmageView üst sığdırmak için, ve bu boyut en boy oranını korumak için adapte etmek istiyorum.

İşte benim XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:id="@ id/banner" android:layout_width="fill_parent" android:layout_height="wrap_content"></RelativeLayout>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
</LinearLayout>

Ve kodu :

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    RelativeLayout banner = (RelativeLayout) findViewById(R.id.banner);
    ImageView imgV = new ImageView(this);

    imgV.setScaleType(ImageView.ScaleType.CENTER_CROP);
    // I tried all the scale types : CENTER_INSIDE : same effect, FIT_CENTER : same effect... 

    imgV.setBackgroundColor(0x00FFFF00);

    imgV.setAdjustViewBounds(Color.BLUE);


    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    banner.addView(imgV,params);

    // Some code downloading the image stream

    bitmap = BitmapFactory.decodeStream(stream);


    imgV.setImageBitmap(bitmap);

    }

İstenen :

Desired result

Sonuç :

Current result

CEVAP
5 EYLÜL 2012, ÇARŞAMBA


Teşekkürler @Julien ve @js için. Burada ise bitmap İmageView küçükse bile bitmap yüksekliği koruyarak En-Boy oranı streç İmageView Tam Çözüm.

public class ResizableImageView extends ImageView {

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

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
         Drawable d = getDrawable();

         if(d!=null){
                 // ceil not round - avoid thin vertical gaps along the left/right edges
                 int width = MeasureSpec.getSize(widthMeasureSpec);
                 int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
                 setMeasuredDimension(width, height);
         }else{
                 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         }
    }

}

Xml düzenleri bu sınıf yerine İmageView kullanabilirsiniz.

<com.example.ResizableImageView
    android:id="@ id/banner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/banner" />

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Angry Paintballer

    Angry Paintb

    8 Ocak 2012
  • Justin Schenck

    Justin Schen

    24 Kasım 2006
  • NCIX Tech Tips

    NCIX Tech Ti

    2 Ocak 2007