Skip to content

Instantly share code, notes, and snippets.

@DarrienG
Last active February 17, 2017 15:17
Show Gist options
  • Save DarrienG/14f34e2c784d502144a491dfd3d8e56f to your computer and use it in GitHub Desktop.
Save DarrienG/14f34e2c784d502144a491dfd3d8e56f to your computer and use it in GitHub Desktop.

The code called to initialize views in onCreate()

       youStockView.setAttributes(
            normalizedNamePair.first, game.getSelfCharacter(), game.getSelfStocksLeft());

        opponentStockView.setAttributes(
            normalizedNamePair.second, game.getOpponentCharacter(), game.getOpponentStocksLeft());

And this is how the views are initialized:

    /**
     * Helper method: Used to initialized and prepare view.
     */
    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.challenger_view, this, true);
        ButterKnife.bind(this);

        challengerText.setText(challenger);

        stockList = new ArrayList<>(4);
        stockList.addAll(Arrays.asList(stockOneView, stockTwoView, stockThreeView, stockFourView));

        challengerText.setVisibility(GONE);
        challengerText.setAlpha(0);

        // Here we loop through the ImageViews provide assets for them, and decide if the stock
        // was taken by an opponent or not. If it was, we set it to greyscale.
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0);

        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
        int count = 0;
        for (ImageView view: stockList) {
            int resource = getResources().getIdentifier(
                character.getIcon(), "mipmap", "com.rowdyboiz.smashkeeper");

            Drawable d = getResources().getDrawable(resource, getContext().getTheme());

            if (count >= stocksLeft) {
                view.setColorFilter(filter);
            }

            ++count;

            view.setImageDrawable(d);

            view.setVisibility(GONE);
            view.setAlpha(0f);
        }

        getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                getViewTreeObserver().removeOnPreDrawListener(this);

                ViewCompat.animate(challengerText).alpha(1).setDuration(500);

                int delay = 0;
                for (ImageView view: stockList) {
                    ViewCompat.animate(view).alpha(1).setStartDelay(delay).setDuration(900);
                    delay += 250;
                }

                return false;
            }
        });

        challengerText.setVisibility(VISIBLE);
        for (View v: stockList) {
            v.setVisibility(VISIBLE);
        }

        if (animationFinishedListener != null) {
            animationFinishedListener.onFinished();
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment