improved media gallery

This commit is contained in:
Mariotaku Lee 2015-02-18 19:13:23 +08:00
parent 9d469b80c6
commit 3dce8d9a9f
4 changed files with 123 additions and 6 deletions

View File

@ -25,11 +25,14 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLayoutChangeListener;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;
import android.widget.ProgressBar;
import com.diegocarloslima.byakugallery.lib.TileBitmapDrawable;
@ -47,11 +50,13 @@ import org.mariotaku.twidere.model.ParcelableMedia;
import org.mariotaku.twidere.util.ThemeUtils;
import org.mariotaku.twidere.util.Utils;
import org.mariotaku.twidere.view.TouchImageView;
import org.mariotaku.twidere.view.TouchImageView.ZoomListener;
public final class MediaViewerActivity extends ThemedActionBarActivity implements Constants {
public final class MediaViewerActivity extends ThemedActionBarActivity implements Constants, OnPageChangeListener {
private ViewPager mViewPager;
private MediaPagerAdapter mAdapter;
private ActionBar mActionBar;
@Override
public int getThemeColor() {
@ -63,13 +68,31 @@ public final class MediaViewerActivity extends ThemedActionBarActivity implement
return ThemeUtils.getViewerThemeResource(this);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
setBarVisibility(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_media_viewer);
mAdapter = new MediaPagerAdapter(this);
mViewPager.setAdapter(mAdapter);
mViewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.element_spacing_normal));
mViewPager.setOnPageChangeListener(this);
final Intent intent = getIntent();
final long accountId = intent.getLongExtra(EXTRA_ACCOUNT_ID, -1);
final ParcelableMedia[] media = Utils.newParcelableArray(intent.getParcelableArrayExtra(EXTRA_MEDIA), ParcelableMedia.CREATOR);
@ -87,8 +110,19 @@ public final class MediaViewerActivity extends ThemedActionBarActivity implement
mViewPager = (ViewPager) findViewById(R.id.view_pager);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
finish();
return true;
}
}
return super.onOptionsItemSelected(item);
}
public static final class MediaPageFragment extends BaseSupportFragment
implements DownloadListener, LoaderCallbacks<Result>, OnLayoutChangeListener {
implements DownloadListener, LoaderCallbacks<Result>, OnLayoutChangeListener, OnClickListener, ZoomListener {
private TouchImageView mImageView;
private ProgressBar mProgressBar;
@ -102,6 +136,12 @@ public final class MediaViewerActivity extends ThemedActionBarActivity implement
mProgressBar = (ProgressBar) view.findViewById(R.id.progress);
}
@Override
public void onClick(View v) {
final MediaViewerActivity activity = (MediaViewerActivity) getActivity();
activity.toggleBar();
}
@Override
public Loader<Result> onCreateLoader(final int id, final Bundle args) {
mProgressBar.setVisibility(View.VISIBLE);
@ -160,6 +200,8 @@ public final class MediaViewerActivity extends ThemedActionBarActivity implement
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mImageView.setOnClickListener(this);
mImageView.setZoomListener(this);
loadImage();
}
@ -207,6 +249,18 @@ public final class MediaViewerActivity extends ThemedActionBarActivity implement
}
@Override
public void onZoomOut() {
final MediaViewerActivity activity = (MediaViewerActivity) getActivity();
activity.setBarVisibility(true);
}
@Override
public void onZoomIn() {
final MediaViewerActivity activity = (MediaViewerActivity) getActivity();
activity.setBarVisibility(false);
}
private void loadImage() {
getLoaderManager().destroyLoader(0);
if (!mLoaderInitialized) {
@ -231,6 +285,26 @@ public final class MediaViewerActivity extends ThemedActionBarActivity implement
}
}
private void toggleBar() {
setBarVisibility(!isBarShowing());
}
private void setBarVisibility(boolean visible) {
if (mActionBar == null) return;
if (visible) {
mActionBar.show();
} else {
mActionBar.hide();
}
}
private boolean isBarShowing() {
if (mActionBar == null) return false;
return mActionBar.isShowing();
}
private static class MediaPagerAdapter extends SupportFixedFragmentStatePagerAdapter {
private final MediaViewerActivity mActivity;

View File

@ -61,6 +61,7 @@ public class TouchImageView extends ImageView {
private final FlingScroller mFlingScroller = new FlingScroller();
private boolean mIsAnimatingBack;
private ZoomListener mZoomListener;
public TouchImageView(Context context) {
this(context, null);
@ -91,7 +92,8 @@ public class TouchImageView extends ImageView {
final float minScale = getMinScale();
// If we have already zoomed in, we should return to our initial scale value (minScale). Otherwise, scale to full size
final float targetScale = mScale > minScale ? minScale : mMaxScale;
final boolean shouldZoomOut = mScale > minScale;
final float targetScale = shouldZoomOut ? minScale : mMaxScale;
// First, we try to keep the focused point in the same position when the animation ends
final float desiredTranslationX = e.getX() - (e.getX() - mTranslationX) * (targetScale / mScale);
@ -106,6 +108,13 @@ public class TouchImageView extends ImageView {
animation.setDuration(DOUBLE_TAP_ANIMATION_DURATION);
startAnimation(animation);
if (mZoomListener != null) {
if (shouldZoomOut) {
mZoomListener.onZoomOut();
} else {
mZoomListener.onZoomIn();
}
}
return true;
}
@ -511,4 +520,14 @@ public class TouchImageView extends ImageView {
ViewCompat.postInvalidateOnAnimation(TouchImageView.this);
}
}
public void setZoomListener(ZoomListener listener) {
mZoomListener = listener;
}
public static interface ZoomListener {
void onZoomOut();
void onZoomIn();
}
}

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Twidere - Twitter client for Android
~
~ Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#80000000"/>
</shape>

View File

@ -27,8 +27,8 @@
</style>
<style name="Widget.Twidere.Viewer.ActionBar" parent="Widget.AppCompat.ActionBar">
<item name="android:background">#80000000</item>
<item name="android:backgroundSplit">#80000000</item>
<item name="background">@drawable/bg_viewer_actionbar</item>
<item name="backgroundSplit">@drawable/bg_viewer_actionbar</item>
</style>
<style name="Widget.Twidere.ImageButton.Borderless" parent="Widget.Base.ImageButton">