android view pager image is stretched

1.3k views Asked by At
  • I have added my splash screen image as a window background image of my splash activity.
  • I have the same splash image as the first image in my walkthrough screen.
  • Walkthrough screen has a view pager with 5 imageviews.

Now I'm trying to add fadein animation from the splash screen to open the walkthrough activity.

Problem here is the image that i have used in the splash and walkthrough(1st pic) is same. But the image in walkthrough is getting stretched when compared to the splash.

How to show the images in walkthrough screen same as splash screen without getting stretched.

CODE: 1. My view pager code in walkthrough activity

mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(new ViewPagerAdapter(this, mWalkThroughPic));
  1. Viewpager adapter code

    public class ViewPagerAdapter extends PagerAdapter {
     Activity mActivity;
     int mImageArray[];
    
     public ViewPagerAdapter(Activity act, int[] imgArra) {
      mImageArray = imgArra;
      mActivity = act;
    
     }
    
     public int getCount() {
      return mImageArray.length;
     }
    
     public Object instantiateItem(View collection, int position) {
      ImageView view = new ImageView(mActivity);
      view.setScaleType(ScaleType.FIT_XY);
      view.setImageResource(mImageArray[position]);
      ((ViewPager) collection).addView(view, 0);
      return view;
     }
    
     @Override
     public void destroyItem(View arg0, int arg1, Object arg2) {
      ((ViewPager) arg0).removeView((View) arg2);
     }
    
     @Override
     public boolean isViewFromObject(View arg0, Object arg1) {
      return arg0 == ((View) arg1);
     }
    
     @Override
     public Parcelable saveState() {
      return null;
     }
    

    }

  2. Splash screen window background code used in splash theme :

    "<"style name="SplashTheme" parent="@android:style/Theme.NoTitleBar" "<"item name="android:windowBackground">@drawable/screen1"<"/item> "<"item name="android:windowNoTitle">true"<"/item> "<"/style> "

2

There are 2 answers

1
Mr Nice On

Get the screen size using the below code and use the same size image in all screen

DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    density=dm.densityDpi;      
    height = display.getHeight();
    width = display.getWidth();     
    System.out.println("width==>"+width+" "+"Height==>"+height+" "+"densityDpi==>"+dm.densityDpi);
1
Gabe On

Try adding android:scaleType="centerCrop" to the ImageView