Android Open Source - ihatecolor_and Question View Group






From Project

Back to project page ihatecolor_and.

License

The source code is released under:

Copyright (c) 2013, Sewon Ann All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * ...

If you think the Android project ihatecolor_and listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package kr.pe.kingori.ihatecolor.ui.view;
//from   w  w w. j av a  2s .c  om
import android.content.Context;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.TextView;
import kr.pe.kingori.ihatecolor.R;
import kr.pe.kingori.ihatecolor.model.Color;
import kr.pe.kingori.ihatecolor.util.FontManager;
import kr.pe.kingori.ihatecolor.util.UiUtil;

import java.util.ArrayList;

import static kr.pe.kingori.ihatecolor.ui.fragment.GameFragment.Question;

public class QuestionViewGroup extends RelativeLayout {

    private ArrayList<Question> question;
    private int currentPosition = -1;

    public QuestionViewGroup(Context context) {
        super(context);
    }

    public QuestionViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public QuestionViewGroup(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setQuestion(final ArrayList<Question> question) {
        this.question = question;
        currentPosition = 0;
        if (getMeasuredWidth() > 0) {
            layoutQuestion(question, getMeasuredWidth());
        } else {
            getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    layoutQuestion(question, getMeasuredWidth());
                    getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });
        }
    }

    private float textSize;

    private void layoutQuestion(ArrayList<Question> question, int measuredWidth) {
        removeAllViews();

        int spaceBetweenItems = getResources().getDimensionPixelSize(R.dimen.question_padding);

        Color[] colors = Color.values();
        int[] colorTextWidth = new int[colors.length];
        TextPaint tp = new TextPaint(0);
        FontManager.applyTypeface(tp);
        textSize = getResources().getDimension(UiUtil.isWideDevice(getContext()) ? R.dimen.question_text_size_wide : R.dimen.question_text_size);
        tp.setTextSize(textSize);
        for (int i = 0; i < colors.length; i++) {
            colorTextWidth[i] = (int) (tp.measureText(getContext().getResources().getString(colors[i].nameResId)) * 1.1 + 0.5)
                    + getResources().getDimensionPixelSize(R.dimen.question_padding);
        }

        int currentRow = 0;
        int widthSum = 0;
        int rowHeight = getResources().getDimensionPixelSize(R.dimen.question_height);
        ArrayList<Question> questionsInRow = new ArrayList<Question>();
        Question q;
        for (int i = 0; i < question.size(); i++) {
            q = question.get(i);
            int qWidth = colorTextWidth[q.text.ordinal()];
            if (widthSum + qWidth >= measuredWidth) {
                addQuestionViews(questionsInRow, measuredWidth, widthSum, currentRow, rowHeight, colorTextWidth);
                questionsInRow.clear();
                widthSum = 0;
                currentRow++;
            }
            questionsInRow.add(q);
            widthSum += qWidth + spaceBetweenItems;
        }
        addQuestionViews(questionsInRow, measuredWidth, widthSum, currentRow, rowHeight, colorTextWidth);
        updateQestionColor();
    }


    private void addQuestionViews(ArrayList<Question> questionsInRow, int measuredWidth, int widthSum, int currentRow, int rowHeight, int[] colorTextWidth) {
        int leftMargin = (measuredWidth - widthSum) / 2;
        int padding = getResources().getDimensionPixelSize(R.dimen.question_padding);
        int spaceBetweenRows = padding / 2;
        int marginTop = (rowHeight + spaceBetweenRows) * currentRow;


//        int textSizeSp = UiUtil.isWideDevice(getContext()) ? 14 : 26;

        for (Question question : questionsInRow) {
            CustomFontTextView tv = new CustomFontTextView(getContext());
            tv.setText(question.text.nameResId);
            tv.setTextColor(getResources().getColor(question.answer.colorResId));
            tv.setGravity(Gravity.CENTER);
            tv.setIncludeFontPadding(false);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
//            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeSp);
            tv.setPadding(0, padding, 0, padding / 2);

            int width = colorTextWidth[question.text.ordinal()];

            LayoutParams lParams = new LayoutParams(width, rowHeight);
            lParams.setMargins(leftMargin, marginTop, 0, 0);
            addView(tv, lParams);
            leftMargin += width + padding;
        }
    }

    public void setCurrent(int position) {
        currentPosition = position;
        updateQestionColor();
    }

    private void updateQestionColor() {
        int color = getResources().getColor(R.color.current_question_bg);

        for (int i = 0; i < currentPosition; i++) {
            TextView target = (TextView) getChildAt(i);
            target.setBackgroundColor(android.graphics.Color.TRANSPARENT);
            target.setTextColor(color);
        }

        if (currentPosition >= 0 && getChildCount() > currentPosition) {
            getChildAt(currentPosition).setBackgroundColor(color);
        }
    }
}




Java Source Code List

com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.GameHelper.java
kr.pe.kingori.ihatecolor.Application.java
kr.pe.kingori.ihatecolor.model.Color.java
kr.pe.kingori.ihatecolor.model.GameMode.java
kr.pe.kingori.ihatecolor.ui.Constants.java
kr.pe.kingori.ihatecolor.ui.CustomDialogFragment.java
kr.pe.kingori.ihatecolor.ui.activity.MainActivity.java
kr.pe.kingori.ihatecolor.ui.activity.SplashActivity.java
kr.pe.kingori.ihatecolor.ui.event.DialogEvent.java
kr.pe.kingori.ihatecolor.ui.event.GameEvent.java
kr.pe.kingori.ihatecolor.ui.event.PlayEvent.java
kr.pe.kingori.ihatecolor.ui.fragment.BaseFragment.java
kr.pe.kingori.ihatecolor.ui.fragment.GameFragment.java
kr.pe.kingori.ihatecolor.ui.fragment.MainFragment.java
kr.pe.kingori.ihatecolor.ui.fragment.WaitingFragment.java
kr.pe.kingori.ihatecolor.ui.view.CustomFontButton.java
kr.pe.kingori.ihatecolor.ui.view.CustomFontTextView.java
kr.pe.kingori.ihatecolor.ui.view.QuestionViewGroup.java
kr.pe.kingori.ihatecolor.util.FontManager.java
kr.pe.kingori.ihatecolor.util.SharedPreferenceUtil.java
kr.pe.kingori.ihatecolor.util.UiUtil.java