我是Android开发的新手,我的应用程序出了问题。我试图用面朝上的卡片洗牌,正如你在下面的视频中看到的那样,有一个延迟/慢动作移动,整个应用程序在洗牌后移动缓慢。当我用面朝下的卡片洗牌,或者不是所有牌面朝上时,都没有慢动作。
卡片的卡片是ImageView,整个卡片组是RecyclerView布局,您可以在以下代码中看到。
您可以在下面看到所有相关代码:
1)使用RecyclerView的主XML
<?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)包含在recyclerView中的卡的XML
<?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)带有随机播放和翻转卡代码的课程
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)调用函数shuffle&amp;翻转
@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);
}
}
});
}
翻译代码:
private void translate(View viewToMove, View target) {
viewToMove.animate()
.x(target.getX())
.y(target.getY())
.setDuration(800)
.start();
}