in app/src/main/java/com/google/reviewit/widget/SelectCodeReviewView.java [65:210]
public SelectCodeReviewView(Context context, AttributeSet attrs) {
super(context, attrs);
this.widgetUtil = new WidgetUtil(context);
setOrientation(VERTICAL);
RelativeLayout votingPanel = new RelativeLayout(context);
votingPanel.setLayoutParams(new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, widgetUtil.dpToPx(200)));
addView(votingPanel);
VotingButton voteMinus1 = new VotingButton(context,
Background.Corner.LEFT_TOP,
R.drawable.ic_exposure_neg_1_white_48dp,
R.drawable.ic_sentiment_dissatisfied_white_48dp,
R.color.votingNegative,
R.color.votingNegativeSelected,
-1);
VotingButton voteMinus2 = new VotingButton(context,
Background.Corner.LEFT_BOTTOM,
R.drawable.ic_exposure_neg_2_white_48dp,
R.drawable.ic_sentiment_very_dissatisfied_white_48dp,
R.color.votingNegative,
R.color.votingNegativeSelected,
-2);
VotingButton votePlus1 = new VotingButton(context,
Background.Corner.RIGHT_TOP,
R.drawable.ic_exposure_plus_1_white_48dp,
R.drawable.ic_sentiment_satisfied_white_48dp,
R.color.votingPositive,
R.color.votingPositiveSelected,
1);
VotingButton votePlus2 = new VotingButton(context,
Background.Corner.RIGHT_BOTTOM,
R.drawable.ic_exposure_plus_2_white_48dp,
R.drawable.ic_sentiment_very_satisfied_white_48dp,
R.color.votingPositive,
R.color.votingPositiveSelected,
2);
votingButtons.addAll(
Arrays.asList(voteMinus1, voteMinus2, votePlus1, votePlus2));
LinearLayout votePanels = new LinearLayout(context);
votePanels.setLayoutParams(matchLayout());
votePanels.setOrientation(LinearLayout.VERTICAL);
votingPanel.addView(votePanels);
LinearLayout votePanel1 = new LinearLayout(context);
votePanel1.setLayoutParams(matchAndWrapLayout());
votePanel1.setOrientation(LinearLayout.HORIZONTAL);
votePanels.addView(votePanel1);
votePanel1.addView(voteMinus1);
votePanel1.addView(votePlus1);
LinearLayout votePanel2 = new LinearLayout(context);
votePanel2.setLayoutParams(matchAndWrapLayout());
votePanel2.setOrientation(LinearLayout.HORIZONTAL);
votePanels.addView(votePanel2);
votePanel2.addView(voteMinus2);
votePanel2.addView(votePlus2);
emoticon = new ImageView(context);
RelativeLayout.LayoutParams emoticonParams =
new RelativeLayout.LayoutParams(
widgetUtil.dpToPx(110), widgetUtil.dpToPx(110));
emoticonParams.addRule(RelativeLayout.CENTER_IN_PARENT,
RelativeLayout.TRUE);
emoticon.setLayoutParams(emoticonParams);
emoticon.setImageDrawable(
widgetUtil.getDrawable(R.drawable.ic_sentiment_neutral_white_48dp));
emoticon.setColorFilter(
widgetUtil.color(R.color.votingNeutral));
votingPanel.addView(emoticon);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Rect emoticonBounds = new Rect(emoticon.getLeft(), emoticon.getTop(),
emoticon.getRight(), emoticon.getBottom());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if (applyEvent(emoticonBounds, event)) {
emoticon.setImageDrawable(widgetUtil.getDrawable(
R.drawable.ic_sentiment_neutral_white_48dp));
emoticon.setColorFilter(widgetUtil.color(R.color.votingNeutral));
for (VotingButton b : votingButtons) {
if (!b.isDisabled()) {
WidgetUtil.setBackground(b, b.getBackgroundDrawable());
}
}
} else {
boolean selected = false;
for (VotingButton b : votingButtons) {
if (!b.isDisabled()) {
if (applyEvent(b, event)) {
WidgetUtil.setBackground(b,
b.getBackgroundDrawableSelected());
b.applyIcon(emoticon);
selected = true;
} else {
WidgetUtil.setBackground(b, b.getBackgroundDrawable());
}
}
if (!selected) {
emoticon.setImageDrawable(widgetUtil.getDrawable(
R.drawable.ic_sentiment_neutral_white_48dp));
emoticon.setColorFilter(
widgetUtil.color(R.color.votingNeutral));
}
}
}
break;
case MotionEvent.ACTION_UP:
checkState(change != null, "Change not set");
Integer vote = null;
if (applyEvent(emoticonBounds, event)) {
vote = 0;
} else {
for (VotingButton b : votingButtons) {
if (!b.isDisabled() && applyEvent(b, event)) {
vote = b.getVote();
}
}
}
if (vote != null && onVoteListener != null) {
onVoteListener.vote(vote);
}
break;
default:
break;
}
return true;
}
private boolean applyEvent(VotingButton b, MotionEvent event) {
return applyEvent(b.getBounds(), event);
}
private boolean applyEvent(Rect bounds, MotionEvent event) {
return bounds.contains((int) event.getX() - getPaddingLeft(),
(int) event.getY() - getPaddingTop());
}
});
}