Created
April 15, 2014 12:01
-
-
Save CoffeeCode/10726601 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ap.wificam; | |
import android.app.ActionBar; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.RectF; | |
import android.graphics.drawable.Drawable; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.provider.MediaStore; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.text.Spannable; | |
import android.text.SpannableString; | |
import android.util.TypedValue; | |
import android.view.View; | |
import android.view.animation.AccelerateDecelerateInterpolator; | |
import android.widget.AbsListView; | |
import android.widget.ArrayAdapter; | |
import android.widget.ImageView; | |
import android.widget.ListView; | |
import com.ap.wificam.ImageDetails.MyFragment; | |
import com.astuetz.PagerSlidingTabStrip; | |
import com.flavienlaurent.notboringactionbar.AlphaForegroundColorSpan; | |
import com.flavienlaurent.notboringactionbar.KenBurnsView; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class NotBoringActionbar extends FragmentActivity { | |
private static final String TAG = "NotBoringActionBar"; | |
private int mActionBarTitleColor; | |
private int mActionBarHeight; | |
private int mHeaderHeight; | |
private int mMinHeaderTranslation; | |
private ListView mListView; | |
private KenBurnsView mHeaderPicture; | |
private ImageView mHeaderLogo; | |
private View mHeader; | |
private View mPlaceHolderView; | |
private AccelerateDecelerateInterpolator mSmoothInterpolator; | |
private RectF mRect1 = new RectF(); | |
private RectF mRect2 = new RectF(); | |
private AlphaForegroundColorSpan mAlphaForegroundColorSpan; | |
private SpannableString mSpannableString; | |
private TypedValue mTypedValue = new TypedValue(); | |
//TabPageSlider | |
private final Handler handler = new Handler(); | |
private PagerSlidingTabStrip tabs; | |
private ViewPager pager; | |
private MyPageAdapter pagerAdapter; | |
private Drawable oldBackground = null; | |
private int currentColor = 0xFF666666; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_not_boring_actionbar); | |
/* | |
//Setup TabPageSlider | |
List<Fragment> fragments = getFragments(); | |
pagerAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments); | |
pager = (ViewPager) findViewById(R.id.viewpager); | |
pager.setAdapter(pagerAdapter); | |
*/ | |
//Setup Actionbar | |
String imagePath = getIntent().getExtras().getString("imagePath"); | |
Bitmap headerImage = decodeActionBarImage(imagePath); | |
File headerImageFile = new File(imagePath); | |
mSmoothInterpolator = new AccelerateDecelerateInterpolator(); | |
mHeaderHeight = getResources().getDimensionPixelSize(R.dimen.header_height); | |
mMinHeaderTranslation = -mHeaderHeight + getActionBarHeight(); | |
mListView = (ListView) findViewById(R.id.listview); | |
mHeader = findViewById(R.id.header); | |
mHeaderPicture = (KenBurnsView) findViewById(R.id.header_picture); | |
mHeaderPicture.setBitmap(headerImage); | |
mHeaderLogo = (ImageView) findViewById(R.id.header_logo); | |
mActionBarTitleColor = getResources().getColor(R.color.actionbar_title_color); | |
mSpannableString = new SpannableString(headerImageFile.getName()); | |
mAlphaForegroundColorSpan = new AlphaForegroundColorSpan(mActionBarTitleColor); | |
setupActionBar(); | |
setupListView(); | |
} | |
private Bitmap decodeActionBarImage(String imagePath) { | |
final Uri thumbUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI; | |
final String thumb_IMAGE_ID = MediaStore.Images.Thumbnails.IMAGE_ID; | |
final String thumb_DATA = MediaStore.Images.Thumbnails.DATA; | |
String[] thumbColumns = {thumb_DATA, thumb_IMAGE_ID}; | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeFile(imagePath, options); | |
return decodeSampledBitmapFromFile(imagePath, 800, 800); | |
} | |
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) { | |
// Calculate ratios of height and width to requested height and width | |
final int heightRatio = Math.round((float) height / (float) reqHeight); | |
final int widthRatio = Math.round((float) width / (float) reqWidth); | |
// Choose the smallest ratio as inSampleSize value, this will guarantee | |
// a final image with both dimensions larger than or equal to the | |
// requested height and width. | |
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; | |
} | |
return inSampleSize; | |
} | |
public static Bitmap decodeSampledBitmapFromFile(String file, int reqWidth, int reqHeight) { | |
// First decode with inJustDecodeBounds=true to check dimensions | |
final BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeFile(file, options); | |
// Calculate inSampleSize | |
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); | |
// Decode bitmap with inSampleSize set | |
options.inJustDecodeBounds = false; | |
return BitmapFactory.decodeFile(file, options); | |
} | |
private void setupListView() { | |
ArrayList<String> FAKES = new ArrayList<String>(); | |
for (int i = 0; i < 1000; i++) { | |
FAKES.add("entry " + i); | |
} | |
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false); | |
mListView.addHeaderView(mPlaceHolderView); | |
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES)); | |
mListView.setOnScrollListener(new AbsListView.OnScrollListener() { | |
@Override | |
public void onScrollStateChanged(AbsListView view, int scrollState) { | |
} | |
@Override | |
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | |
int scrollY = getScrollY(); | |
//sticky actionbar | |
mHeader.setTranslationY(Math.max(-scrollY, mMinHeaderTranslation)); | |
//header_logo --> actionbar icon | |
float ratio = clamp(mHeader.getTranslationY() / mMinHeaderTranslation, 0.0f, 1.0f); | |
interpolate(mHeaderLogo, getActionBarIconView(), mSmoothInterpolator.getInterpolation(ratio)); | |
//actionbar title alpha | |
//getActionBarTitleView().setAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F)); | |
//--------------------------------- | |
//better way thanks to @cyrilmottier | |
setTitleAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F)); | |
} | |
}); | |
} | |
private void setTitleAlpha(float alpha) { | |
mAlphaForegroundColorSpan.setAlpha(alpha); | |
mSpannableString.setSpan(mAlphaForegroundColorSpan, 0, mSpannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); | |
getActionBar().setTitle(mSpannableString); | |
} | |
public static float clamp(float value, float max, float min) { | |
return Math.max(Math.min(value, min), max); | |
} | |
private void interpolate(View view1, View view2, float interpolation) { | |
getOnScreenRect(mRect1, view1); | |
getOnScreenRect(mRect2, view2); | |
float scaleX = 1.0F + interpolation * (mRect2.width() / mRect1.width() - 1.0F); | |
float scaleY = 1.0F + interpolation * (mRect2.height() / mRect1.height() - 1.0F); | |
float translationX = 0.5F * (interpolation * (mRect2.left + mRect2.right - mRect1.left - mRect1.right)); | |
float translationY = 0.5F * (interpolation * (mRect2.top + mRect2.bottom - mRect1.top - mRect1.bottom)); | |
view1.setTranslationX(translationX); | |
view1.setTranslationY(translationY - mHeader.getTranslationY()); | |
view1.setScaleX(scaleX); | |
view1.setScaleY(scaleY); | |
} | |
private RectF getOnScreenRect(RectF rect, View view) { | |
rect.set(view.getLeft(), view.getTop(), view.getRight(), view.getBottom()); | |
return rect; | |
} | |
public int getScrollY() { | |
View c = mListView.getChildAt(0); | |
if (c == null) { | |
return 0; | |
} | |
int firstVisiblePosition = mListView.getFirstVisiblePosition(); | |
int top = c.getTop(); | |
int headerHeight = 0; | |
if (firstVisiblePosition >= 1) { | |
headerHeight = mPlaceHolderView.getHeight(); | |
} | |
return -top + firstVisiblePosition * c.getHeight() + headerHeight; | |
} | |
private void setupActionBar() { | |
ActionBar actionBar = getActionBar(); | |
actionBar.setIcon(R.drawable.ic_transparent); | |
//getActionBarTitleView().setAlpha(0f); | |
} | |
private ImageView getActionBarIconView() { | |
return (ImageView) findViewById(android.R.id.home); | |
} | |
/*private TextView getActionBarTitleView() { | |
int id = Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); | |
return (TextView) findViewById(id); | |
}*/ | |
public int getActionBarHeight() { | |
if (mActionBarHeight != 0) { | |
return mActionBarHeight; | |
} | |
getTheme().resolveAttribute(android.R.attr.actionBarSize, mTypedValue, true); | |
mActionBarHeight = TypedValue.complexToDimensionPixelSize(mTypedValue.data, getResources().getDisplayMetrics()); | |
return mActionBarHeight; | |
} | |
private List<Fragment> getFragments() { | |
List<Fragment> fList = new ArrayList<Fragment>(); | |
fList.add(MyFragment.newInstance("Fragment 1")); | |
fList.add(MyFragment.newInstance("Fragment 2")); | |
fList.add(MyFragment.newInstance("Fragment 3")); | |
return fList; | |
} | |
class MyPageAdapter extends FragmentPagerAdapter { | |
private List<Fragment> fragments; | |
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) { | |
super(fm); | |
this.fragments = fragments; | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return this.fragments.get(position); | |
} | |
@Override | |
public int getCount() { | |
return this.fragments.size(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment