Reputation: 3
Hi I have a swipe view with corresponding tabs using fragments. The first view only contains a full hd image in the size of about 1.91mb.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/my_picture"
android:src="@drawable/image1" />
</LinearLayout>
Now when I swipe from Page 1 to Page 2 or the other Way around the animation seems really laggy.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch(position) {
case 0:
fragment = new Tab_one();
break;
case 1:
fragment = new Tab_two();
break;
case 2:
fragment = new Tab_three();
break;
default:
fragment = new Tab_one();
break;
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
}
So the transition from page1(contains only the imageview) to page2(contains a few textviews with information) is laggy. Can someone help me there? And does the filesize promote the lagging?
Upvotes: 0
Views: 1554
Reputation: 18977
Your image size is not important, your image resolution is important because in order to show an image for example in ARGB_8888
format each pixel will take 4 bytes so quick analyses will yield :
image with different size:
image: 1024 * 768 * 4 = 2MB
image: 1920 * 1080* 4 = 6MB
image: 1280 * 720 * 4 = 3MB
and in your computer all of them might be less than 1 MB. so what should you do? you must scale down your image and then assign to your image view, take a look at
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
Loading Large Bitmaps Efficiently
you can use decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
to loading your image.
Upvotes: 1