簡體   English   中英

Android位置在2.X上不正確

[英]Android locations not correct on 2.X

我將嘗試盡可能詳細地解釋該問題,但是由於這是一個非常具體的問題,因此這將非常困難。 但是我已經選擇不了了,真的不知道如何解決這個問題。

我正在創建一個Android游戲,其中用戶以正確的順序單擊圖像以前進到下一個級別。 我在屏幕上的隨機位置生成這些圖像,並且當用戶單擊圖像時,它將觸發事件。 在Android 4.0及更高版本上,此功能非常有效。 但是,當我在較低版本上對其進行測試時,圖像的位置似乎正確,但是單擊圖像時,它會觸發另一幅圖像的事件,或者根本不會觸發。 另外,當您單擊空白區域(通常在左上角附近)時,也會觸發一個事件。 我不確定這是Android版本低還是屏幕分辨率低。

編碼

BubbleImage XML

這是圖像的布局,onClickEvent直接設置為RelativeLayout而不是relativelayout內的ImageView

<?xml version="1.0" encoding="utf-8"?>
<com.timkranen.customui.BubbleImage xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bubImgMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/bubbleImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    android:src="@drawable/bubble_selector" />

    <TextView
    android:id="@+id/numberTxtView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/bubbleImage"
    android:layout_alignLeft="@+id/bubbleImage"
    android:layout_alignBottom="@+id/bubbleImage"
    android:layout_alignRight="@+id/bubbleImage"
    android:gravity="center"
    android:text="test"
    android:textColor="#fff"
    android:textSize="23sp" />

生成隨機的BubbleImages

抱歉,這里的代碼很長,我應該提一下,我使用ninoldandroid庫在較舊的Android版本上獲取正確的x / y坐標(ViewHelper類執行此操作):

  private void generateRandomView(int amount) {
    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i <= amount; i++) {
        // add imageview to layout
        BubbleImage rImg = (BubbleImage) inflater.inflate(
                R.layout.bubbleimage, null);
        rImg.setEnabled(false);
        rImg.setOnClickListener(new BubbleClick());
        bubbleImages.add(rImg);
        totalBubbles++;

        // create random number in between amount and 0
        int genNum = this.generateRandomNum(amount);
        rImg.setNumber(genNum);
        numbers.add(genNum);

        // determine the screen size
        screenSize = getScreenSize();
        screenSize.x = screenSize.x - screenSize.x / 4;
        screenSize.y = screenSize.y - ((screenSize.x / 4) * 3) + 20;

        // stick might throw a stackoverflow error if its called too much
        // but the game only needs to call it on level max
        // stackoverflowerror @ 10x on 300x300
        // stackoverflowerror @ 16x on 250x250
        // depends on screen size
        Point randomLoc = null;
        try {
            Random random = new Random();
            randomLoc = generateRandomLocation(screenSize, random);
        } catch (StackOverflowError e) {
            Toast.makeText(
                    this,
                    "BrainTest PRO encountered a fatal error caused by low memomory on your device. Restart the app and try again, if the error persists please report it to the developer.",
                    Toast.LENGTH_LONG).show();
            finish();
        }

        Resources r = getResources();


        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(180,
                180);
        mainLayout.addView(rImg, params);

        ViewHelper.setTranslationX(rImg, (float) randomLoc.x);
        ViewHelper.setTranslationY(rImg, (float) randomLoc.y);

        // scalefactor on basis of screensize
        float scaleFactor = 1.2f;
        ViewHelper.setScaleX(rImg, scaleFactor);
        ViewHelper.setScaleY(rImg, scaleFactor);

生成隨機定位方法

我認為不需要澄清generateRandomNum(),但是我必須澄清這種方法,它是一種遞歸方法,會循環進入直到生成的圖像不再碰撞為止

  private Point generateRandomLocation(Point dimensions, Random random) {

    // generate random x
    int x = random.nextInt((dimensions.x - 0) + 1);

    // generate random y
    int y = random.nextInt((dimensions.y - 0) + 1);

    Point location = new Point(x, y);

    if (!collision(location)) {
        return new Point(x, y);
    } else {
        return generateRandomLocation(dimensions, new Random());
    }

}

BubbleClick事件這是會觸發的事件,事件本身可以運行,但不會在正確的位置觸發

 private class BubbleClick implements OnClickListener {

    private ScoreCalculator calculator;

    public BubbleClick() {
        calculator = new ScoreCalculator();
    }

    @Override
    public void onClick(View arg0) {
        BubbleImage clicked = (BubbleImage) arg0;
        Log.d("Clicked_bubble", "Num: " + clicked.getNumber() + " | X: " + ViewHelper.getTranslationX(clicked) + " | Y: " + ViewHelper.getTranslationY(clicked));
        ImageView clicked_img = (ImageView) clicked
                .findViewById(R.id.bubbleImage);
        if (currentClick == clicked.getNumber()) {
            if (previousBub != null) {
                GameActivity.this.drawLine(clicked);
            }

            // SUCCESFULL CLICK
            score += calculator.pointsPerBubble(currentLevel);
            scoreLabel.setText("Score: " + String.valueOf(score));
            // clicked_img.setImageResource(R.drawable.bubble_ok);
            clicked.setEnabled(false);
            previousBub = clicked;
            playPosSound();

            for (int i = 0; i <= clicked.getChildCount(); i++) {
                View child = clicked.getChildAt(i);
                if (child instanceof TextView) {
                    TextView txtView = (TextView) clicked.getChildAt(i);
                    txtView.setVisibility(View.VISIBLE);
                }
            }

            // check wether the last bubbleimage was clicked
            if (countListClick + 1 != numbers.size()) {
                countListClick++;
                currentClick = numbers.get(countListClick);
            } else {
                GameActivity.this.resetGame(true);
            }

        } else {
            // WRONG ATTEMPT
            clicked_img.setImageResource(R.drawable.bubble_wrong);
            scores.add(new Score(score, attempt));
            attempt++;
            if (attempt > 3) {
                // GAME OVER
                isGameOver = true;
                continueBut.setVisibility(View.INVISIBLE);
                GameActivity.this.gameOver();
            }

            previousBub = null;

            playNegSound();

            if (currentLevel > 0) {
                currentLevel--;
            }

            vibrate();

            for (int i = 0; i <= clicked.getChildCount(); i++) {
                View child = clicked.getChildAt(i);
                if (child instanceof TextView) {
                    TextView txtView = (TextView) clicked.getChildAt(i);
                    txtView.setVisibility(View.VISIBLE);
                }
            }

            // show all text and disable buttons
            if (bubbleImages != null) {
                for (BubbleImage bImg : bubbleImages) {
                    bImg.setEnabled(false);
                    for (int i = 0; i <= bImg.getChildCount(); i++) {
                        View childV = bImg.getChildAt(i);
                        if (childV instanceof TextView) {
                            TextView childView = (TextView) childV;
                            childView.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }

            if (!isGameOver) {
                GameActivity.this.resetGame(false);
            }

            // GAME OVER

        }

    }

onClickListener的內容是無關緊要的,因為代碼不能在適當的時候正常運行。 有人知道發生了什么嗎?

提前致謝!

因為這對我來說是一個真正的問題:

ViewHelper.getTranslation方法在2.x上的行為不同於在4.x上的行為,我改用了Margins。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM