Created
January 15, 2014 06:57
-
-
Save BbAndroid/8431993 to your computer and use it in GitHub Desktop.
drag-drop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package game.jigsawgame; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.graphics.Typeface; | |
import android.graphics.drawable.Drawable; | |
import android.media.MediaPlayer; | |
import android.media.MediaPlayer.OnCompletionListener; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.util.Log; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.view.View.OnTouchListener; | |
import android.view.ViewGroup; | |
import android.widget.ImageButton; | |
import android.widget.ImageView; | |
import android.widget.RelativeLayout; | |
import android.widget.TextView; | |
public class JigThree extends Activity implements OnClickListener, | |
OnTouchListener { | |
private static final String TAG = JigThree.class.getSimpleName(); | |
ImageButton btnSound, btnHelp, btnHome; | |
MediaPlayer soundEffect; | |
boolean status2 = true;// play/stop sound click | |
boolean state = true;// status stop or resume music | |
Music music; | |
// allDialog | |
Context context = JigThree.this; | |
Class<?> next = JigFour.class; | |
int soundclick = R.raw.soundclick; | |
int soundlock = R.raw.toop; | |
int imagehelp, view = 0; | |
int name = 3; | |
// View | |
ViewGroup re1; | |
private int xDelta = 0; | |
private int yDelta = 0; | |
int topy1, leftX1, rightX1, bottomY1; | |
int topy2, leftX2, rightX2, bottomY2; | |
int topy3, leftX3, rightX3, bottomY3; | |
int topy4, leftX4, rightX4, bottomY4; | |
ImageView imgDrop4, imgDrop1, imgDrop2, imgDrop3, piece1, piece2, piece3, | |
piece4; | |
int Mar1X, Mar1Y, Mar2X, Mar2Y, Mar3X, Mar3Y, Mar4X, Mar4Y; | |
// array ImageView | |
int[] imageViewId = { R.id.piece1, R.id.piece2, R.id.piece3, R.id.piece4 }; | |
ImageView[] iv = new ImageView[imageViewId.length]; | |
int[] imageID = { R.drawable.rpiz1, R.drawable.rpiz2, R.drawable.rpiz3, | |
R.drawable.rpiz4 }; | |
// Timer | |
TimerThread timerthread; | |
TextView timerText; | |
int mins, secs; | |
Handler mainHandler; | |
boolean stop = false; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.three_jigsaw); | |
// get intent from SelectedActivity | |
try { | |
Bundle extra = getIntent().getExtras(); | |
if (extra != null) { | |
status2 = extra.getBoolean("sound"); | |
state = extra.getBoolean("state"); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
// button home | |
btnHome = (ImageButton) findViewById(R.id.btn_home); | |
btnHome.setOnClickListener(this); | |
// button help | |
btnHelp = (ImageButton) findViewById(R.id.btn_help); | |
btnHelp.setOnClickListener(this); | |
// button sound | |
btnSound = (ImageButton) findViewById(R.id.btn_sound); | |
btnSound.setOnClickListener(this); | |
// link piece | |
for (int i = 0; i < imageViewId.length; i++) { | |
iv[i] = (ImageView) findViewById(imageViewId[i]); | |
iv[i].setOnTouchListener(this); | |
} | |
// Random Piece Jigsaw Location | |
int[] data = RandomPiece.randomIntArray(imageViewId.length, 0, | |
imageID.length - 1); | |
// -1 because array of image has 0-3 | |
for (int i = 0; i < data.length; i++) { | |
if (data[i] == 3) { | |
iv[i].setId(imageViewId[3]); | |
} else if (data[i] == 2) { | |
iv[i].setId(imageViewId[2]); | |
} else if (data[i] == 1) { | |
iv[i].setId(imageViewId[1]); | |
} else if (data[i] == 0) { | |
iv[i].setId(imageViewId[0]); | |
} | |
iv[i].setBackgroundResource(imageID[data[i]]); | |
} | |
// play music this stage | |
if (status2 == true) { | |
music = new Music(context, name, status2); | |
state = true; | |
btnSound.setBackgroundResource(R.drawable.button_game_sound); | |
} else { | |
btnSound.setBackgroundResource(R.drawable.button_game_sound2); | |
} | |
// link Drop | |
imgDrop4 = (ImageView) findViewById(R.id.imgDrop4); | |
imgDrop1 = (ImageView) findViewById(R.id.imgDrop1); | |
imgDrop2 = (ImageView) findViewById(R.id.imgDrop2); | |
imgDrop3 = (ImageView) findViewById(R.id.imgDrop3); | |
// link RelativeLayout | |
re1 = (ViewGroup) findViewById(R.id.re1); | |
// set font | |
Typeface FontFace = Typeface.createFromAsset(getAssets(), | |
"font/FORTE.TTF"); | |
// link text | |
timerText = (TextView) findViewById(R.id.clock); | |
timerText.setTypeface(FontFace); | |
try { | |
ReciveMessage(); | |
timerthread = new TimerThread(timerText, mins, secs, mainHandler); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
// check time | |
private void ReciveMessage() { | |
mainHandler = new Handler() { | |
@Override | |
public void handleMessage(Message msg) { | |
super.handleMessage(msg); | |
try { | |
if (msg.what == 1) { | |
stop = msg.getData().getBoolean("stop"); | |
if (stop == true) { | |
timeOutAlert(); | |
} | |
} | |
if (msg.what == 0) { | |
mins = msg.getData().getInt("mins"); | |
secs = msg.getData().getInt("secs"); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
} | |
@Override | |
public void onClick(View v) { | |
if (mins == 1 && secs == 30) { | |
v = null; | |
} else { | |
switch (v.getId()) { | |
case R.id.btn_sound: | |
if (status2 == false) { | |
btnSound.setBackgroundResource(R.drawable.button_game_sound); | |
music = new Music(context, name, status2); | |
state = true; | |
status2 = true; | |
} else { | |
playSound(context, soundclick); | |
btnSound.setBackgroundResource(R.drawable.button_game_sound2); | |
if (state == true) { | |
music.onPause(); | |
state = false; | |
} | |
status2 = false; | |
} | |
break; | |
case R.id.btn_home: | |
if (status2 == true) { | |
playSound(context, soundclick); | |
} | |
// Pause Time | |
timerthread.PauseTime(); | |
ExitAlert(); | |
break; | |
case R.id.btn_help: | |
if (status2 == true) { | |
playSound(context, soundclick); | |
} | |
HelpAlert(); | |
} | |
} | |
} | |
// soundEffect | |
private void playSound(Context context, int sound) { | |
try { | |
if (soundEffect != null && soundEffect.isPlaying()) { | |
soundEffect.stop(); | |
soundEffect.release(); | |
soundEffect = null; | |
} | |
} catch (IllegalStateException e) { | |
e.printStackTrace(); | |
} | |
soundEffect = MediaPlayer.create(context, sound); | |
soundEffect.start(); | |
soundEffect.setOnCompletionListener(new OnCompletionListener() { | |
@Override | |
public void onCompletion(MediaPlayer mp) { | |
soundEffect.release(); | |
soundEffect = null; | |
} | |
}); | |
} | |
// Drag & Drop | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
final int X = (int) event.getRawX(); | |
final int Y = (int) event.getRawY(); | |
if (mins == 1 && secs == 30) { | |
v = null; | |
} else { | |
switch (event.getAction() & MotionEvent.ACTION_MASK) { | |
case MotionEvent.ACTION_DOWN: | |
if (status2 == true) { | |
playSound(context, soundclick); | |
} | |
topy1 = imgDrop1.getTop(); | |
leftX1 = imgDrop1.getLeft(); | |
rightX1 = imgDrop1.getRight(); | |
bottomY1 = imgDrop1.getBottom(); | |
topy2 = imgDrop2.getTop(); | |
leftX2 = imgDrop2.getLeft(); | |
rightX2 = imgDrop2.getRight(); | |
bottomY2 = imgDrop2.getBottom(); | |
topy3 = imgDrop3.getTop(); | |
leftX3 = imgDrop3.getLeft(); | |
rightX3 = imgDrop3.getRight(); | |
bottomY3 = imgDrop3.getBottom(); | |
topy4 = imgDrop4.getTop(); | |
leftX4 = imgDrop4.getLeft(); | |
rightX4 = imgDrop4.getRight(); | |
bottomY4 = imgDrop4.getBottom(); | |
Mar1X = (int) imgDrop1.getX(); | |
Mar1Y = (int) imgDrop1.getY(); | |
Mar2X = (int) imgDrop2.getX(); | |
Mar2Y = (int) imgDrop2.getY(); | |
Mar3X = (int) imgDrop3.getX(); | |
Mar3Y = (int) imgDrop3.getY(); | |
Mar4X = (int) imgDrop4.getX(); | |
Mar4Y = (int) imgDrop4.getY(); | |
RelativeLayout.LayoutParams Params = (RelativeLayout.LayoutParams) v | |
.getLayoutParams(); | |
xDelta = X - Params.leftMargin; | |
yDelta = Y - Params.topMargin; | |
v.bringToFront(); | |
break; | |
case MotionEvent.ACTION_MOVE: | |
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v | |
.getLayoutParams(); | |
layoutParams.leftMargin = X - xDelta; | |
layoutParams.topMargin = Y - yDelta; | |
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( | |
RelativeLayout.LayoutParams.WRAP_CONTENT, | |
RelativeLayout.LayoutParams.WRAP_CONTENT); | |
int width = getWindowManager().getDefaultDisplay().getWidth(); | |
int height = getWindowManager().getDefaultDisplay().getHeight(); | |
if (layoutParams.leftMargin < 0) { | |
layoutParams.leftMargin = 0; | |
} | |
if (layoutParams.topMargin < 0) { | |
layoutParams.topMargin = 0; | |
} | |
if (v.findViewById(R.id.piece1) != null) { | |
// border p1 | |
if (layoutParams.leftMargin > width | |
- v.findViewById(R.id.piece1).getWidth()) { | |
layoutParams.leftMargin = width | |
- v.findViewById(R.id.piece1).getWidth(); | |
} | |
if (layoutParams.topMargin > height | |
- v.findViewById(R.id.piece1).getHeight()) { | |
layoutParams.topMargin = height | |
- v.findViewById(R.id.piece1).getHeight(); | |
} | |
} | |
if (v.findViewById(R.id.piece2) != null) { | |
// border p2 | |
if (layoutParams.leftMargin > width | |
- v.findViewById(R.id.piece2).getWidth()) { | |
layoutParams.leftMargin = width | |
- v.findViewById(R.id.piece2).getWidth(); | |
} | |
if (layoutParams.topMargin > height | |
- v.findViewById(R.id.piece2).getHeight()) { | |
layoutParams.topMargin = height | |
- v.findViewById(R.id.piece2).getHeight(); | |
} | |
} | |
if (v.findViewById(R.id.piece3) != null) { | |
// border p3 | |
if (layoutParams.leftMargin > width | |
- v.findViewById(R.id.piece3).getWidth()) { | |
layoutParams.leftMargin = width | |
- v.findViewById(R.id.piece3).getWidth(); | |
} | |
if (layoutParams.topMargin > height | |
- v.findViewById(R.id.piece3).getHeight()) { | |
layoutParams.topMargin = height | |
- v.findViewById(R.id.piece3).getHeight(); | |
} | |
} | |
if (v.findViewById(R.id.piece4) != null) { | |
// border p4 | |
if (layoutParams.leftMargin > width | |
- v.findViewById(R.id.piece4).getWidth()) { | |
layoutParams.leftMargin = width | |
- v.findViewById(R.id.piece4).getWidth(); | |
} | |
if (layoutParams.topMargin > height | |
- v.findViewById(R.id.piece4).getHeight()) { | |
layoutParams.topMargin = height | |
- v.findViewById(R.id.piece4).getHeight(); | |
} | |
} | |
// lock p1 | |
if (layoutParams.leftMargin > leftX1 | |
&& layoutParams.leftMargin < rightX1 | |
&& layoutParams.topMargin > topy1 | |
&& layoutParams.topMargin < bottomY1) { | |
if (v.findViewById(R.id.piece1) != null) { | |
Drawable temp = v.getBackground(); | |
imgDrop1.setBackgroundDrawable(temp); | |
lp.setMargins(Mar1X, Mar1Y, 0, 0); | |
imgDrop1.setLayoutParams(lp); | |
v.setVisibility(View.INVISIBLE); | |
v.findViewById(R.id.piece1).setEnabled(false); | |
playSound(context, soundlock); | |
} | |
} | |
// lock p2 | |
else if (layoutParams.leftMargin > leftX2 | |
&& layoutParams.leftMargin < rightX2 | |
&& layoutParams.topMargin > topy2 | |
&& layoutParams.topMargin < bottomY2) { | |
if (v.findViewById(R.id.piece2) != null) { | |
Drawable temp = v.getBackground(); | |
imgDrop2.setBackgroundDrawable(temp); | |
lp.setMargins(Mar2X, Mar2Y, 0, 0); | |
imgDrop2.setLayoutParams(lp); | |
v.setVisibility(View.INVISIBLE); | |
v.findViewById(R.id.piece2).setEnabled(false); | |
playSound(context, soundlock); | |
} | |
} | |
// lock p3 | |
else if (layoutParams.leftMargin > leftX3 | |
&& layoutParams.leftMargin < rightX3 | |
&& layoutParams.topMargin > topy3 | |
&& layoutParams.topMargin < bottomY3) { | |
if (v.findViewById(R.id.piece3) != null) { | |
Drawable temp = v.getBackground(); | |
imgDrop3.setBackgroundDrawable(temp); | |
lp.setMargins(Mar3X, Mar3Y, 0, 0); | |
imgDrop3.setLayoutParams(lp); | |
v.setVisibility(View.INVISIBLE); | |
v.findViewById(R.id.piece3).setEnabled(false); | |
playSound(context, soundlock); | |
} | |
} | |
// lock p4 | |
else if (layoutParams.leftMargin > leftX4 | |
&& layoutParams.leftMargin < rightX4 | |
&& layoutParams.topMargin > topy4 | |
&& layoutParams.topMargin < bottomY4) { | |
if (v.findViewById(R.id.piece4) != null) { | |
Drawable temp = v.getBackground(); | |
imgDrop4.setBackgroundDrawable(temp); | |
lp.setMargins(Mar4X, Mar4Y, 0, 0); | |
imgDrop4.setLayoutParams(lp); | |
v.setVisibility(View.INVISIBLE); | |
v.findViewById(R.id.piece4).setEnabled(false); | |
playSound(context, soundlock); | |
} | |
} | |
v.setLayoutParams(layoutParams); | |
break; | |
case MotionEvent.ACTION_UP: | |
break; | |
} | |
imgDrop2.bringToFront(); | |
if (imgDrop1.getBackground() != null | |
&& imgDrop2.getBackground() != null | |
&& imgDrop3.getBackground() != null | |
&& imgDrop4.getBackground() != null) { | |
Log.d("count++", "complete"); | |
// Pause Time | |
timerthread.PauseTime(); | |
if (mins == 1 && secs == 30) { | |
v = null; | |
} else { | |
// Dialogs | |
Complete(); | |
} | |
} | |
} | |
re1.invalidate(); | |
return true; | |
} | |
private void Complete() { | |
try { | |
imagehelp = 0; | |
view = 5; | |
AllDialog complete = new AllDialog(context, null, status2, | |
soundclick, imagehelp, view, mins, secs, next, name); | |
// animation | |
complete.getWindow().getAttributes().windowAnimations = R.style.dialog_animation2; | |
complete.show(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
// timeOut | |
private void timeOutAlert() { | |
try { | |
imagehelp = 0; | |
view = 4; | |
AllDialog timeOut = new AllDialog(context, null, status2, | |
soundclick, imagehelp, view, mins, secs, null, name); | |
timeOut.show(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
// Help | |
private void HelpAlert() { | |
try { | |
imagehelp = R.drawable.help3; | |
view = 2; | |
AllDialog help = new AllDialog(context, null, status2, soundclick, | |
imagehelp, view, mins, secs, null, name); | |
// ////////////////not repeat | |
help.show(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
// ExitGame | |
private void ExitAlert() { | |
try { | |
imagehelp = 0; | |
view = 1; | |
AllDialog exit = new AllDialog(context, timerthread, status2, | |
soundclick, imagehelp, view, mins, secs, null, name); | |
// ////////////////not repeat | |
exit.show(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void onBackPressed() { | |
super.onBackPressed(); | |
Log.d(TAG, "BackPressed..."); | |
Intent back = new Intent(context, StageSelected.class); | |
back.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
back.putExtra("soundClick", status2); | |
back.putExtra("state", state); | |
startActivity(back); | |
// Stop music | |
if (state == true) { | |
music.onPause(); | |
state = false; | |
} | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
Log.d(TAG, "Start..."); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
Log.d(TAG, "Resume..."); | |
// Resume time | |
timerthread.ResumeTime(); | |
try { | |
if (state == false && status2 == true) { | |
music = new Music(context, name, status2); | |
state = true; | |
} | |
} catch (IllegalStateException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
Log.d(TAG, "Pause..."); | |
// Pause time | |
timerthread.PauseTime(); | |
if (state == true) { | |
music.onPause(); | |
state = false; | |
} | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
Log.d(TAG, "Stop..."); | |
try { | |
if (soundEffect != null && soundEffect.isPlaying()) { | |
soundEffect.stop(); | |
soundEffect.release(); | |
soundEffect = null; | |
} | |
} catch (IllegalStateException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
Log.d(TAG, "Destroy..."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/parent" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@drawable/easy3" > | |
<ImageView | |
android:id="@+id/imgBoard" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="320dp" | |
android:layout_marginTop="50dp" | |
android:background="@drawable/rac_board" /> | |
<ImageButton | |
android:id="@+id/btn_sound" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="18dp" | |
android:layout_marginTop="474dp" | |
android:background="@drawable/button_game_sound" /> | |
<ImageButton | |
android:id="@+id/btn_help" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="112dp" | |
android:layout_marginTop="475dp" | |
android:background="@drawable/button_game_help" /> | |
<ImageButton | |
android:id="@+id/btn_home" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="199dp" | |
android:layout_marginTop="473dp" | |
android:background="@drawable/button_game_home" /> | |
<ImageView | |
android:id="@+id/imgSticker" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="788dp" | |
android:layout_marginTop="448dp" | |
android:background="@drawable/sticker_time3" /> | |
<TextView | |
android:id="@+id/clock" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="829dp" | |
android:layout_marginTop="472dp" | |
android:text="@string/clock" | |
android:textColor="#000000" | |
android:textSize="30sp" /> | |
<ImageView | |
android:id="@+id/imgDrop4" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:layout_marginLeft="324dp" | |
android:layout_marginTop="179dp" /> | |
<ImageView | |
android:id="@+id/imgDrop1" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:layout_marginLeft="396dp" | |
android:layout_marginTop="209dp" | |
android:background="#FF00FF" /> | |
<ImageView | |
android:id="@+id/imgDrop3" | |
android:layout_width="45dp" | |
android:layout_height="45dp" | |
android:layout_marginLeft="379dp" | |
android:layout_marginTop="54dp" /> | |
<ImageView | |
android:id="@+id/imgDrop2" | |
android:layout_width="45dp" | |
android:layout_height="45dp" | |
android:layout_marginLeft="549dp" | |
android:layout_marginTop="199dp" | |
android:background="#00FF00" /> | |
<RelativeLayout | |
android:id="@+id/re1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" > | |
<ImageView | |
android:id="@+id/piece4" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="625dp" | |
android:layout_marginTop="40dp" | |
android:background="@drawable/rpiz4" /> | |
<ImageView | |
android:id="@+id/piece1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="60dp" | |
android:layout_marginTop="120dp" | |
android:background="@drawable/rpiz1" /> | |
<ImageView | |
android:id="@+id/piece2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="60dp" | |
android:layout_marginTop="290dp" | |
android:background="@drawable/rpiz2" /> | |
<ImageView | |
android:id="@+id/piece3" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="700dp" | |
android:layout_marginTop="250dp" | |
android:background="@drawable/rpiz3" /> | |
</RelativeLayout> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment