Example usage for android.view ViewGroup getWidth

List of usage examples for android.view ViewGroup getWidth

Introduction

In this page you can find the example usage for android.view ViewGroup getWidth.

Prototype

@ViewDebug.ExportedProperty(category = "layout")
public final int getWidth() 

Source Link

Document

Return the width of your view.

Usage

From source file:com.juick.android.MainActivity.java

public void openNavigationMenu(boolean animate) {
    if (isNavigationMenuShown())
        return;//from   w  w  w. j a v  a 2s.  co m
    navigationMenuShown = true;
    final ViewGroup navigationPanel = (ViewGroup) findViewById(R.id.navigation_panel);
    navigationPanel.setVisibility(View.VISIBLE);
    final View frag = (ViewGroup) findViewById(R.id.messagesfragment);
    if (animate) {
        AnimationSet set = new AnimationSet(true);
        TranslateAnimation translate = new TranslateAnimation(0, navigationPanel.getWidth(), 0, 0);
        translate.setFillAfter(false);
        translate.setFillEnabled(true);
        translate.setDuration(400);
        set.addAnimation(translate);
        set.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                frag.clearAnimation();
                layoutNavigationPane();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        frag.startAnimation(set);
    } else {
        layoutNavigationPane();
    }
}

From source file:com.juick.android.MainActivity.java

public void closeNavigationMenu(boolean animate, boolean immediate) {
    if (!isNavigationMenuShown())
        return;//  w  ww.j av  a  2s .com
    final ViewGroup navigationPanel = (ViewGroup) findViewById(R.id.navigation_panel);
    final View frag = findViewById(R.id.messagesfragment);
    final DragSortListView navigationList = (DragSortListView) findViewById(R.id.navigation_list);
    if (navigationList.isDragEnabled()) {
        navigationList.setDragEnabled(false);
        updateNavigation();
    }
    if (animate) {
        final AnimationSet set = new AnimationSet(true);
        TranslateAnimation translate = new TranslateAnimation(0, -navigationPanel.getWidth(), 0, 0);
        translate.setFillAfter(false);
        translate.setFillEnabled(true);
        translate.setDuration(400);
        set.addAnimation(translate);
        set.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                navigationMenuShown = false;
                frag.clearAnimation();
                layoutNavigationPane();
                //navigationPanel.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                frag.startAnimation(set);
                getActivity().findViewById(R.id.layout_container).invalidate();
            }
        }, immediate ? 1 : 200); // to smooth the slide

    } else {
        navigationMenuShown = false;
        layoutNavigationPane();
    }
}

From source file:com.android.mail.browse.ConversationItemViewCoordinates.java

