This repository was archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 413
Expand file tree
/
Copy pathBoardView.java
More file actions
198 lines (168 loc) · 6.17 KB
/
BoardView.java
File metadata and controls
198 lines (168 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.snatik.matches.ui;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.LinearLayout;
import com.snatik.matches.R;
import com.snatik.matches.common.Shared;
import com.snatik.matches.events.ui.FlipCardEvent;
import com.snatik.matches.model.BoardArrangment;
import com.snatik.matches.model.BoardConfiguration;
import com.snatik.matches.model.Game;
import com.snatik.matches.utils.Utils;
public class BoardView extends LinearLayout {
private LinearLayout.LayoutParams mRowLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
private LinearLayout.LayoutParams mTileLayoutParams;
private int mScreenWidth;
private int mScreenHeight;
private BoardConfiguration mBoardConfiguration;
private BoardArrangment mBoardArrangment;
private Map<Integer, TileView> mViewReference;
private List<Integer> flippedUp = new ArrayList<Integer>();
private boolean mLocked = false;
private int mSize;
private MediaPlayer mediaPlayer;
public BoardView(Context context) {
this(context, null);
}
public BoardView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
setOrientation(LinearLayout.VERTICAL);
setGravity(Gravity.CENTER);
int margin = getResources().getDimensionPixelSize(R.dimen.margine_top);
int padding = getResources().getDimensionPixelSize(R.dimen.board_padding);
mScreenHeight = getResources().getDisplayMetrics().heightPixels - margin - padding*2;
mScreenWidth = getResources().getDisplayMetrics().widthPixels - padding*2 - Utils.px(20);
mViewReference = new HashMap<Integer, TileView>();
setClipToPadding(false);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
}
public static BoardView fromXml(Context context, ViewGroup parent) {
return (BoardView) LayoutInflater.from(context).inflate(R.layout.board_view, parent, false);
}
public void setBoard(Game game) {
mBoardConfiguration = game.boardConfiguration;
mBoardArrangment = game.boardArrangment;
// calc prefered tiles in width and height
int singleMargin = getResources().getDimensionPixelSize(R.dimen.card_margin);
float density = getResources().getDisplayMetrics().density;
singleMargin = Math.max((int) (1 * density), (int) (singleMargin - mBoardConfiguration.difficulty * 2 * density));
int sumMargin = 0;
for (int row = 0; row < mBoardConfiguration.numRows; row++) {
sumMargin += singleMargin * 2;
}
int tilesHeight = (mScreenHeight - sumMargin) / mBoardConfiguration.numRows;
int tilesWidth = (mScreenWidth - sumMargin) / mBoardConfiguration.numTilesInRow;
mSize = Math.min(tilesHeight, tilesWidth);
mTileLayoutParams = new LinearLayout.LayoutParams(mSize, mSize);
mTileLayoutParams.setMargins(singleMargin, singleMargin, singleMargin, singleMargin);
// build the ui
buildBoard();
}
/**
* Build the board
*/
private void buildBoard() {
for (int row = 0; row < mBoardConfiguration.numRows; row++) {
// add row
addBoardRow(row);
}
setClipChildren(false);
}
private void addBoardRow(int rowNum) {
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
for (int tile = 0; tile < mBoardConfiguration.numTilesInRow; tile++) {
addTile(rowNum * mBoardConfiguration.numTilesInRow + tile, linearLayout);
}
// add to this view
addView(linearLayout, mRowLayoutParams);
linearLayout.setClipChildren(false);
}
private void addTile(final int id, ViewGroup parent) {
final TileView tileView = TileView.fromXml(getContext(), parent);
tileView.setLayoutParams(mTileLayoutParams);
parent.addView(tileView);
parent.setClipChildren(false);
mViewReference.put(id, tileView);
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
return mBoardArrangment.getTileBitmap(id, mSize);
}
@Override
protected void onPostExecute(Bitmap result) {
tileView.setTileImage(result);
}
}.execute();
tileView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mLocked && tileView.isFlippedDown()) {
tileView.flipUp();
mediaPlayer = MediaPlayer.create(Shared.context, R.raw.click_on);
mediaPlayer.start();
flippedUp.add(id);
if (flippedUp.size() == 2) {
mLocked = true;
}
Shared.eventBus.notify(new FlipCardEvent(id));
}
}
});
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(tileView, "scaleX", 0.8f, 1f);
scaleXAnimator.setInterpolator(new BounceInterpolator());
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(tileView, "scaleY", 0.8f, 1f);
scaleYAnimator.setInterpolator(new BounceInterpolator());
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(scaleXAnimator, scaleYAnimator);
animatorSet.setDuration(500);
tileView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
animatorSet.start();
}
public void flipDownAll() {
for (Integer id : flippedUp) {
mViewReference.get(id).flipDown();
}
flippedUp.clear();
mLocked = false;
}
public void hideCards(int id1, int id2) {
animateHide(mViewReference.get(id1));
animateHide(mViewReference.get(id2));
flippedUp.clear();
mLocked = false;
}
protected void animateHide(final TileView v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(v, "alpha", 0f);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
v.setLayerType(View.LAYER_TYPE_NONE, null);
v.setVisibility(View.INVISIBLE);
}
});
animator.setDuration(100);
v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
animator.start();
}
}