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();
}
}