private ConversationItemViewCoordinates(final Context context, final Config config,
        final CoordinatesCache cache) {
    Utils.traceBeginSection("CIV coordinates constructor");
    final Resources res = context.getResources();

    final int layoutId = R.layout.conversation_item_view;

    ViewGroup view = (ViewGroup) cache.getView(layoutId);
    if (view == null) {
        view = (ViewGroup) LayoutInflater.from(context).inflate(layoutId, null);
        cache.put(layoutId, view);/*from ww  w.j  a  v a  2s . co m*/
    }

    // Show/hide optional views before measure/layout call
    final TextView folders = (TextView) view.findViewById(R.id.folders);
    folders.setVisibility(config.areFoldersVisible() ? View.VISIBLE : View.GONE);

    View contactImagesView = view.findViewById(R.id.contact_image);

    switch (config.getGadgetMode()) {
    case GADGET_CONTACT_PHOTO:
        contactImagesView.setVisibility(View.VISIBLE);
        break;
    case GADGET_CHECKBOX:
        contactImagesView.setVisibility(View.GONE);
        contactImagesView = null;
        break;
    default:
        contactImagesView.setVisibility(View.GONE);
        contactImagesView = null;
        break;
    }

    final View replyState = view.findViewById(R.id.reply_state);
    replyState.setVisibility(config.isReplyStateVisible() ? View.VISIBLE : View.GONE);

    final View personalIndicator = view.findViewById(R.id.personal_indicator);
    personalIndicator.setVisibility(config.isPersonalIndicatorVisible() ? View.VISIBLE : View.GONE);

    setFramePadding(context, view, config.useFullPadding());

    // Layout the appropriate view.
    ViewCompat.setLayoutDirection(view, config.getLayoutDirection());
    final int widthSpec = MeasureSpec.makeMeasureSpec(config.getWidth(), MeasureSpec.EXACTLY);
    final int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

    view.measure(widthSpec, heightSpec);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    // Once the view is measured, let's calculate the dynamic width variables.
    folderLayoutWidth = (int) (view.getWidth() * res.getInteger(R.integer.folder_max_width_proportion) / 100.0);
    folderCellWidth = (int) (view.getWidth() * res.getInteger(R.integer.folder_cell_max_width_proportion)
            / 100.0);

    //        Utils.dumpViewTree((ViewGroup) view);

    // Records coordinates.

    // Contact images view
    if (contactImagesView != null) {
        contactImagesWidth = contactImagesView.getWidth();
        contactImagesHeight = contactImagesView.getHeight();
        contactImagesX = getX(contactImagesView);
        contactImagesY = getY(contactImagesView);
    } else {
        contactImagesX = contactImagesY = contactImagesWidth = contactImagesHeight = 0;
    }

    final boolean isRtl = ViewUtils.isViewRtl(view);

    final View star = view.findViewById(R.id.star);
    final int starPadding = res.getDimensionPixelSize(R.dimen.conv_list_star_padding_start);
    starX = getX(star) + (isRtl ? 0 : starPadding);
    starY = getY(star);
    starWidth = star.getWidth();

    final TextView senders = (TextView) view.findViewById(R.id.senders);
    final int sendersTopAdjust = getLatinTopAdjustment(senders);
    sendersX = getX(senders);
    sendersY = getY(senders) + sendersTopAdjust;
    sendersWidth = senders.getWidth();
    sendersHeight = senders.getHeight();
    sendersLineCount = SINGLE_LINE;
    sendersFontSize = senders.getTextSize();

    final TextView subject = (TextView) view.findViewById(R.id.subject);
    final int subjectTopAdjust = getLatinTopAdjustment(subject);
    subjectX = getX(subject);
    subjectY = getY(subject) + subjectTopAdjust;
    subjectWidth = subject.getWidth();
    subjectHeight = subject.getHeight();
    subjectFontSize = subject.getTextSize();

    final TextView snippet = (TextView) view.findViewById(R.id.snippet);
    final int snippetTopAdjust = getLatinTopAdjustment(snippet);
    snippetX = getX(snippet);
    snippetY = getY(snippet) + snippetTopAdjust;
    maxSnippetWidth = snippet.getWidth();
    snippetHeight = snippet.getHeight();
    snippetFontSize = snippet.getTextSize();

    if (config.areFoldersVisible()) {
        foldersLeft = getX(folders);
        foldersRight = foldersLeft + folders.getWidth();
        foldersY = getY(folders);
        foldersTypeface = folders.getTypeface();
        foldersFontSize = folders.getTextSize();
    } else {
        foldersLeft = 0;
        foldersRight = 0;
        foldersY = 0;
        foldersTypeface = null;
        foldersFontSize = 0;
    }

    final View colorBlock = view.findViewById(R.id.color_block);
    if (config.isColorBlockVisible() && colorBlock != null) {
        colorBlockX = getX(colorBlock);
        colorBlockY = getY(colorBlock);
        colorBlockWidth = colorBlock.getWidth();
        colorBlockHeight = colorBlock.getHeight();
    } else {
        colorBlockX = colorBlockY = colorBlockWidth = colorBlockHeight = 0;
    }

    if (config.isReplyStateVisible()) {
        replyStateX = getX(replyState);
        replyStateY = getY(replyState);
    } else {
        replyStateX = replyStateY = 0;
    }

    if (config.isPersonalIndicatorVisible()) {
        personalIndicatorX = getX(personalIndicator);
        personalIndicatorY = getY(personalIndicator);
    } else {
        personalIndicatorX = personalIndicatorY = 0;
    }

    final View infoIcon = view.findViewById(R.id.info_icon);
    infoIconX = getX(infoIcon);
    infoIconXRight = infoIconX + infoIcon.getWidth();
    infoIconY = getY(infoIcon);

    final TextView date = (TextView) view.findViewById(R.id.date);
    dateX = getX(date);
    dateXRight = dateX + date.getWidth();
    dateY = getY(date);
    datePaddingStart = ViewUtils.getPaddingStart(date);
    dateFontSize = date.getTextSize();
    dateYBaseline = dateY + getLatinTopAdjustment(date) + date.getBaseline();

    final View paperclip = view.findViewById(R.id.paperclip);
    paperclipY = getY(paperclip);
    paperclipPaddingStart = ViewUtils.getPaddingStart(paperclip);

    height = view.getHeight() + sendersTopAdjust;
    Utils.traceEndSection();
}

