How to increase image size in Android

I am working on a project that requires me to implement two horizontal scrolls with an image between them . I would like to expand the image size to fill in the gap between scrolling. I looked at the sample code, but it seems it doesn’t mention the image size anywhere. I have attached the code below after the image describing my problem.

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; } } 

This is my main XML file.

 <?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> 

Below is my attribute file:

 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="GalleryTheme"> <attr name="android:galleryItemBackground" /> </declare-styleable> </resources> 
+4
source share
4 answers

Winner

The problem here is two problems:

  • The size of the ImageView is based on the parent, which means that it shares its width and height.

  • The image size changes according to the maximum size (width).

The easiest way to handle this is to install ScaleType for ImageView. There are several types of scaling, but the one you probably want is Center Crop. In code, this is done using view_name.setScaleType(ImageView.ScaleType.CENTER_CROP) . You can also do this in XML for your ImageView with the android:scaleType="centerCrop" attribute android:scaleType="centerCrop" . Keep in mind that this will resize your image and keep the aspect ratio, but it will crop the sides.

Just add the above attribute to your ImageView in your XML (perhaps under your layout) and it should do what you want.

Hope this helps,

Fuzzicallogic

+3
source

try under the code ...?

 ?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> 

and if that doesn't work, try setting the gallery height to some dp, it will work. thanks

+1
source

The size of the ImageView is based on the parent, which means that it shares its width and height. In addition, the image size changes according to the maximum size (width).

0
source

It looks like in the AddImgAdp class you set the height and width of the ImageView for specific pixel sizes.

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

I would personally place the layout as RelativeLayout, align the first gallery with the top, the second gallery with the bottom, and I have an ImageView installed to align with the second gallery. Then make an image:

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

If you set ImageView to fill_parent with LinearLayout, it will fill at the bottom of the screen and the second gallery will be disabled.

Also, be aware that by filling the entire space, you will most likely distort the image. Consider all resizing options: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

-1
source

Source: https://habr.com/ru/post/1391396/


All Articles