Pregunta

Estoy trabajando en un proyecto que me requiere implementar Dos vistas de desplazamiento horizontal con un Vista de imagen en el medio a ellos. Me gustaría extender el tamaño de la vista de imagen para llenar el vacío entre la vista de desplazamiento. Revisé el código de muestra, pero no parece mencionar en ningún lugar del tamaño de la vista de imagen. He incluido el código a continuación después de la imagen que describe mi problema.

enter image description here

public class Gallery2DemoActivity extends Activity {
private Gallery gallery,gallery1;
private ImageView imgView;

private Integer[] Imgid = {
    R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));

gallery1 = (Gallery)findViewById(R.id.examplegallery1);
gallery1.setAdapter(new AddImgAdp(this));

imgView = (ImageView)findViewById(R.id.ImageView01);    
imgView.setImageResource(Imgid[0]);


 gallery.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        imgView.setImageResource(Imgid[position]); 
    }
});

}

public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;

public AddImgAdp(Context c) {
    cont = c;
    TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
    GalItemBg =  typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
    typArray.recycle();
}

public int getCount() {
    return Imgid.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imgView = new ImageView(cont);

    imgView.setImageResource(Imgid[position]);
    imgView.setLayoutParams(new Gallery.LayoutParams(110, 100));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(GalItemBg);

    return imgView;
}
}

Este es mi archivo principal XML.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<ImageView
    android:id="@+id/ImageView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"/>
<Gallery
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

A continuación se muestra mi archivo de atributos:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GalleryTheme">
    <attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
¿Fue útil?

Solución

Vinner,

El problema aquí es realmente dos problemas:

  1. El tamaño de ImageView se basa en el padre, lo que significa que comparte su ancho y altura.

  2. La imagen se redimensiona para que coincida con la dimensión más limitante (ancho).

La forma más fácil de manejar esto es establecer el tipo de escamas de ImageView. Hay varios tipos de escala, pero el que probablemente desee es Cosecha central. En código, esto se hace por view_name.setScaleType(ImageView.ScaleType.CENTER_CROP). También puede hacer esto en XML para su ImageView con el atributo android:scaleType="centerCrop". Eso sí, esto cambiará su imagen y mantendrá su relación de aspecto, pero cortará los lados.

Simplemente agregue el atributo anterior a su ImageView en su XML (probablemente debajo de su diseño_weight), y debe hacer lo que desee.

Espero que esto ayude,

Fuzzicallógico

Otros consejos

¿Probarás el siguiente código ...?

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1" />
<ImageView
android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="0dp" 
android:src="@drawable/ic_launcher"
android:layout_weight="1"/>
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/examplegallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"/>
</LinearLayout>

Y si no funciona, intente configurar la altura de su galería en algún DP, funcionará ... gracias

El tamaño de ImageView se basa en el padre, lo que significa que comparte su ancho y altura. Además, la imagen se redimensiona para que coincida con la dimensión más limitante (ancho).

Parece que en la clase AddImGAdp, establece la altura y el ancho de ImageView en tamaños específicos de píxeles.

imgView.setLayoutParams(new Gallery.LayoutParams(110, 100)); //width = "110px", height="100px"

Personalmente, pondría el diseño como un relativeLayout, alinearía la primera galería con la parte superior, la segunda galería con la parte inferior, y tiene el ImageView configurado para alinearse por encima de la segunda galería. Luego haz la imagen:

imgView.setLayoutParams( new Gallery.LayoutParams(
   Gallery.LayoutParams.FILL_PARENT, 
   Gallery.LayoutParams.FILL_PARENT) );

Si establece el ImageView en Fill_parent con un LinearLayout, se llenará en la parte inferior de la pantalla y la segunda galería estará fuera de la pantalla.

También sepa que al completar todo el espacio, probablemente distorsione la imagen. Considere todas sus opciones de cambio de tamaño: http://developer.android.com/reference/android/widget/imageview.scaletype.html

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top