From source file:com.tct.mail.browse.ConversationItemViewCoordinates.java

private ConversationItemViewCoordinates(final Context context, final Config config,
        final CoordinatesCache cache) {
    Utils.traceBeginSection("CIV coordinates constructor");
    final Resources res = context.getResources();
    mMinListWidthForWide = res.getDimensionPixelSize(R.dimen.list_min_width_is_wide);

    mMode = calculateMode(res, config);/*  w  ww.  j  a  va 2  s  .c o m*/

    final int layoutId = R.layout.conversation_item_view;

    ViewGroup view = (ViewGroup) cache.getView(layoutId);
    if (view == null) {
        view = (ViewGroup) LayoutInflater.from(context).inflate(layoutId, null);
        cache.put(layoutId, view);
    }

    // Show/hide optional views before measure/layout call
    final TextView folders = (TextView) view.findViewById(R.id.folders);
    folders.setVisibility(config.areFoldersVisible() ? View.VISIBLE : View.GONE);

    View contactImagesView = view.findViewById(R.id.contact_image);

    switch (config.getGadgetMode()) {
    case GADGET_CONTACT_PHOTO:
        contactImagesView.setVisibility(View.VISIBLE);
        break;
    case GADGET_CHECKBOX:
        contactImagesView.setVisibility(View.GONE);
        contactImagesView = null;
        break;
    default:
        contactImagesView.setVisibility(View.GONE);
        contactImagesView = null;
        break;
    }

    final View replyState = view.findViewById(R.id.reply_state);
    replyState.setVisibility(config.isReplyStateVisible() ? View.VISIBLE : View.GONE);

    final View personalIndicator = view.findViewById(R.id.personal_indicator);
    personalIndicator.setVisibility(config.isPersonalIndicatorVisible() ? View.VISIBLE : View.GONE);

    setFramePadding(context, view, config.useFullPadding());

    // Layout the appropriate view.
    ViewCompat.setLayoutDirection(view, config.getLayoutDirection());
    final int widthSpec = MeasureSpec.makeMeasureSpec(config.getWidth(), MeasureSpec.EXACTLY);
    final int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

    view.measure(widthSpec, heightSpec);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    // Once the view is measured, let's calculate the dynamic width variables.
    folderLayoutWidth = (int) (view.getWidth() * res.getInteger(R.integer.folder_max_width_proportion) / 100.0);
    folderCellWidth = (int) (view.getWidth() * res.getInteger(R.integer.folder_cell_max_width_proportion)
            / 100.0);

    //        Utils.dumpViewTree((ViewGroup) view);

    // Records coordinates.

    // Contact images view
    if (contactImagesView != null) {
        contactImagesWidth = contactImagesView.getWidth();
        contactImagesHeight = contactImagesView.getHeight();
        contactImagesX = getX(contactImagesView);
        contactImagesY = getY(contactImagesView);
    } else {
        contactImagesX = contactImagesY = contactImagesWidth = contactImagesHeight = 0;
    }

    final boolean isRtl = ViewUtils.isViewRtl(view);

    final View star = view.findViewById(R.id.star);
    final int starPadding = res.getDimensionPixelSize(R.dimen.conv_list_star_padding_start);
    starX = getX(star) + (isRtl ? 0 : starPadding);
    starY = getY(star);
    starWidth = star.getWidth();

    final TextView senders = (TextView) view.findViewById(R.id.senders);
    final int sendersTopAdjust = getLatinTopAdjustment(senders);
    sendersX = getX(senders);
    sendersY = getY(senders) + sendersTopAdjust;
    sendersWidth = senders.getWidth();
    sendersHeight = senders.getHeight();
    sendersLineCount = SINGLE_LINE;
    sendersFontSize = senders.getTextSize();

    final TextView subject = (TextView) view.findViewById(R.id.subject);
    final int subjectTopAdjust = getLatinTopAdjustment(subject);
    subjectX = getX(subject);
    subjectY = getY(subject) + subjectTopAdjust;
    subjectWidth = subject.getWidth();
    subjectHeight = subject.getHeight();
    subjectFontSize = subject.getTextSize();
    // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_S
    final TextView status = (TextView) view.findViewById(R.id.status);
    final int statusTopAdjust = getLatinTopAdjustment(status);
    statusX = getX(status);
    statusY = getY(status) + statusTopAdjust;
    statusWidth = status.getWidth();
    statusHeight = status.getHeight();
    statusFontSize = status.getTextSize();
    statusPaddingStart = ViewUtils.getPaddingStart(status);
    statusPanddingEnd = ViewUtils.getPaddingEnd(status);
    // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_E
    final TextView snippet = (TextView) view.findViewById(R.id.snippet);
    final int snippetTopAdjust = getLatinTopAdjustment(snippet);
    snippetX = getX(snippet);
    snippetY = getY(snippet) + snippetTopAdjust;
    maxSnippetWidth = snippet.getWidth();
    snippetHeight = snippet.getHeight();
    snippetFontSize = snippet.getTextSize();

    if (config.areFoldersVisible()) {
        // vertically align folders min left edge with subject
        foldersLeft = getX(folders);
        foldersRight = foldersLeft + folders.getWidth();
        foldersY = getY(folders) + sendersTopAdjust;
        foldersHeight = folders.getHeight();
        foldersTypeface = folders.getTypeface();
        foldersTextBottomPadding = res.getDimensionPixelSize(R.dimen.folders_text_bottom_padding);
        foldersFontSize = folders.getTextSize();
    } else {
        foldersLeft = 0;
        foldersRight = 0;
        foldersY = 0;
        foldersHeight = 0;
        foldersTypeface = null;
        foldersTextBottomPadding = 0;
        foldersFontSize = 0;
    }

    final View colorBlock = view.findViewById(R.id.color_block);
    if (config.isColorBlockVisible() && colorBlock != null) {
        colorBlockX = getX(colorBlock);
        colorBlockY = getY(colorBlock);
        colorBlockWidth = colorBlock.getWidth();
        colorBlockHeight = colorBlock.getHeight();
    } else {
        colorBlockX = colorBlockY = colorBlockWidth = colorBlockHeight = 0;
    }

    if (config.isReplyStateVisible()) {
        replyStateX = getX(replyState);
        replyStateY = getY(replyState);
    } else {
        replyStateX = replyStateY = 0;
    }

    if (config.isPersonalIndicatorVisible()) {
        personalIndicatorX = getX(personalIndicator);
        personalIndicatorY = getY(personalIndicator);
    } else {
        personalIndicatorX = personalIndicatorY = 0;
    }

    final View infoIcon = view.findViewById(R.id.info_icon);
    infoIconX = getX(infoIcon);
    infoIconXRight = infoIconX + infoIcon.getWidth();
    infoIconY = getY(infoIcon);

    final TextView date = (TextView) view.findViewById(R.id.date);
    dateX = getX(date);
    dateXRight = dateX + date.getWidth();
    dateY = getY(date);
    datePaddingStart = ViewUtils.getPaddingStart(date);
    dateFontSize = date.getTextSize();
    dateYBaseline = dateY + getLatinTopAdjustment(date) + date.getBaseline();

    final View paperclip = view.findViewById(R.id.paperclip);
    paperclipY = getY(paperclip);
    paperclipPaddingStart = ViewUtils.getPaddingStart(paperclip);

    height = view.getHeight() + sendersTopAdjust;
    Utils.traceEndSection();
}

