Skip to content

Commit

Permalink
preload images
Browse files Browse the repository at this point in the history
contributes to #94
  • Loading branch information
niccokunzmann committed Mar 12, 2020
1 parent 2298ead commit 013cfef
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {
implementation 'commons-io:commons-io:2.4'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.12'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
*/
package org.androidsoft.coloring.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.DisplayMetrics;
import android.view.WindowManager;

import org.androidsoft.coloring.ui.widget.PreCachingLayoutManager;
import org.androidsoft.coloring.util.ScreenUtils;
import org.androidsoft.coloring.util.Settings;
import org.androidsoft.coloring.util.images.ImageDB;
Expand Down Expand Up @@ -50,6 +55,10 @@ public void onCreate(Bundle savedInstanceState)

setContentView(R.layout.choose_picture);
RecyclerView imagesView = findViewById(R.id.images);
// load images ahead
int space = getScreenHeight() / 2;
LinearLayoutManager manager = new PreCachingLayoutManager(this, space);
imagesView.setLayoutManager(manager);

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
Expand Down Expand Up @@ -99,4 +108,11 @@ public void onWindowFocusChanged(boolean hasFocus)
ScreenUtils.setFullscreen(this);
}
}

private int getScreenHeight() {
// from https://stackoverflow.com/a/4744499
DisplayMetrics displayMetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.androidsoft.coloring.ui.widget;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/* This layout manager loads images ahead.
* see https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager#setsmoothscrollbarenabled
* thanks to https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager#getextralayoutspace
* thanks to https://github.com/ovy9086/recyclerview-playground/blob/master/app/src/main/java/com/olu/recyclerview/widget/PreCachingLayoutManager.java
* thanks to https://androiddevx.wordpress.com/2014/12/05/recycler-view-pre-cache-views/
* thanks to https://github.com/facebook/fresco/issues/335#issuecomment-110280822
* also https://github.com/ovy9086/recyclerview-playground/blob/master/app/src/main/java/com/olu/recyclerview/fragments/CatsListFragment.java#L45
* deprecated, see https://developer.android.com/reference/androidx/recyclerview/widget/LinearLayoutManager?hl=en#getExtraLayoutSpace(androidx.recyclerview.widget.RecyclerView.State)
* see https://developer.android.com/reference/androidx/recyclerview/widget/LinearLayoutManager?hl=en#calculateExtraLayoutSpace(androidx.recyclerview.widget.RecyclerView.State,%20int%5B%5D)
*/
public class PreCachingLayoutManager extends LinearLayoutManager {
private final int extraLayoutSpace;

public PreCachingLayoutManager(Context context, int extraLayoutSpace) {
super(context);
this.extraLayoutSpace = extraLayoutSpace;
}

@Override
protected void calculateExtraLayoutSpace(@NonNull RecyclerView.State state, @NonNull int[] extraLayoutSpace) {
extraLayoutSpace[0] = this.extraLayoutSpace;
extraLayoutSpace[1] = this.extraLayoutSpace;
}
}
5 changes: 3 additions & 2 deletions src/main/res/layout/choose_picture_line.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
android:adjustViewBounds="true"
android:background="#FFFFFF"
android:scaleType="fitXY"
android:src="@drawable/ic_logo"
android:src="@drawable/download"
tools:ignore="ContentDescription" />

<ImageView
Expand All @@ -28,7 +28,8 @@
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#FFFFFF"
android:overScrollMode="never"
android:scaleType="fitXY"
android:src="@drawable/ic_logo"
android:src="@drawable/download"
tools:ignore="ContentDescription" />
</LinearLayout>

0 comments on commit 013cfef

Please sign in to comment.