Add cardview animation

This commit is contained in:
Tanguy Herbron 2018-04-06 15:01:07 +02:00
parent daf2f0ddc1
commit 4193fc0709
10 changed files with 133 additions and 95 deletions

View File

@ -13,7 +13,7 @@ import android.widget.ListView;
import android.widget.SearchView; import android.widget.SearchView;
import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; 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 com.nauk.coinfolio.R;
import java.util.ArrayList; import java.util.ArrayList;
@ -22,7 +22,7 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear
private String[] currencySymbols; private String[] currencySymbols;
private String[] currencyNames; private String[] currencyNames;
private CurrencyAdapter adapter; private CurrencyListAdapter adapter;
private ListView listView; private ListView listView;
private android.widget.Filter filter; private android.widget.Filter filter;
@ -75,7 +75,7 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear
currencyArrayList.add(new Currency(currencyNames[i], currencySymbols[i])); currencyArrayList.add(new Currency(currencyNames[i], currencySymbols[i]));
} }
adapter = new CurrencyAdapter(this, currencyArrayList); adapter = new CurrencyListAdapter(this, currencyArrayList);
} }
private void setupList() private void setupList()

View File

@ -6,6 +6,7 @@ import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;

View File

@ -17,12 +17,12 @@ import java.util.ArrayList;
* Created by Guitoune on 17/01/2018. * Created by Guitoune on 17/01/2018.
*/ */
public class CurrencyAdapter extends ArrayAdapter<Currency> { public class CurrencyListAdapter extends ArrayAdapter<Currency> {
private ArrayList<Currency> tempCurrency, suggestions; private ArrayList<Currency> tempCurrency, suggestions;
private Context context; private Context context;
public CurrencyAdapter(Context context, ArrayList<Currency> objects) { public CurrencyListAdapter(Context context, ArrayList<Currency> objects) {
super(context, android.R.layout.simple_list_item_1, objects); super(context, android.R.layout.simple_list_item_1, objects);
this.tempCurrency = new ArrayList<Currency>(objects); this.tempCurrency = new ArrayList<Currency>(objects);
this.suggestions = new ArrayList<Currency>(objects); this.suggestions = new ArrayList<Currency>(objects);

View File

@ -5,9 +5,12 @@ import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter; import android.graphics.PorterDuffColorFilter;
import android.support.v7.widget.CardView;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
@ -53,33 +56,24 @@ public class HomeLayoutGenerator {
view.setOnClickListener(new View.OnClickListener() { view.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { 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); collapseView(view);
} }
else else
{ {
extendView(currency, view); extendView(view);
} }
} }
}); });
updateCardViewInfos(view, currency, totalValue, isBalanceHidden); 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); setupLineChart(view, currency);
if(isExtended) if(isExtended)
{ {
extendView(currency, view); extendView(view);
} }
else else
{ {
@ -91,6 +85,60 @@ public class HomeLayoutGenerator {
return view; 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) private void setupLineChart(View view, final Currency currency)
{ {
LineChart lineChart = view.findViewById(R.id.LineChartView); LineChart lineChart = view.findViewById(R.id.LineChartView);
@ -159,31 +207,13 @@ public class HomeLayoutGenerator {
private void collapseView(View view) private void collapseView(View view)
{ {
view.findViewById(R.id.separationLayout).setVisibility(View.GONE); collapse(view.findViewById(R.id.collapsableLayout));
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);
} }
private void extendView(Currency currency, View view) private void extendView(View view)
{ {
view.findViewById(R.id.separationLayout).setVisibility(View.VISIBLE); expand(view.findViewById(R.id.collapsableLayout));
view.findViewById(R.id.detailsArrow).setVisibility(View.VISIBLE); view.findViewById(R.id.LineChartView).invalidate();
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);
}
} }
private List<Double> getAxisBorders(Currency currency) private List<Double> getAxisBorders(Currency currency)

View File

@ -0,0 +1,5 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#42000000" />
<corners android:radius="5dp" />
</shape>

View File

@ -0,0 +1,5 @@
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/ic_unfold_less_black_24dp" android:duration="200" />
<item android:drawable="@drawable/ic_details_black_24dp" android:duration="200" />
</animation-list>

View File

@ -12,7 +12,8 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="10dp" android:layout_margin="10dp"
android:clickable="false" android:clickable="false"
android:backgroundTint="@color/listBackground2" android:backgroundTint="@color/cardview_background"
android:elevation="@dimen/cardview_elevation"
app:cardCornerRadius="2dp" app:cardCornerRadius="2dp"
app:layout_constraintBottom_toBottomOf="parent"> app:layout_constraintBottom_toBottomOf="parent">
@ -127,66 +128,57 @@
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/separationLayout" android:id="@+id/collapsableLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="5dp" android:visibility="gone"
android:visibility="gone"> android:orientation="vertical">
<TextView <LinearLayout
android:layout_width="wrap_content" android:id="@+id/separationLayout"
android:layout_height="wrap_content"
android:text="Day history"
android:textSize="@dimen/cardViewSecondaryText" />
<View
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/separationLineSize" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_margin="5dp">
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" <TextView
android:background="@color/separationLine" /> android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day history"
android:textSize="@dimen/cardViewSecondaryText" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/separationLineSize"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/separationLine" />
</LinearLayout>
<FrameLayout
android:id="@+id/frameLayoutChart"
android:layout_width="match_parent"
android:layout_height="@dimen/cardViewChartSize"
android:clickable="true"
android:focusable="true">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/LineChartView"
android:layout_width="match_parent"
android:layout_height="150dp"/>
<ImageView
android:id="@+id/detailsArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_keyboard_arrow_right_grey_48dp"
android:layout_gravity="center_vertical|end"/>
</FrameLayout>
</LinearLayout> </LinearLayout>
<FrameLayout
android:id="@+id/frameLayoutChart"
android:layout_width="match_parent"
android:layout_height="@dimen/cardViewChartSize"
android:clickable="true"
android:focusable="true"
android:visibility="gone">
<!--<com.db.chart.view.LineChartView
android:id="@+id/LineChartView"
android:layout_width="match_parent"
android:layout_height="@dimen/cardViewChartSize"
android:visibility="gone" />-->
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/LineChartView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:visibility="gone"/>
<TextView
android:id="@+id/errorTextView"
android:layout_width="match_parent"
android:layout_height="@dimen/cardViewChartSize"
android:gravity="center"
android:text="Error"
android:visibility="gone" />
<ImageView
android:id="@+id/detailsArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_keyboard_arrow_right_grey_48dp"
android:visibility="gone"
android:layout_gravity="center_vertical|end"/>
</FrameLayout>
</LinearLayout> </LinearLayout>
</android.support.v7.widget.CardView> </android.support.v7.widget.CardView>

View File

@ -5,7 +5,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_marginBottom="50dp"> android:layout_marginBottom="50dp"
android:background="@color/summary_background">
<ViewFlipper <ViewFlipper
android:id="@+id/viewFlipperSummary" android:id="@+id/viewFlipperSummary"

View File

@ -14,7 +14,7 @@
<color name="listBackground2">#000046</color> <color name="listBackground2">#000046</color>
<color name="separationLine">#FF999999</color> <color name="separationLine">#FF999999</color>
<color name="red">#FFF44336</color> <color name="red">#FFF44336</color>
<color name="green">#FF4CAF50</color> <color name="green">#FF4CAF50</color>#FBFCFF
</resources> </resources>
--> -->
@ -34,4 +34,6 @@
<color name="red">#FFF44336</color> <color name="red">#FFF44336</color>
<color name="green">#FF4CAF50</color> <color name="green">#FF4CAF50</color>
<color name="white">#FFFFFFFF</color> <color name="white">#FFFFFFFF</color>
<color name="cardview_background">#FFFFFFFF</color>
<color name="summary_background">#FBFCFF</color>
</resources> </resources>

View File

@ -17,4 +17,6 @@
<dimen name="fingerprint_dialog_width">250dp</dimen> <dimen name="fingerprint_dialog_width">250dp</dimen>
<dimen name="separationLineSize">1dp</dimen> <dimen name="separationLineSize">1dp</dimen>
<dimen name="cardview_elevation">8dp</dimen>
</resources> </resources>