From source file:com.ring.widget.PagerSlidingTabStrip.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (disableViewPager)
        return;/* w w w .  j av  a2  s  .  co m*/

    /* ? */
    ViewGroup tabsLayout = getTabsLayout();
    int tabCount = tabsLayout.getChildCount();
    if (tabsLayout == null || tabCount == 0) {
        return;
    }

    View currentTab = tabsLayout.getChildAt(currentPosition);
    if (currentTab == null) {
        return;
    }
    int height = getHeight();
    float slidingBlockLeft = currentTab.getLeft();
    float slidingBlockRight = currentTab.getRight();
    if (currentPositionOffset > 0f && currentPosition < tabsLayout.getChildCount() - 1) {
        View nextTab = tabsLayout.getChildAt(currentPosition + 1);
        if (nextTab != null) {
            final float nextTabLeft = nextTab.getLeft();
            final float nextTabRight = nextTab.getRight();
            slidingBlockLeft = (currentPositionOffset * nextTabLeft
                    + (1f - currentPositionOffset) * slidingBlockLeft);
            slidingBlockRight = (currentPositionOffset * nextTabRight
                    + (1f - currentPositionOffset) * slidingBlockRight);
        }
    }
    if (slidingBlockDrawable != null) {
        // ?
        if (disableTensileSlidingBlock) {
            int center = (int) (slidingBlockLeft + (slidingBlockRight - slidingBlockLeft) / 2);
            slidingBlockLeft = center - slidingBlockDrawable.getIntrinsicWidth() / 2;
            slidingBlockRight = center + slidingBlockDrawable.getIntrinsicWidth() / 2;
        }

        slidingBlockDrawable.setBounds((int) slidingBlockLeft,
                getHeight() - slidingBlockDrawable.getIntrinsicHeight(), (int) slidingBlockRight, getHeight());
        slidingBlockDrawable.draw(canvas);
    } else {

        rectPaint.setColor(barColor);

        canvas.drawRect(slidingBlockLeft, height - barHeight, slidingBlockRight, height, rectPaint);
        rectPaint.setColor(underlineColor);
        canvas.drawRect(0, height - underLineHeight, tabsLayout.getWidth(), height, rectPaint);

    }
    if (isAllowDivider) {
        // draw divider

        dividerPaint.setColor(dividerColor);
        for (int i = 0; i < tabCount - 1; i++) {
            View tab = tabsLayout.getChildAt(i);
            canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding,
                    dividerPaint);
        }
    }
}

