Skip to content

Commit

Permalink
Update version to 1.0.1, Support dynamic change title
Browse files Browse the repository at this point in the history
  • Loading branch information
hearsilent committed Mar 8, 2017
1 parent 332309a commit 51b0047
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "hearsilent.amazingavatar"
minSdkVersion 16
targetSdkVersion 25
versionCode 100
versionName "1.0.0"
versionCode 101
versionName "1.0.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
69 changes: 50 additions & 19 deletions app/src/main/java/hearsilent/amazingavatar/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package hearsilent.amazingavatar;

import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.Space;
Expand Down Expand Up @@ -40,10 +42,12 @@ public class MainActivity extends AppCompatActivity {
private Toolbar mToolBar;

private RecyclerView mRecyclerView;
private AppBarStateChangeListener mAppBarStateChangeListener;

private int[] mAvatarPoint = new int[2], mSpacePoint = new int[2], mToolbarTextPoint =
new int[2], mTitleTextViewPoint = new int[2];
private float mTitleTextSize;
private int mTitleTextViewWidth;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -90,7 +94,7 @@ private void setUpToolbar() {
}

private void setUpAmazingAvatar() {
mAppBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
mAppBarStateChangeListener = new AppBarStateChangeListener() {

@Override
public void onStateChanged(AppBarLayout appBarLayout,
Expand All @@ -99,24 +103,30 @@ public void onStateChanged(AppBarLayout appBarLayout,

@Override
public void onOffsetChanged(AppBarStateChangeListener.State state, float offset) {
float xOffset = -(mAvatarPoint[0] - mSpacePoint[0]) * offset;
float yOffset = -(mAvatarPoint[1] - mSpacePoint[1]) * offset;
float xTitleOffset = -(mTitleTextViewPoint[0] - mToolbarTextPoint[0]) * offset;
float yTitleOffset = -(mTitleTextViewPoint[1] - mToolbarTextPoint[1]) * offset;
int newSize = Utils.convertDpToPixelSize(EXPAND_AVATAR_SIZE_DP -
(EXPAND_AVATAR_SIZE_DP - COLLAPSED_AVATAR_SIZE_DP) * offset, MainActivity
.this);
float newTextSize =
mTitleTextSize - (mTitleTextSize - mToolbarTextView.getTextSize()) * offset;
mAvatarImageView.getLayoutParams().width = newSize;
mAvatarImageView.getLayoutParams().height = newSize;
mAvatarImageView.setTranslationX(xOffset);
mAvatarImageView.setTranslationY(yOffset);
mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newTextSize);
mTitleTextView.setTranslationX(xTitleOffset);
mTitleTextView.setTranslationY(yTitleOffset);
translationView(offset);
}
});
};
mAppBarLayout.addOnOffsetChangedListener(mAppBarStateChangeListener);
}

private void translationView(float offset) {
float xOffset = -(mAvatarPoint[0] - mSpacePoint[0]) * offset;
float yOffset = -(mAvatarPoint[1] - mSpacePoint[1]) * offset;
float xTitleOffset = -(mTitleTextViewPoint[0] - mToolbarTextPoint[0]) * offset;
float yTitleOffset = -(mTitleTextViewPoint[1] - mToolbarTextPoint[1]) * offset;
int newSize = Utils.convertDpToPixelSize(
EXPAND_AVATAR_SIZE_DP - (EXPAND_AVATAR_SIZE_DP - COLLAPSED_AVATAR_SIZE_DP) * offset,
MainActivity
.this);
float newTextSize =
mTitleTextSize - (mTitleTextSize - mToolbarTextView.getTextSize()) * offset;
mAvatarImageView.getLayoutParams().width = newSize;
mAvatarImageView.getLayoutParams().height = newSize;
mAvatarImageView.setTranslationX(xOffset);
mAvatarImageView.setTranslationY(yOffset);
mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newTextSize);
mTitleTextView.setTranslationX(xTitleOffset);
mTitleTextView.setTranslationY(yTitleOffset);
}

/**
Expand All @@ -128,7 +138,27 @@ private void fetchAvatar() {
@Override
public void onSuccess(AvatarModel avatarModel) {
super.onSuccess(avatarModel);
if (isFinishing()) {
return;
}
ImageLoader.getInstance().displayImage(avatarModel.url, mAvatarImageView);
mTitleTextView.setText(
String.format(Locale.getDefault(), "%s %s", avatarModel.firstName,
avatarModel.lastName));
mTitleTextView.post(new Runnable() {

@Override
public void run() {
Rect bounds = new Rect();
Paint textPaint = mTitleTextView.getPaint();
textPaint.setTextSize(mTitleTextSize);
textPaint.getTextBounds(mTitleTextView.getText().toString(), 0,
mTitleTextView.getText().length(), bounds);
mTitleTextViewPoint[0] -= (bounds.width() - mTitleTextViewWidth) / 2;
mTitleTextViewWidth = bounds.width();
translationView(mAppBarStateChangeListener.getCurrentOffset());
}
});
}
});
}
Expand All @@ -140,8 +170,9 @@ public void onWindowFocusChanged(boolean hasFocus) {
mAvatarImageView.getLocationOnScreen(mAvatarPoint);
mSpace.getLocationOnScreen(mSpacePoint);
mToolbarTextView.getLocationOnScreen(mToolbarTextPoint);
mToolbarTextPoint[0] += Utils.convertDpToPixelSize(COLLAPSED_AVATAR_SIZE_DP, this);
mToolbarTextPoint[0] += Utils.convertDpToPixelSize(24, this);
mTitleTextView.getLocationOnScreen(mTitleTextViewPoint);
mTitleTextViewWidth = mTitleTextView.getWidth();
}

private class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {

public enum State {
EXPANDED,
COLLAPSED,
IDLE
EXPANDED, COLLAPSED, IDLE
}

private State mCurrentState = State.IDLE;
private float mCurrentOffset = 0f;

@Override
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
Expand All @@ -30,13 +29,21 @@ public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
}
mCurrentState = State.IDLE;
}
onOffsetChanged(mCurrentState, Math.abs(i / (float) appBarLayout.getTotalScrollRange()));
float offset = Math.abs(i / (float) appBarLayout.getTotalScrollRange());
if (offset != mCurrentOffset) {
mCurrentOffset = offset;
onOffsetChanged(mCurrentState, offset);
}
}

public State getCurrentState() {
return mCurrentState;
}

public float getCurrentOffset() {
return mCurrentOffset;
}

public abstract void onStateChanged(AppBarLayout appBarLayout, State state);

public abstract void onOffsetChanged(State state, float offset);
Expand Down

0 comments on commit 51b0047

Please sign in to comment.