I am new in android development and i have a problem in my application. I am trying to shuffle a deck with face up cards and as you can see in the following video there is a delay/slow motion moving and also the entire application moves slow after shuffling the cards. When i am shuffling the deck with face down cards or with not all the cards face up, there is no slow motion moving.
The cards of the deck are ImageView and the whole deck is a RecyclerView Layout as you can see in the following codes.
Below you can see all the relevant codes:
1) Main XML with RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<android.support.v7.widget.RecyclerView
android:id="@+id/deck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.60"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp" />
</LinearLayout>
2) XML with cards included in recyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/list_frame"
android:layout_width="32dp"
android:layout_height="46dp">
<ImageView
android:id="@+id/list_card_face"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:visibility="gone" />
<ImageView
android:id="@+id/list_card_back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="fitCenter"
app:srcCompat="@drawable/backblue" />
</FrameLayout>
</LinearLayout>
3) Class with shuffle and flip card code
public void shuffle() {
Random random = new Random();
ArrayList<ViewHolder> deckDem = new ArrayList<>();
ArrayList<ViewHolder> deckDem2 = new ArrayList<>();
for (int i = 0; i < cards.size(); i++) {
//imageAddresses.add(cards.get(i).imageTo);
deckDem.add(cards.get(i));
}
int count = 0;
while (deckDem.size() > 0) {
int rand = random.nextInt(deckDem.size());
deckDem2.add(deckDem.remove(rand));
}
for (int i = 0; i < deckDem2.size(); i++) {
translate(cards.get(i).cardView, deckDem2.get(i).cardView);
}
}
}
4) Call of functions shuffle & flip
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
viewHolder.imageBack.setImageResource(R.drawable.backblue);
viewHolder.imageFront.setImageResource(alImage.get(i));
cards.add(viewHolder);
viewHolder.setClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
if (isLongClick) {
myDeck.shuffle();
} else {
myDeck.flipCard(cards.get(position).frame, cards.get(position).imageFront, cards.get(position).imageBack);
}
}
});
}
Translate code:
private void translate(View viewToMove, View target) {
viewToMove.animate()
.x(target.getX())
.y(target.getY())
.setDuration(800)
.start();
}