From source file:com.android.incallui.CallCardFragment.java

@Override
public void animateForNewOutgoingCall() {
    final ViewGroup parent = (ViewGroup) mPrimaryCallCardContainer.getParent();

    final ViewTreeObserver observer = getView().getViewTreeObserver();

    mIsAnimating = true;//from w  w  w  .  j av a 2  s . co  m

    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            final ViewTreeObserver observer = getView().getViewTreeObserver();
            if (!observer.isAlive()) {
                return;
            }
            observer.removeOnGlobalLayoutListener(this);

            final LayoutIgnoringListener listener = new LayoutIgnoringListener();
            mPrimaryCallCardContainer.addOnLayoutChangeListener(listener);

            // Prepare the state of views before the slide animation
            final int originalHeight = mPrimaryCallCardContainer.getHeight();
            mPrimaryCallCardContainer.setTag(R.id.view_tag_callcard_actual_height, originalHeight);
            mPrimaryCallCardContainer.setBottom(parent.getHeight());

            // Set up FAB.
            mFloatingActionButtonContainer.setVisibility(View.GONE);
            mFloatingActionButtonController.setScreenWidth(parent.getWidth());

            mCallButtonsContainer.setAlpha(0);
            mCallStateLabel.setAlpha(0);
            mPrimaryName.setAlpha(0);
            mCallTypeLabel.setAlpha(0);
            mCallNumberAndLabel.setAlpha(0);

            assignTranslateAnimation(mCallStateLabel, 1);
            assignTranslateAnimation(mCallStateIcon, 1);
            assignTranslateAnimation(mPrimaryName, 2);
            assignTranslateAnimation(mCallNumberAndLabel, 3);
            assignTranslateAnimation(mCallTypeLabel, 4);
            assignTranslateAnimation(mCallButtonsContainer, 5);

            final Animator animator = getShrinkAnimator(parent.getHeight(), originalHeight);

            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mPrimaryCallCardContainer.setTag(R.id.view_tag_callcard_actual_height, null);
                    setViewStatePostAnimation(listener);
                    mIsAnimating = false;
                    InCallPresenter.getInstance().onShrinkAnimationComplete();
                }
            });
            animator.start();
        }
    });
}

From source file:de.anderdonau.spacetrader.Main.java

