diff options
author | Jorge Ruesga <jorge@ruesga.com> | 2013-08-14 21:24:09 +0200 |
---|---|---|
committer | Jorge Ruesga <jorge@ruesga.com> | 2013-08-14 21:24:09 +0200 |
commit | 9390e9c4ccd9521a29b925da3054c65da0fce6c8 (patch) | |
tree | 082d940986a4f88ff66bfba59c26352aa54ee984 /src/org/cyanogenmod | |
parent | dd000d67095ec3e76b59987b56b8fbe239227d47 (diff) | |
download | android_packages_wallpapers_PhotoPhase-9390e9c4ccd9521a29b925da3054c65da0fce6c8.tar.gz android_packages_wallpapers_PhotoPhase-9390e9c4ccd9521a29b925da3054c65da0fce6c8.tar.bz2 android_packages_wallpapers_PhotoPhase-9390e9c4ccd9521a29b925da3054c65da0fce6c8.zip |
Improve responsiveness of CardLayout
Signed-off-by: Jorge Ruesga <jorge@ruesga.com>
Diffstat (limited to 'src/org/cyanogenmod')
3 files changed, 203 insertions, 23 deletions
diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/ChoosePicturesFragment.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/ChoosePicturesFragment.java index 9d85ef9..322bcc5 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/ChoosePicturesFragment.java +++ b/src/org/cyanogenmod/wallpapers/photophase/preferences/ChoosePicturesFragment.java @@ -19,6 +19,7 @@ package org.cyanogenmod.wallpapers.photophase.preferences; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; +import android.content.res.Resources; import android.database.Cursor; import android.os.AsyncTask; import android.os.AsyncTask.Status; @@ -40,6 +41,8 @@ import org.cyanogenmod.wallpapers.photophase.preferences.PreferencesProvider.Pre import org.cyanogenmod.wallpapers.photophase.widgets.AlbumInfo; import org.cyanogenmod.wallpapers.photophase.widgets.AlbumPictures; import org.cyanogenmod.wallpapers.photophase.widgets.CardLayout; +import org.cyanogenmod.wallpapers.photophase.widgets.VerticalEndlessScroller; +import org.cyanogenmod.wallpapers.photophase.widgets.VerticalEndlessScroller.OnEndScrollListener; import java.io.File; import java.text.DateFormat; @@ -53,12 +56,14 @@ import java.util.Set; /** * A fragment class for select the picture that will be displayed on the wallpaper */ -public class ChoosePicturesFragment extends PreferenceFragment { +public class ChoosePicturesFragment extends PreferenceFragment implements OnEndScrollListener { private static final String TAG = "ChoosePicturesFragment"; private static final boolean DEBUG = false; + private static final int AMOUNT_OF_ADDED_STEPS = 5; + private final AsyncTask<Void, Album, Void> mAlbumsLoaderTask = new AsyncTask<Void, Album, Void>() { /** * {@inheritDoc} @@ -91,7 +96,6 @@ public class ChoosePicturesFragment extends PreferenceFragment { if (album != null) { mAlbums.add(album); mOriginalAlbums.add((Album)album.clone()); - this.publishProgress(album); } album = new Album(); album.setPath(path.getAbsolutePath()); @@ -114,7 +118,6 @@ public class ChoosePicturesFragment extends PreferenceFragment { if (album != null) { mAlbums.add(album); mOriginalAlbums.add((Album)album.clone()); - this.publishProgress(album); } } finally { @@ -128,23 +131,37 @@ public class ChoosePicturesFragment extends PreferenceFragment { * {@inheritDoc} */ @Override - protected void onProgressUpdate(Album... values) { - for (Album album : values) { - addAlbum(album); + protected void onPostExecute(Void result) { + Resources res = getActivity().getResources(); + int size = (int)(res.getDimension(R.dimen.album_size) + + res.getDimension(R.dimen.small_margin)); + mScroller.setEndPadding(size * AMOUNT_OF_ADDED_STEPS); + int height = mScroller.getMeasuredHeight(); + int steps = (height / size) + AMOUNT_OF_ADDED_STEPS; + + // Create the views an force a redraw the items + mAlbumViews = new ArrayList<View>(mAlbums.size()); + for (Album item : mAlbums) { + mAlbumViews.add(createAlbumView(item)); } + doEndScroll(steps, true); + + // Load in background + loadInBackground(1500L); } }; - /*package*/ ContentResolver mContentResolver; /*package*/ List<Album> mAlbums; + /*package*/ List<View> mAlbumViews; /*package*/ List<Album> mOriginalAlbums; /*package*/ List<AlbumsFlip3dAnimationController> mAnimationControllers; /*package*/ Set<String> mSelectedAlbums; private Set<String> mOriginalSelectedAlbums; + /*package*/ VerticalEndlessScroller mScroller; private CardLayout mAlbumsPanel; /*package*/ boolean mSelectionChanged; @@ -225,13 +242,16 @@ public class ChoosePicturesFragment extends PreferenceFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment - View v = inflater.inflate(R.layout.choose_picture_fragment, container, false); - mAlbumsPanel = (CardLayout) v.findViewById(R.id.albums_panel); + mScroller = + (VerticalEndlessScroller)inflater.inflate( + R.layout.choose_picture_fragment, container, false); + mScroller.setCallback(this); + mAlbumsPanel = (CardLayout)mScroller.findViewById(R.id.albums_panel); // Load the albums mAlbumsLoaderTask.execute(); - return v; + return mScroller; } /** @@ -325,11 +345,12 @@ public class ChoosePicturesFragment extends PreferenceFragment { } /** - * Method that adds a new album to the card layout + * Method that creates a new album to the card layout * - * @param album The album to add + * @param album The album to create + * @return View The view create */ - void addAlbum(Album album) { + View createAlbumView(Album album) { LayoutInflater li = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View albumView = li.inflate(R.layout.album, mAlbumsPanel, false); final AlbumInfo albumInfo = (AlbumInfo)albumView.findViewById(R.id.album_info); @@ -393,8 +414,7 @@ public class ChoosePicturesFragment extends PreferenceFragment { controller.register(); mAnimationControllers.add(controller); - // Add to the panel of cards - mAlbumsPanel.addCard(albumView); + return albumView; } /** @@ -431,4 +451,44 @@ public class ChoosePicturesFragment extends PreferenceFragment { } } } + + /** + * {@inheritDoc} + */ + @Override + public void onEndScroll() { + doEndScroll(AMOUNT_OF_ADDED_STEPS, false); + } + + /** + * Method that performs a scroll creating new items + * + * @param amount The amount of items to create + * @param animate If the add should be animated + */ + /*package*/ synchronized void doEndScroll(int amount, boolean animate) { + for (int i = 0; i < amount; i++) { + //Add to the panel of cards + if (mAlbumViews != null && !mAlbumViews.isEmpty()) { + mAlbumsPanel.addCard(mAlbumViews.remove(0), animate); + } + } + } + + /** + * Method that load albums in background + * + * @param delay The delay time + */ + /*package*/ void loadInBackground(long delay) { + mAlbumsPanel.postDelayed(new Runnable() { + @Override + public void run() { + doEndScroll(AMOUNT_OF_ADDED_STEPS, false); + if (mAlbumViews != null && !mAlbumViews.isEmpty()) { + loadInBackground(300L); + } + } + }, delay); + } } diff --git a/src/org/cyanogenmod/wallpapers/photophase/widgets/CardLayout.java b/src/org/cyanogenmod/wallpapers/photophase/widgets/CardLayout.java index 9204424..35971f6 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/widgets/CardLayout.java +++ b/src/org/cyanogenmod/wallpapers/photophase/widgets/CardLayout.java @@ -79,20 +79,23 @@ public class CardLayout extends LinearLayout { * Add a new card to the layout * * @param card The card view to add + * @param animate If the add should be animated */ - public void addCard(final View card) { + public void addCard(final View card, final boolean animate) { post(new Runnable() { @Override public void run() { addView(card); - if (inverted) { - card.startAnimation(AnimationUtils.loadAnimation( - getContext(), R.anim.cards_animation_up_right)); - } else { - card.startAnimation(AnimationUtils.loadAnimation( - getContext(), R.anim.cards_animation_up_left)); + if (animate) { + if (inverted) { + card.startAnimation(AnimationUtils.loadAnimation( + getContext(), R.anim.cards_animation_up_right)); + } else { + card.startAnimation(AnimationUtils.loadAnimation( + getContext(), R.anim.cards_animation_up_left)); + } + inverted = !inverted; } - inverted = !inverted; } }); } diff --git a/src/org/cyanogenmod/wallpapers/photophase/widgets/VerticalEndlessScroller.java b/src/org/cyanogenmod/wallpapers/photophase/widgets/VerticalEndlessScroller.java new file mode 100644 index 0000000..55e6811 --- /dev/null +++ b/src/org/cyanogenmod/wallpapers/photophase/widgets/VerticalEndlessScroller.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2013 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.cyanogenmod.wallpapers.photophase.widgets; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; +import android.widget.ScrollView; + +/** + * A scroll view that notifies the end of the scroll to create new views + * dynamically. + */ +public class VerticalEndlessScroller extends ScrollView { + + /** + * Interface to communicate end-scroll events + */ + public interface OnEndScrollListener { + /** + * Called when the scroll reachs the end of the scroll + */ + void onEndScroll(); + } + + private OnEndScrollListener mCallback; + private int mEndPadding = 0; + private boolean mSwitch = false; + + /** + * Constructor of <code>VerticalEndlessScroller</code>. + * + * @param context The current context + */ + public VerticalEndlessScroller(Context context) { + super(context); + } + + /** + * Constructor of <code>VerticalEndlessScroller</code>. + * + * @param context The current context + * @param attrs The attributes of the XML tag that is inflating the view. + */ + public VerticalEndlessScroller(Context context, AttributeSet attrs) { + super(context, attrs); + } + + /** + * Constructor of <code>VerticalEndlessScroller</code>. + * + * @param context The current context + * @param attrs The attributes of the XML tag that is inflating the view. + * @param defStyle The default style to apply to this view. If 0, no style + * will be applied (beyond what is included in the theme). This may + * either be an attribute resource, whose value will be retrieved + * from the current theme, or an explicit style resource. + */ + public VerticalEndlessScroller(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + /** + * Method that set the callback for notify end-scroll events + * + * @param callback The callback + */ + public void setCallback(OnEndScrollListener callback) { + mCallback = callback; + } + + /** + * Method that set the end padding for fired the event + * + * @param endPadding The end padding + */ + public void setEndPadding(int endPadding) { + this.mEndPadding = endPadding; + } + + /** + * {@inheritDoc} + */ + @Override + protected void onScrollChanged(int l, int t, int oldl, int oldt) { + // We take the last son in the scrollview + View view = getChildAt(getChildCount() - 1); + int diff = (view.getBottom() - (getHeight() + getScrollY())); + if ((!mSwitch && diff <= mEndPadding)) { + if (mCallback != null) { + mCallback.onEndScroll(); + mSwitch = true; + return; + } + } else if (diff > mEndPadding) { + mSwitch = false; + } + if (!mSwitch) { +// super.onScrollChanged(l, t, oldl, oldt); + } + } + +} |