Skip to content

Commit

Permalink
Update version to 1.0.5, Fix position moved when window refocus
Browse files Browse the repository at this point in the history
hearsilent committed Jun 12, 2018
1 parent 854561e commit 894b522
Showing 11 changed files with 118 additions and 50 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -8,10 +8,10 @@ An android amazing avatar anim in CollapsingToolbarLayout.
<img src="https://raw.githubusercontent.com/hearsilent/AmazingAvatar/master/screenshots/screenrecord.gif" height="500">

### Expanded
<img src="https://raw.githubusercontent.com/hearsilent/AmazingAvatar/master/screenshots/device-2017-03-12-220509.png" height="500">
<img src="https://raw.githubusercontent.com/hearsilent/AmazingAvatar/master/screenshots/device-2018-06-12-173327.png" height="500">

### Collapsed
<img src="https://raw.githubusercontent.com/hearsilent/AmazingAvatar/master/screenshots/device-2017-03-12-220529.png" height="500">
<img src="https://raw.githubusercontent.com/hearsilent/AmazingAvatar/master/screenshots/device-2018-06-12-173349.png" height="500">

## Usage

5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@ android {
applicationId "hearsilent.amazingavatar"
minSdkVersion 16
targetSdkVersion 27
versionCode 104
versionName "1.0.4"
versionCode 105
versionName "1.0.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
@@ -27,6 +27,7 @@ dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
97 changes: 69 additions & 28 deletions app/src/main/java/hearsilent/amazingavatar/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hearsilent.amazingavatar;

import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
@@ -45,8 +46,8 @@ public class MainActivity extends AppCompatActivity {
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[] mAvatarPoint = new float[2], mSpacePoint = new float[2], mToolbarTextPoint =
new float[2], mTitleTextViewPoint = new float[2];
private float mTitleTextSize;

@Override
@@ -86,6 +87,9 @@ private void setUpRecyclerView() {
}

private void setUpToolbar() {
mAppBarLayout.getLayoutParams().height = Utils.getDisplayMetrics(this).widthPixels * 9 / 16;
mAppBarLayout.requestLayout();

setSupportActionBar(mToolBar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
@@ -110,20 +114,31 @@ public void onOffsetChanged(AppBarStateChangeListener.State state, float offset)
}

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(
float newAvatarSize = Utils.convertDpToPixel(
EXPAND_AVATAR_SIZE_DP - (EXPAND_AVATAR_SIZE_DP - COLLAPSED_AVATAR_SIZE_DP) * offset,
MainActivity
.this);
this);
float expandAvatarSize = (int) Utils.convertDpToPixel(EXPAND_AVATAR_SIZE_DP, this);
float xAvatarOffset =
(mSpacePoint[0] - mAvatarPoint[0] - (expandAvatarSize - newAvatarSize) / 2f) *
offset;
float yAvatarOffset =
(mSpacePoint[1] - mAvatarPoint[1] - (expandAvatarSize - newAvatarSize) / 2f) *
offset;
mAvatarImageView.getLayoutParams().width = Math.round(newAvatarSize);
mAvatarImageView.getLayoutParams().height = Math.round(newAvatarSize);
mAvatarImageView.setTranslationX(xAvatarOffset);
mAvatarImageView.setTranslationY(yAvatarOffset);

float newTextSize =
mTitleTextSize - (mTitleTextSize - mToolbarTextView.getTextSize()) * offset;
mAvatarImageView.getLayoutParams().width = newSize;
mAvatarImageView.getLayoutParams().height = newSize;
mAvatarImageView.setTranslationX(xOffset);
mAvatarImageView.setTranslationY(yOffset);
Paint paint = new Paint();
paint.setTextSize(newTextSize);
float newTextWidth = Utils.getTextWidth(paint, mTitleTextView.getText().toString());
paint.setTextSize(mTitleTextSize);
float originTextWidth = Utils.getTextWidth(paint, mTitleTextView.getText().toString());
float xTitleOffset = (mToolbarTextPoint[0] - mTitleTextViewPoint[0] -
(originTextWidth - newTextWidth) / 2f) * offset;
float yTitleOffset = (mToolbarTextPoint[1] - mTitleTextViewPoint[1]) * offset;
mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newTextSize);
mTitleTextView.setTranslationX(xTitleOffset);
mTitleTextView.setTranslationY(yTitleOffset);
@@ -142,9 +157,9 @@ public void onSuccess(AvatarModel avatarModel) {
return;
}
ImageLoader.getInstance().displayImage(avatarModel.url, mAvatarImageView);
mTitleTextView.setText(
String.format(Locale.getDefault(), "%s %s", avatarModel.firstName,
avatarModel.lastName));
String name = String.format(Locale.getDefault(), "%s %s", avatarModel.firstName,
avatarModel.lastName);
mTitleTextView.setText(name);
new Handler(Looper.getMainLooper()).post(new Runnable() {

@Override
@@ -157,22 +172,48 @@ public void run() {
}

private void resetPoints() {
int avatarSize = Utils.convertDpToPixelSize(EXPAND_AVATAR_SIZE_DP, this);
mAvatarImageView.getLocationOnScreen(mAvatarPoint);
mAvatarPoint[0] -= mAvatarImageView.getTranslationX() +
(avatarSize - mAvatarImageView.getWidth()) / 2f;
mAvatarPoint[1] -= mAvatarImageView.getTranslationY();
mSpace.getLocationOnScreen(mSpacePoint);
mToolbarTextView.getLocationOnScreen(mToolbarTextPoint);
mToolbarTextPoint[0] += Utils.convertDpToPixelSize(16, this);
mTitleTextView.getLocationOnScreen(mTitleTextViewPoint);
mTitleTextViewPoint[0] -= mTitleTextView.getTranslationX();
mTitleTextViewPoint[1] -= mTitleTextView.getTranslationY();
final float offset = mAppBarStateChangeListener.getCurrentOffset();

float newAvatarSize = Utils.convertDpToPixel(
EXPAND_AVATAR_SIZE_DP - (EXPAND_AVATAR_SIZE_DP - COLLAPSED_AVATAR_SIZE_DP) * offset,
this);
float expandAvatarSize = (int) Utils.convertDpToPixel(EXPAND_AVATAR_SIZE_DP, this);

int[] avatarPoint = new int[2];
mAvatarImageView.getLocationOnScreen(avatarPoint);
mAvatarPoint[0] = avatarPoint[0] - mAvatarImageView.getTranslationX() -
(expandAvatarSize - newAvatarSize) / 2f;
mAvatarPoint[1] = avatarPoint[1] - mAvatarImageView.getTranslationY() -
(expandAvatarSize - newAvatarSize) / 2f;

int[] spacePoint = new int[2];
mSpace.getLocationOnScreen(spacePoint);
mSpacePoint[0] = spacePoint[0];
mSpacePoint[1] = spacePoint[1];

int[] toolbarTextPoint = new int[2];
mToolbarTextView.getLocationOnScreen(toolbarTextPoint);
mToolbarTextPoint[0] = toolbarTextPoint[0];
mToolbarTextPoint[1] = toolbarTextPoint[1];

float newTextSize =
mTitleTextSize - (mTitleTextSize - mToolbarTextView.getTextSize()) * offset;
Paint paint = new Paint();
paint.setTextSize(newTextSize);
float newTextWidth = Utils.getTextWidth(paint, mTitleTextView.getText().toString());
paint.setTextSize(mTitleTextSize);
float originTextWidth = Utils.getTextWidth(paint, mTitleTextView.getText().toString());
int[] titleTextViewPoint = new int[2];
mTitleTextView.getLocationOnScreen(titleTextViewPoint);
mTitleTextViewPoint[0] = titleTextViewPoint[0] - mTitleTextView.getTranslationX() -
(originTextWidth - newTextWidth) / 2f;
mTitleTextViewPoint[1] = titleTextViewPoint[1] - mTitleTextView.getTranslationY();

new Handler().post(new Runnable() {

@Override
public void run() {
translationView(mAppBarStateChangeListener.getCurrentOffset());
translationView(offset);
}
});
}
5 changes: 5 additions & 0 deletions app/src/main/java/hearsilent/amazingavatar/libs/Utils.java
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.util.DisplayMetrics;

public class Utils {
@@ -28,4 +29,8 @@ public static int convertDpToPixelSize(float dp, Context context) {
return -1;
}

public static float getTextWidth(Paint paint, String text) {
return paint.measureText(text);
}

}
53 changes: 36 additions & 17 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -11,13 +11,13 @@
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="224dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_behavior="hearsilent.amazingavatar.libs.FlingBehavior">

<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="224dp"
android:layout_height="match_parent"
app:contentScrim="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:statusBarScrim="@android:color/transparent"
@@ -42,8 +42,8 @@
android:id="@+id/space"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="16dp"/>
android:layout_marginLeft="56dp"
android:layout_marginRight="8dp"/>

<TextView
android:id="@+id/toolbar_title"
@@ -53,37 +53,56 @@
android:gravity="center_vertical"/>
</LinearLayout>

<FrameLayout
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="1">

<TextView
android:id="@+id/textView_title_shadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textSize="24sp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/space_avatar"/>

<TextView
android:id="@+id/textView_title"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="56dp"
android:layout_marginRight="56dp"
android:layout_marginTop="130dp"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:text="HearSilent"
android:textColor="#FFF"
android:textSize="24sp"/>
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/textView_title_shadow"
app:layout_constraintEnd_toEndOf="@+id/textView_title_shadow"
app:layout_constraintStart_toStartOf="@+id/textView_title_shadow"
app:layout_constraintTop_toTopOf="@+id/textView_title_shadow"/>

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imageView_avatar"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="?attr/actionBarSize"
android:background="@drawable/avatar_background"
android:contentDescription="@null"
android:padding="1dp"/>
</FrameLayout>
android:padding="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<Space
android:id="@+id/space_avatar"
android:layout_width="80dp"
android:layout_height="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -3,9 +3,10 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.android.tools.build:gradle:3.1.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
@@ -15,6 +16,7 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

Binary file removed screenshots/device-2017-03-12-220509.png
Binary file not shown.
Binary file removed screenshots/device-2017-03-12-220529.png
Binary file not shown.
Binary file added screenshots/device-2018-06-12-173327.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/device-2018-06-12-173349.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/screenrecord.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 894b522

Please sign in to comment.