简体   繁体   中英

How to get width and height of an Image in viewpager?

is there a way to get the correct with and height of an image displayed in an viewpager?

I have tried using:

viewpager.getWidth() viewpager.getHeight()

but these return the viewpager width/height + margine.

I have also tried using adapter.getView().getWidth() and adapter.getView().getHeight(), but they didn't work either.

Edit: Added my Adapter Class and my Layout.

图片

My Adapter Class:

public class DetailViewPagerAdapter extends PagerAdapter {

private Fishton fishton;
private LayoutInflater inflater;
private Uri[] images;
private TouchImageView imageView;

public DetailViewPagerAdapter(LayoutInflater inflater, Uri[] images) {
    this.inflater = inflater;
    this.images = images;
    fishton = Fishton.getInstance();
}

public TouchImageView getView() {
    return imageView;
}


@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {

     View itemView = inflater.inflate(R.layout.detail_item, container, false);
     container.addView(itemView);

     imageView = itemView.findViewById(R.id.img_detail_image);

    if (imageView != null
            && images[position] != null)
        fishton
                .imageAdapter
                .loadDetailImage(imageView, images[position]);

    return itemView;
}

@Override
public int getCount() {
    return images.length;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    if (container instanceof ViewPager) {
        container.removeView((ConstraintLayout) object);
    }
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
 }
}

detail_item.xml

<com.sangcomz.fishbun.util.TouchImageView
        android:id="@+id/img_detail_image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginBottom="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5"/>

You're answering your own question. You want the width/height of the View inside the ViewPager. So you need the view.getWidth() and view.getHeight() inside the individual Fragment of your ViewPager

Edit:

Okay after you posted your code I think you want itemView.getWidth() and itemView.getHeight()

Also:

You can also achieve this by doing viewPager.getChildAt(0).getWidth()...

public Object instantiateItem(@NonNull ViewGroup container, int position) {

 View itemView = inflater.inflate(R.layout.detail_item, container, false);
 container.addView(itemView);

 imageView = itemView.findViewById(R.id.img_detail_image);

if (imageView != null
        && images[position] != null)
    fishton
            .imageAdapter
            .loadDetailImage(imageView, images[position]);
    //itemView.getWidth() 
    //itemView.getHeight()
   return itemView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM