From 4193fc0709dfc4595c26e5b65c94dda42ba35db5 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Fri, 6 Apr 2018 15:01:07 +0200 Subject: [PATCH] Add cardview animation --- .../Activities/CurrencySelectionActivity.java | 6 +- .../coinfolio/Activities/HomeActivity.java | 1 + ...yAdapter.java => CurrencyListAdapter.java} | 4 +- .../LayoutManagers/HomeLayoutGenerator.java | 98 +++++++++++------ .../res/drawable/drop_shadow_cardview.xml | 5 + .../expand_collapse_button_animation.xml | 5 + app/src/main/res/layout/cardview_currency.xml | 100 ++++++++---------- .../res/layout/content_currency_summary.xml | 3 +- app/src/main/res/values/colors.xml | 4 +- app/src/main/res/values/dimens.xml | 2 + 10 files changed, 133 insertions(+), 95 deletions(-) rename app/src/main/java/com/nauk/coinfolio/LayoutManagers/{CurrencyAdapter.java => CurrencyListAdapter.java} (96%) create mode 100644 app/src/main/res/drawable/drop_shadow_cardview.xml create mode 100644 app/src/main/res/drawable/expand_collapse_button_animation.xml diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/CurrencySelectionActivity.java b/app/src/main/java/com/nauk/coinfolio/Activities/CurrencySelectionActivity.java index 5d431a5..0a7efbd 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/CurrencySelectionActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/CurrencySelectionActivity.java @@ -13,7 +13,7 @@ import android.widget.ListView; import android.widget.SearchView; import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; -import com.nauk.coinfolio.LayoutManagers.CurrencyAdapter; +import com.nauk.coinfolio.LayoutManagers.CurrencyListAdapter; import com.nauk.coinfolio.R; import java.util.ArrayList; @@ -22,7 +22,7 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear private String[] currencySymbols; private String[] currencyNames; - private CurrencyAdapter adapter; + private CurrencyListAdapter adapter; private ListView listView; private android.widget.Filter filter; @@ -75,7 +75,7 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear currencyArrayList.add(new Currency(currencyNames[i], currencySymbols[i])); } - adapter = new CurrencyAdapter(this, currencyArrayList); + adapter = new CurrencyListAdapter(this, currencyArrayList); } private void setupList() diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java index f89f283..bbb22c3 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; +import android.graphics.drawable.AnimationDrawable; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; diff --git a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyAdapter.java b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyListAdapter.java similarity index 96% rename from app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyAdapter.java rename to app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyListAdapter.java index 166ede8..404440e 100644 --- a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyAdapter.java +++ b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/CurrencyListAdapter.java @@ -17,12 +17,12 @@ import java.util.ArrayList; * Created by Guitoune on 17/01/2018. */ -public class CurrencyAdapter extends ArrayAdapter { +public class CurrencyListAdapter extends ArrayAdapter { private ArrayList tempCurrency, suggestions; private Context context; - public CurrencyAdapter(Context context, ArrayList objects) { + public CurrencyListAdapter(Context context, ArrayList objects) { super(context, android.R.layout.simple_list_item_1, objects); this.tempCurrency = new ArrayList(objects); this.suggestions = new ArrayList(objects); diff --git a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java index f946862..70de870 100644 --- a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java +++ b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java @@ -5,9 +5,12 @@ import android.content.Intent; import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; +import android.support.v7.widget.CardView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; +import android.view.animation.Animation; +import android.view.animation.Transformation; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; @@ -53,33 +56,24 @@ public class HomeLayoutGenerator { view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - if(view.findViewById(R.id.LineChartView).getVisibility() == View.VISIBLE || view.findViewById(R.id.errorTextView).getVisibility() == View.VISIBLE) + if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) { collapseView(view); } else { - extendView(currency, view); + extendView(view); } } }); updateCardViewInfos(view, currency, totalValue, isBalanceHidden); - view.findViewById(R.id.errorTextView).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Intent intent = new Intent(context.getApplicationContext(), CurrencyDetailsActivity.class); - intent.putExtra("currency", currency); - context.getApplicationContext().startActivity(intent); - } - }); - setupLineChart(view, currency); if(isExtended) { - extendView(currency, view); + extendView(view); } else { @@ -91,6 +85,60 @@ public class HomeLayoutGenerator { return view; } + public static void expand(final View v) { + v.measure(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT); + final int targetHeight = v.getMeasuredHeight(); + + // Older versions of android (pre API 21) cancel animations for views with a height of 0. + v.getLayoutParams().height = 1; + v.setVisibility(View.VISIBLE); + Animation a = new Animation() + { + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + v.getLayoutParams().height = interpolatedTime == 1 + ? CardView.LayoutParams.WRAP_CONTENT + : (int)(targetHeight * interpolatedTime); + v.requestLayout(); + } + + @Override + public boolean willChangeBounds() { + return true; + } + }; + + // 1dp/ms + a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density)); + v.startAnimation(a); + } + + public static void collapse(final View v) { + final int initialHeight = v.getMeasuredHeight(); + + Animation a = new Animation() + { + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + if(interpolatedTime == 1){ + v.setVisibility(View.GONE); + }else{ + v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime); + v.requestLayout(); + } + } + + @Override + public boolean willChangeBounds() { + return true; + } + }; + + // 1dp/ms + a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density)); + v.startAnimation(a); + } + private void setupLineChart(View view, final Currency currency) { LineChart lineChart = view.findViewById(R.id.LineChartView); @@ -159,31 +207,13 @@ public class HomeLayoutGenerator { private void collapseView(View view) { - view.findViewById(R.id.separationLayout).setVisibility(View.GONE); - view.findViewById(R.id.frameLayoutChart).setVisibility(View.GONE); - view.findViewById(R.id.LineChartView).setVisibility(View.GONE); - view.findViewById(R.id.errorTextView).setVisibility(View.GONE); - view.findViewById(R.id.detailsArrow).setVisibility(View.GONE); + collapse(view.findViewById(R.id.collapsableLayout)); } - private void extendView(Currency currency, View view) + private void extendView(View view) { - view.findViewById(R.id.separationLayout).setVisibility(View.VISIBLE); - view.findViewById(R.id.detailsArrow).setVisibility(View.VISIBLE); - view.findViewById(R.id.frameLayoutChart).setVisibility(View.VISIBLE); - - if(currency.getHistoryMinutes() != null) - { - view.findViewById(R.id.LineChartView).setVisibility(View.VISIBLE); - view.findViewById(R.id.LineChartView).invalidate(); - view.findViewById(R.id.errorTextView).setVisibility(View.GONE); - } - else - { - view.findViewById(R.id.LineChartView).setVisibility(View.GONE); - view.findViewById(R.id.errorTextView).setVisibility(View.VISIBLE); - } - + expand(view.findViewById(R.id.collapsableLayout)); + view.findViewById(R.id.LineChartView).invalidate(); } private List getAxisBorders(Currency currency) diff --git a/app/src/main/res/drawable/drop_shadow_cardview.xml b/app/src/main/res/drawable/drop_shadow_cardview.xml new file mode 100644 index 0000000..e5fbdff --- /dev/null +++ b/app/src/main/res/drawable/drop_shadow_cardview.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/expand_collapse_button_animation.xml b/app/src/main/res/drawable/expand_collapse_button_animation.xml new file mode 100644 index 0000000..45c0a19 --- /dev/null +++ b/app/src/main/res/drawable/expand_collapse_button_animation.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/cardview_currency.xml b/app/src/main/res/layout/cardview_currency.xml index 3ebaaa1..cebcffb 100644 --- a/app/src/main/res/layout/cardview_currency.xml +++ b/app/src/main/res/layout/cardview_currency.xml @@ -12,7 +12,8 @@ android:layout_height="wrap_content" android:layout_margin="10dp" android:clickable="false" - android:backgroundTint="@color/listBackground2" + android:backgroundTint="@color/cardview_background" + android:elevation="@dimen/cardview_elevation" app:cardCornerRadius="2dp" app:layout_constraintBottom_toBottomOf="parent"> @@ -127,66 +128,57 @@ + android:visibility="gone" + android:orientation="vertical"> - - - + android:layout_height="wrap_content" + android:layout_margin="5dp"> + + + + + + + + + + + + + + - - - - - - - - - - - - diff --git a/app/src/main/res/layout/content_currency_summary.xml b/app/src/main/res/layout/content_currency_summary.xml index 6d0a96d..a811fe6 100644 --- a/app/src/main/res/layout/content_currency_summary.xml +++ b/app/src/main/res/layout/content_currency_summary.xml @@ -5,7 +5,8 @@ android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" - android:layout_marginBottom="50dp"> + android:layout_marginBottom="50dp" + android:background="@color/summary_background"> #000046 #FF999999 #FFF44336 - #FF4CAF50 + #FF4CAF50#FBFCFF --> @@ -34,4 +34,6 @@ #FFF44336 #FF4CAF50 #FFFFFFFF + #FFFFFFFF + #FBFCFF \ No newline at end of file diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index bf14994..5ebce76 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -17,4 +17,6 @@ 250dp 1dp + + 8dp