拖放在某些设备中无法正常工作

时间:2017-02-01 13:30:21

标签: android drag-and-drop

我创建了一个简单的拖放式游戏应用,用户需要从堆栈中拖出一张卡片并将其放入正确的类别插槽中。我的问题是当我在三个设备中测试应用程序时,两个设备通过了测试,但一个设备失败了。这个设备的问题是,只要将卡正确地拖入正确的类别插槽,类别插槽就不会接受卡。 logcat中的结果返回$name = item::where('id',1)->value('name'); 而不是false

true

以下是02-01 21:13:03.219 25955-25971/com.theothercard.dragdrop I/art: Background sticky concurrent mark sweep GC freed 7750(606KB) AllocSpace objects, 1(12KB) LOS objects, 5% free, 10MB/11MB, paused 11.205ms total 47.587ms 02-01 21:13:03.459 25955-25955/com.theothercard.dragdrop D/android.support.v7.widget.GridLayout: horizontal constraints: x1-x0>=192, x2-x1>=192, x3-x2>=192, x4-x3>=182, x4-x0<=749 are inconsistent; permanently removing: x4-x0<=749. 02-01 21:13:18.059 25955-25955/com.theothercard.dragdrop I/ViewRootImpl: Reporting drop result: false 02-01 21:13:21.139 25955-25955/com.theothercard.dragdrop I/ViewRootImpl: Reporting drop result: false 02-01 21:13:30.119 25955-25955/com.theothercard.dragdrop I/ViewRootImpl: Reporting drop result: false

中拖放机制的代码
GameActivity()