public boolean ExecuteAction(final Boolean CommanderFlees) {
    // *************************************************************************
    // A fight round
    // Return value indicates whether fight continues into another round
    // *************************************************************************
    Boolean CommanderGotHit, OpponentGotHit;
    long OpponentHull, ShipHull;
    int i;//from  w w w .j av  a  2  s  .com
    int PrevEncounterType;
    Ship Ship = gameState.Ship;
    Ship Opponent = gameState.Opponent;
    Popup popup;

    CommanderGotHit = false;
    OpponentHull = Opponent.hull;
    ShipHull = Ship.hull;

    // Fire shots
    if (gameState.EncounterType == GameState.PIRATEATTACK || gameState.EncounterType == GameState.POLICEATTACK
            || gameState.EncounterType == GameState.TRADERATTACK
            || gameState.EncounterType == GameState.SPACEMONSTERATTACK
            || gameState.EncounterType == GameState.DRAGONFLYATTACK
            || gameState.EncounterType == GameState.POSTMARIEPOLICEENCOUNTER
            || gameState.EncounterType == GameState.SCARABATTACK
            || gameState.EncounterType == GameState.FAMOUSCAPATTACK) {
        CommanderGotHit = ExecuteAttack(Opponent, Ship, CommanderFlees, true);
    }

    OpponentGotHit = false;

    if (!CommanderFlees) {
        if (gameState.EncounterType == GameState.POLICEFLEE || gameState.EncounterType == GameState.TRADERFLEE
                || gameState.EncounterType == GameState.PIRATEFLEE) {
            OpponentGotHit = ExecuteAttack(Ship, Opponent, true, false);
        } else {
            OpponentGotHit = ExecuteAttack(Ship, Opponent, false, false);
        }
    }

    if (CommanderGotHit) {
        ((FragmentEncounter) currentFragment).playerShipNeedsUpdate = true;
        Bitmap tribble = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.tribble);
        ViewGroup container = (ViewGroup) findViewById(R.id.container);
        for (i = 0; i <= GameState.TRIBBLESONSCREEN; ++i) {
            int resID = mContext.getResources().getIdentifier("tribbleButton" + String.valueOf(i), "id",
                    mContext.getPackageName());
            ImageView imageView = (ImageView) container.findViewById(resID);
            if (imageView == null) {
                continue;
            }
            //noinspection ConstantConditions
            ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(
                    imageView.getLayoutParams());
            marginParams.setMargins(gameState.GetRandom(container.getWidth() - tribble.getWidth()),
                    gameState.GetRandom(container.getHeight() - tribble.getHeight()), 0, 0);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
            imageView.setLayoutParams(layoutParams);
        }
    }
    if (OpponentGotHit) {
        ((FragmentEncounter) currentFragment).opponentShipNeedsUpdate = true;
    }

    // Determine whether someone gets destroyed
    if (Ship.hull <= 0 && Opponent.hull <= 0) {
        gameState.AutoAttack = false;
        gameState.AutoFlee = false;

        if (gameState.EscapePod) {
            EscapeWithPod();
        } else {
            popup = new Popup(this, "Both Destroyed",
                    "You and your opponent have managed to destroy each other.", "", "OK", cbShowNextPopup);
            popupQueue.push(popup);
            showNextPopup();
            btnDestroyed();
        }
        return false;
    } else if (Opponent.hull <= 0) {
        gameState.AutoAttack = false;
        gameState.AutoFlee = false;

        if (gameState.ENCOUNTERPIRATE(gameState.EncounterType) && Opponent.type != GameState.MANTISTYPE
                && gameState.PoliceRecordScore >= GameState.DUBIOUSSCORE) {
            popup = new Popup(this, "Bounty received",
                    String.format("You earned a bounty of %d cr.", GetBounty(Opponent)), "", "OK",
                    cbShowNextPopup);
            popupQueue.push(popup);
            showNextPopup();
        } else {
            popup = new Popup(this, "You win", "You have destroyed your opponent.", "", "OK", cbShowNextPopup);
            popupQueue.push(popup);
            showNextPopup();
        }
        if (gameState.ENCOUNTERPOLICE(gameState.EncounterType)) {
            ++gameState.PoliceKills;
            gameState.PoliceRecordScore += GameState.KILLPOLICESCORE;
        } else if (gameState.ENCOUNTERFAMOUS(gameState.EncounterType)) {
            if (gameState.ReputationScore < GameState.DANGEROUSREP) {
                gameState.ReputationScore = GameState.DANGEROUSREP;
            } else {
                gameState.ReputationScore += 100;
            }
            // bump news flag from attacked to ship destroyed
            gameState.replaceNewsEvent(gameState.latestNewsEvent(), gameState.latestNewsEvent() + 10);
        } else if (gameState.ENCOUNTERPIRATE(gameState.EncounterType)) {
            if (Opponent.type != GameState.MANTISTYPE) {
                gameState.Credits += GetBounty(Opponent);
                gameState.PoliceRecordScore += GameState.KILLPIRATESCORE;
                Scoop();
            }
            ++gameState.PirateKills;
        } else if (gameState.ENCOUNTERTRADER(gameState.EncounterType)) {
            ++gameState.TraderKills;
            gameState.PoliceRecordScore += GameState.KILLTRADERSCORE;
            Scoop();
        } else if (gameState.ENCOUNTERMONSTER(gameState.EncounterType)) {
            ++gameState.PirateKills;
            gameState.PoliceRecordScore += GameState.KILLPIRATESCORE;
            gameState.MonsterStatus = 2;
        } else if (gameState.ENCOUNTERDRAGONFLY(gameState.EncounterType)) {
            ++gameState.PirateKills;
            gameState.PoliceRecordScore += GameState.KILLPIRATESCORE;
            gameState.DragonflyStatus = 5;
        } else if (gameState.ENCOUNTERSCARAB(gameState.EncounterType)) {
            ++gameState.PirateKills;
            gameState.PoliceRecordScore += GameState.KILLPIRATESCORE;
            gameState.ScarabStatus = 2;
        }
        gameState.ReputationScore += 1 + (Opponent.type >> 1);
        return (false);
    } else if (Ship.hull <= 0) {
        gameState.AutoAttack = false;
        gameState.AutoFlee = false;

        if (gameState.EscapePod) {
            EscapeWithPod();
        } else {
            popup = new Popup(this, "You Lose", "Your ship has been destroyed by your opponent.", "", "OK",
                    cbShowNextPopup);
            popupQueue.push(popup);
            showNextPopup();
            btnDestroyed();
        }
        return (false);
    }

    // Determine whether someone gets away.
    if (CommanderFlees) {
        if (GameState.getDifficulty() == GameState.BEGINNER) {
            gameState.AutoAttack = false;
            gameState.AutoFlee = false;

            popup = new Popup(this, "Escaped", "You have managed to escape your opponent.",
                    "Just because this is Beginner level.", "OK", cbShowNextPopup);
            popupQueue.push(popup);
            showNextPopup();
            if (gameState.ENCOUNTERMONSTER(gameState.EncounterType)) {
                gameState.MonsterHull = Opponent.hull;
            }

            return (false);
        } else if ((gameState.GetRandom(7) + (Ship.PilotSkill() / 3))
                * 2 >= gameState.GetRandom(Opponent.PilotSkill()) * (2 + GameState.getDifficulty())) {
            gameState.AutoAttack = false;
            gameState.AutoFlee = false;
            if (CommanderGotHit) {
                popup = new Popup(this, "You Escaped", "You got hit, but still managed to escape.", "", "OK",
                        cbShowNextPopup);
                popupQueue.push(popup);
                showNextPopup();
            } else {
                popup = new Popup(this, "Escaped", "You have managed to escape your opponent.", "", "OK",
                        cbShowNextPopup);
                popupQueue.push(popup);
                showNextPopup();
            }
            if (gameState.ENCOUNTERMONSTER(gameState.EncounterType)) {
                gameState.MonsterHull = Opponent.hull;
            }

            return (false);
        }
    } else if (gameState.EncounterType == GameState.POLICEFLEE
            || gameState.EncounterType == GameState.TRADERFLEE
            || gameState.EncounterType == GameState.PIRATEFLEE
            || gameState.EncounterType == GameState.TRADERSURRENDER
            || gameState.EncounterType == GameState.PIRATESURRENDER) {
        if (gameState.GetRandom(Ship.PilotSkill()) * 4 <= gameState.GetRandom((7 + (Opponent.PilotSkill() / 3)))
                * 2) {
            gameState.AutoAttack = false;
            gameState.AutoFlee = false;
            popup = new Popup(this, "Opponent Escaped", "Your opponent has managed to escape.", "", "OK",
                    cbShowNextPopup);
            popupQueue.push(popup);
            showNextPopup();
            return (false);
        }
    }

    // Determine whether the opponent's actions must be changed
    PrevEncounterType = gameState.EncounterType;

    if (Opponent.hull < OpponentHull) {
        if (gameState.ENCOUNTERPOLICE(gameState.EncounterType)) {
            if (Opponent.hull < OpponentHull >> 1) {
                if (Ship.hull < ShipHull >> 1) {
                    if (gameState.GetRandom(10) > 5) {
                        gameState.EncounterType = GameState.POLICEFLEE;
                    }
                } else {
                    gameState.EncounterType = GameState.POLICEFLEE;
                }
            }
        } else if (gameState.EncounterType == GameState.POSTMARIEPOLICEENCOUNTER) {
            gameState.EncounterType = GameState.POLICEATTACK;
        } else if (gameState.ENCOUNTERPIRATE(gameState.EncounterType)) {
            if (Opponent.hull < (OpponentHull * 2) / 3) {
                if (Ship.hull < (ShipHull * 2) / 3) {
                    if (gameState.GetRandom(10) > 3) {
                        gameState.EncounterType = GameState.PIRATEFLEE;
                    }
                } else {
                    gameState.EncounterType = GameState.PIRATEFLEE;
                    if (gameState.GetRandom(10) > 8 && Opponent.type < GameState.MAXSHIPTYPE) {
                        gameState.EncounterType = GameState.PIRATESURRENDER;
                    }
                }
            }
        } else if (gameState.ENCOUNTERTRADER(gameState.EncounterType)) {
            if (Opponent.hull < (OpponentHull * 2) / 3) {
                if (gameState.GetRandom(10) > 3) {
                    gameState.EncounterType = GameState.TRADERSURRENDER;
                } else {
                    gameState.EncounterType = GameState.TRADERFLEE;
                }
            } else if (Opponent.hull < (OpponentHull * 9) / 10) {
                if (Ship.hull < (ShipHull * 2) / 3) {
                    // If you get damaged a lot, the trader tends to keep shooting
                    if (gameState.GetRandom(10) > 7) {
                        gameState.EncounterType = GameState.TRADERFLEE;
                    }
                } else if (Ship.hull < (ShipHull * 9) / 10) {
                    if (gameState.GetRandom(10) > 3) {
                        gameState.EncounterType = GameState.TRADERFLEE;
                    }
                } else {
                    gameState.EncounterType = GameState.TRADERFLEE;
                }
            }
        }
    }

    if (PrevEncounterType != gameState.EncounterType) {
        if (!(gameState.AutoAttack && (gameState.EncounterType == GameState.TRADERFLEE
                || gameState.EncounterType == GameState.PIRATEFLEE
                || gameState.EncounterType == GameState.POLICEFLEE))) {
            gameState.AutoAttack = false;
        }
        gameState.AutoFlee = false;
    }

    ((FragmentEncounter) currentFragment).playerShipNeedsUpdate = true;
    ((FragmentEncounter) currentFragment).opponentShipNeedsUpdate = true;

    ((FragmentEncounter) currentFragment).EncounterDisplayShips();
    ((FragmentEncounter) currentFragment).EncounterButtons();

    String buf = "The ";
    String buf2 = "";
    if (gameState.ENCOUNTERPOLICE(PrevEncounterType)) {
        buf2 = "police ship";
    } else if (gameState.ENCOUNTERPIRATE(PrevEncounterType)) {
        if (Opponent.type == GameState.MANTISTYPE) {
            buf2 = "alien ship";
        } else {
            buf2 = "pirate ship";
        }
    } else if (gameState.ENCOUNTERTRADER(PrevEncounterType)) {
        buf2 = "trader ship";
    } else if (gameState.ENCOUNTERMONSTER(PrevEncounterType)) {
        buf2 = "monster";
    } else if (gameState.ENCOUNTERDRAGONFLY(PrevEncounterType)) {
        buf2 = "Dragonfly";
    } else if (gameState.ENCOUNTERSCARAB(PrevEncounterType)) {
        buf2 = "Scarab";
    } else if (gameState.ENCOUNTERFAMOUS(PrevEncounterType)) {
        buf2 = "Captain";
    }

    buf += buf2;
    if (CommanderGotHit) {
        buf += " hits you.";
    } else if (!(PrevEncounterType == GameState.POLICEFLEE || PrevEncounterType == GameState.TRADERFLEE
            || PrevEncounterType == GameState.PIRATEFLEE)) {
        buf += " missed you.";
    } else {
        buf = "";
    }

    if (OpponentGotHit) {
        buf += "\nYou hit the " + buf2 + ".";
    }

    if (!CommanderFlees && !OpponentGotHit) {
        buf += "\n" + "You missed the " + buf2 + ".";
    }

    if (PrevEncounterType == GameState.POLICEFLEE || PrevEncounterType == GameState.TRADERFLEE
            || PrevEncounterType == GameState.PIRATEFLEE) {
        buf += "\nThe " + buf2 + " didn't get away.";
    }

    if (CommanderFlees) {
        buf += "\nThe " + buf2 + " is still following you.";
    }

    ((FragmentEncounter) currentFragment).EncounterDisplayNextAction(false);

    //noinspection ConstantConditions
    buf = ((FragmentEncounter) currentFragment).EncounterText.getText().toString() + "\n" + buf;
    ((FragmentEncounter) currentFragment).EncounterText.setText(buf);

    if (gameState.Continuous && (gameState.AutoAttack || gameState.AutoFlee)) {
        // Make sure there's always just one delayRunnable queued.
        // Otherwise several are queued if player keeps tapping Attack/Flee buttons.
        delayHandler.removeCallbacksAndMessages(null);
        gameState.CommanderFlees = CommanderFlees;
        delayHandler.postDelayed(delayRunnable, 1000);
    }
    return true;
}