以下是 public boolean onDrag(View receivingLayoutView, DragEvent dragEvent) { final ImageView draggedImage = (ImageView) dragEvent.getLocalState(); final ViewGroup draggedParentLayout = (ViewGroup) draggedImage.getParent(); final FrameLayout bottomLayout = (FrameLayout) receivingLayoutView; // stop the timer when all cards dragged into correct place. if(topLayout.getChildCount() == 1) { // only empty card left timer.cancel(); complete = true; // disable skip button because it has no use anymore skipButton.setEnabled(false); // add summary to database Player p = new Player(name, Calendar.getInstance().getTime(), seconds, score); insertRecord(p); // victory! victoryDialog(); } switch (dragEvent.getAction()) { case DragEvent.ACTION_DRAG_STARTED: if (dragEvent.getClipDescription() .hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) { return true; } return false; case DragEvent.ACTION_DROP: String category = ""; switch (bottomLayout.getId()) { case R.id.asetSemasa: category = "AS"; break; case R.id.asetBukanSemasa: category = "ABS"; break; case R.id.belanja: category = "B"; break; case R.id.cost_of_sales: category = "COS"; break; case R.id.ekuitiPemilik: category = "E"; break; case R.id.hasil: category = "H"; break; case R.id.liabiliti: category = "L"; break; } boolean result = placeCard(draggedImage, draggedParentLayout, bottomLayout, category); return result; case DragEvent.ACTION_DRAG_ENDED: if (!dragEvent.getResult()) { draggedImage.setVisibility(View.VISIBLE); } return true; default: break; } return false; }

的方法定义
placeCard()

这是我的protected boolean placeCard(ImageView draggedImage, ViewGroup draggedParentLayout, ViewGroup bottomLayout, String cardCategory) { for (int card = 0; card < Card.cards.length; card++) { // compare dragged image reference is similar as one of the card image reference. if (draggedImage.getDrawable().getConstantState().equals( getResources().getDrawable(Card.cards[card].getCardResId()).getConstantState())) { if (Card.cards[card].getCardCategory().equals(cardCategory)) { draggedParentLayout.removeView(draggedImage); //bottomLayout.removeAllViews(); //bottomLayout.addView(draggedImage); bottomLayout.setBackground(draggedImage.getDrawable()); draggedImage.setVisibility(View.VISIBLE); scoreText.setText(String.format("Score: %s", Integer.toString(score += Score.rightScore()))); wrongCount = 0; return true; } } } scoreText.setText(String.format("Score: %s", Integer.toString(score -= Score.wrongScore()))); wrongCount++; if(wrongCount >= 3) { Toast.makeText(this, "Wrong three times, skip to next card...", Toast.LENGTH_SHORT).show(); skipNextCard(); wrongCount = 0; } return false; } 布局。

game_activity.xml

我真的不知道现在该做什么。我感谢你的帮助。

附加信息:我尝试并获得成功结果的设备是运行Marshmallow的Redmi Note 3 Pro和运行Marshmallow的Galaxy Note 8.0。失败的是Zenfone 5运行Lollipop。

更新:由于我在两个ConstantState之间进行比较以获得正确的图像,因此这里是logcat。

使用运行Marshmallow的Redmi Note 3进行测试:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_space"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/pw_repeating"
    tools:context=".GameActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/player"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Score: 0"/>

        <TextView
            android:id="@+id/timer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="0:00"/>

        <Button
            android:id="@+id/startBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/start"/>

        <Button
            android:id="@+id/resetBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/reset"/>

        <Button
            android:id="@+id/quitBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/quit"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:layout_gravity="center_vertical|center_horizontal"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/top_layout"
            android:layout_width="190dp"
            android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal|center_vertical">

            <ImageView
                android:layout_width="190dp"
                android:layout_height="256dp"
                android:src="@drawable/emptycard"
                android:layout_gravity="center_horizontal|center_vertical" />
        </FrameLayout>

        <Button
            android:id="@+id/skipBtn"
            android:layout_width="155dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/skip"/>

    </LinearLayout>

    <android.support.v7.widget.GridLayout
        xmlns:grid="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/bottom_layout"
        android:layout_height="match_parent"
        app:useDefaultMargins="true"
        android:layout_weight="1"
        grid:rowCount="4"
        grid:columnCount="4"
        android:layout_gravity="fill_horizontal"
        android:paddingRight="5dp"
        app:rowOrderPreserved="false"
        android:paddingTop="0dp"
        app:orientation="horizontal"
        android:layout_width="wrap_content">

        <TextView
            app:layout_row="0"
            app:layout_column="0"
            android:layout_width="0dp"
            app:layout_gravity="fill_horizontal"
            android:text="@string/aset_semasa"
            android:gravity="center" />

        <TextView
            app:layout_row="0"
            app:layout_column="1"
            android:layout_width="0dp"
            app:layout_gravity="fill_horizontal"
            android:text="@string/aset_bukan_semasa"
            android:gravity="center" />

        <TextView
            app:layout_row="0"
            app:layout_column="2"
            android:layout_width="76dp"
            app:layout_gravity="fill_horizontal"
            android:text="@string/belanja"
            android:gravity="center"
            android:paddingRight="10dp" />

        <TextView
            app:layout_row="0"
            app:layout_column="3"
            android:layout_width="76dp"
            app:layout_columnWeight="1"
            app:layout_gravity="fill"
            android:text="@string/cost_of_sales"
            android:gravity="center"
            android:paddingRight="10dp" />

        <FrameLayout
            android:id="@+id/asetSemasa"
            android:layout_width="90dp"
            android:layout_height="0dp"
            app:layout_rowWeight="2"
            android:padding="5dp"
            android:layout_margin="3dp"
            app:layout_row="1"
            app:layout_column="0"
            android:background="@drawable/emptycard">

        </FrameLayout>

        <FrameLayout
            android:id="@+id/asetBukanSemasa"
            android:layout_width="90dp"
            android:layout_height="0dp"
            app:layout_rowWeight="2"
            android:padding="5dp"
            android:layout_margin="3dp"
            app:layout_row="1"
            app:layout_column="1"
            android:background="@drawable/emptycard">

        </FrameLayout>

        <FrameLayout
            android:id="@+id/belanja"
            android:layout_width="90dp"
            android:layout_height="0dp"
            app:layout_rowWeight="2"
            android:padding="5dp"
            android:layout_margin="3dp"
            app:layout_row="1"
            app:layout_column="2"
            android:background="@drawable/emptycard">

        </FrameLayout>

        <FrameLayout
            android:id="@+id/ekuitiPemilik"
            android:layout_width="90dp"
            android:layout_height="0dp"
            app:layout_rowWeight="2"
            android:padding="5dp"
            android:layout_margin="3dp"
            app:layout_row="2"
            app:layout_column="0"
            android:background="@drawable/emptycard">

        </FrameLayout>

        <FrameLayout
            android:id="@+id/hasil"
            android:layout_width="90dp"
            android:layout_height="0dp"
            app:layout_rowWeight="2"
            android:padding="5dp"
            android:layout_margin="3dp"
            app:layout_row="2"
            app:layout_column="1"
            android:background="@drawable/emptycard">

        </FrameLayout>

        <FrameLayout
            android:id="@+id/liabiliti"
            android:layout_width="90dp"
            android:layout_height="0dp"
            app:layout_rowWeight="2"
            android:padding="5dp"
            android:layout_margin="3dp"
            app:layout_row="2"
            app:layout_column="2"
            android:background="@drawable/emptycard">

        </FrameLayout>

        <TextView
            app:layout_row="3"
            app:layout_column="0"
            android:layout_width="0dp"
            app:layout_gravity="fill_horizontal"
            android:text="@string/ekuiti_pemilik"
            android:gravity="center" />

        <TextView
            app:layout_row="3"
            app:layout_column="1"
            android:layout_width="0dp"
            app:layout_gravity="fill_horizontal"
            android:text="@string/hasil"
            android:gravity="center" />

        <TextView
            app:layout_row="3"
            app:layout_column="2"
            android:layout_width="85dp"
            app:layout_gravity="fill_horizontal"
            android:text="@string/liabiliti"
            android:gravity="center" />

        <FrameLayout
            android:id="@+id/cost_of_sales"
            android:layout_width="85dp"
            android:layout_height="0dp"
            app:layout_rowWeight="2"
            android:padding="5dp"
            android:layout_margin="3dp"
            app:layout_row="1"
            app:layout_column="3"
            android:background="@drawable/emptycard" />

    </android.support.v7.widget.GridLayout>


</LinearLayout>

使用Zenfone 5运行Lollipop测试类似的图像:

2-01 21:55:30.616 11714-11714/com.theothercard.dragdrop D/DRAGGED IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@b55d916
02-01 21:55:30.616 11714-11714/com.theothercard.dragdrop D/ORIGINAL IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@8b65297
02-01 21:55:30.617 11714-11714/com.theothercard.dragdrop D/DRAGGED IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@b55d916
02-01 21:55:30.617 11714-11714/com.theothercard.dragdrop D/ORIGINAL IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@a955c84
02-01 21:55:30.617 11714-11714/com.theothercard.dragdrop D/DRAGGED IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@b55d916
02-01 21:55:30.617 11714-11714/com.theothercard.dragdrop D/ORIGINAL IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@5e0cc6d
02-01 21:55:30.617 11714-11714/com.theothercard.dragdrop D/DRAGGED IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@b55d916
02-01 21:55:30.618 11714-11714/com.theothercard.dragdrop D/ORIGINAL IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@84e70a2
02-01 21:55:30.618 11714-11714/com.theothercard.dragdrop D/DRAGGED IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@b55d916
02-01 21:55:30.619 11714-11714/com.theothercard.dragdrop D/ORIGINAL IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@f038133
02-01 21:55:30.620 11714-11714/com.theothercard.dragdrop D/DRAGGED IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@b55d916
02-01 21:55:30.620 11714-11714/com.theothercard.dragdrop D/ORIGINAL IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@64dbcf0
02-01 21:55:30.620 11714-11714/com.theothercard.dragdrop D/DRAGGED IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@b55d916
02-01 21:55:30.621 11714-11714/com.theothercard.dragdrop D/ORIGINAL IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@4ac269
02-01 21:55:30.621 11714-11714/com.theothercard.dragdrop D/DRAGGED IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@b55d916
02-01 21:55:30.621 11714-11714/com.theothercard.dragdrop D/ORIGINAL IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@d46f4ee
02-01 21:55:30.622 11714-11714/com.theothercard.dragdrop D/DRAGGED IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@b55d916
02-01 21:55:30.622 11714-11714/com.theothercard.dragdrop D/ORIGINAL IMAGE: android.graphics.drawable.BitmapDrawable$BitmapState@b55d916
02-01 21:55:30.624 11714-11714/com.theothercard.dragdrop I/ViewRootImpl: Reporting drop result: true

主要关注的是为什么后者没有任何类似的ConstantState,或者我做错了。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

Nvm我参考this answer解决了这个问题。只需改变

 if (draggedImage.getDrawable().getConstantState().equals(
            getResources().getDrawable(Card.cards[card].getCardResId()).getConstantState())) {}

if (draggedImage.getDrawable().getConstantState().equals(
                ContextCompat.getDrawable(GameActivity.this, Card.cards[card].getCardResId()).getConstantState())) {
placeCard()方法中的

基本上从API 21开始,您必须使用ContextCompat.getDrawable()而不是普通的getResources().getDrawable()方法。