Paste
Pasted as Plain Text by narendra ( 11 years ago )
package com.ripl.TrackHekxoGame3;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
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.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;
import com.ripl.Global.Constants;
import com.ripl.HekxoGame1.R;
public class Track_PlayActivity extends Activity implements OnTouchListener {
// private LinearLayout verticalOuterLayout;
private ScrollView verticalScrollview;
private ImageView ivObject;
private Timer scrollTimer = null;
private TimerTask clickSchedule;
private TimerTask scrollerSchedule;
private TimerTask faceAnimationSchedule;
private int scrollPos = 0;
private Timer clickTimer = null;
private Timer faceTimer = null;
ViewGroup _root;
private static FrameLayout mRootLayout;
ImageView imageView;
LayoutParams params;
int tagId = 1;
static int fmLayoutWidth, fmLayoutHeight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.track_game_play_layout);
init();
startAutoScrolling();
}
public void startAutoScrolling() {
if (scrollTimer == null) {
scrollTimer = new Timer();
final Runnable Timer_Tick = new Runnable() {
public void run() {
moveScrollView();
}
};
if (scrollerSchedule != null) {
scrollerSchedule.cancel();
scrollerSchedule = null;
}
scrollerSchedule = new TimerTask() {
@Override
public void run() {
runOnUiThread(Timer_Tick);
}
};
scrollTimer.schedule(scrollerSchedule, 30, 30);
}
}
public void moveScrollView() {
scrollPos = (int) (verticalScrollview.getScrollY() + 1.0);
if (scrollPos == 1) {
verticalScrollview.fullScroll(View.FOCUS_DOWN);
}
System.out.println("------------"+verticalScrollview.getScrollY());
verticalScrollview.scrollBy(scrollPos, -3);
if (mRootLayout != null) {
isTouchInView(mRootLayout, event);
}
}
public void stopAutoScrolling() {
if (scrollTimer != null) {
scrollTimer.cancel();
scrollTimer = null;
}
}
private void clearTimers(Timer timer) {
if (timer != null) {
timer.cancel();
timer = null;
}
}
private void clearTimerTaks(TimerTask timerTask) {
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
}
// ------------------------------------------
private int _xDelta;
private int _yDelta;
private static View viewObject;
MotionEvent event;
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) view
.getLayoutParams();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
viewObject = view;
break;
case MotionEvent.ACTION_UP:
viewObject = view;
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
lParams.leftMargin = X - _xDelta;
lParams.topMargin = Y - _yDelta;
viewObject = view;
x = X;
y = Y;
if (mRootLayout != null) {
fmLayoutWidth = mRootLayout.getWidth();
fmLayoutHeight = mRootLayout.getHeight();
// if (mRootLayout != null) {
// isTouchInView(mRootLayout, event);
// }
}
view.setLayoutParams(lParams);
break;
}
_root.invalidate();
this.event = event;
x = X;
y = Y;
return true;
}
// --------------------------
private int score = 0, objectCount = 0;
private TextView tvScore, tvObjectCount;
private MediaPlayer mPlayer;
private ImageView ivSoundOff, ivSoundOn;
private RelativeLayout rlTitleBar;
boolean isDialogOpen, isPlayView, isPause;
private void init() {
score = 0;
isDialogOpen = false;
objectCount = 0;
mRootLayout = (FrameLayout) findViewById(R.id.fm0);
verticalScrollview = (ScrollView) findViewById(R.id.vertical_scrollview_id);
_root = (ViewGroup) findViewById(R.id.vertical_outer_layout_id);
ivObject = (ImageView) findViewById(R.id.imageView);
verticalScrollview.fullScroll(View.FOCUS_DOWN);
ivObject.setOnTouchListener(this);
tvScore = (TextView) findViewById(R.id.tvScore);
tvObjectCount = (TextView) findViewById(R.id.tvObjectCount);
rlTitleBar = (RelativeLayout) findViewById(R.id.rlTitleBar);
ivSoundOff = (ImageView) findViewById(R.id.ivSoundOff);
ivSoundOff.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ivSoundOff.setClickable(false);
ivSoundOn.setClickable(true);
ivSoundOn.setVisibility(View.VISIBLE);
ivSoundOff.setVisibility(View.GONE);
// if(mPlayer!=null)
// if (!(mPlayer.isPlaying() || mPlayer.isLooping()))
mPlayer = Constants.soundPlay(Track_PlayActivity.this,"traphekxogame");
}
});
ivSound findViewById(R.id.ivSoundOn);
ivSoundOn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ivSoundOn.setVisibility(View.GONE);
ivSoundOff.setVisibility(View.VISIBLE);
ivSoundOff.setClickable(true);
ivSoundOn.setClickable(false);
soundStop();
}
});
mPlayer = Constants.soundPlay(Track_PlayActivity.this, "trackhekxogame");
}
public void onPause() {
soundStop();
super.onPause();
}
public void onDestroy() {
clearTimerTaks(clickSchedule);
clearTimerTaks(scrollerSchedule);
clearTimerTaks(faceAnimationSchedule);
clearTimers(scrollTimer);
clearTimers(clickTimer);
clearTimers(faceTimer);
clickSchedule = null;
scrollerSchedule = null;
faceAnimati
scrollTimer = null;
clickTimer = null;
faceTimer = null;
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
super.onDestroy();
}
@Override
protected void onStop() {
soundStop();
super.onStop();
}
private void soundStop() {
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
@Override
public void onBackPressed() {
soundStop();
super.onBackPressed();
}
// -----------------
public Bitmap loadBitmapFromView(View v) {
Bitmap b1 = Bitmap.createBitmap(fmLayoutWidth, fmLayoutHeight,
Bitmap.Config.ARGB_8888);
Bitmap b = Bitmap.createBitmap(b1, x, y, 80, 80);
b1.recycle();
FrameLayout frame = (FrameLayout) v;
Canvas c = new Canvas(b);
if (viewObject != null)
frame.layout(mRootLayout.getLeft(), mRootLayout.getTop(),
mRootLayout.getRight(), mRootLayout.getBottom());
frame.draw(c);
return b;
}
// private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
// Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(),
// bmp1.getHeight(), bmp1.getConfig());
// Canvas canvas = new Canvas(bmOverlay);
// canvas.drawBitmap(bmp1, new Matrix(), null);
// canvas.drawBitmap(bmp2, x, y, null);
// return bmOverlay;
// }
int x;
int y;
private boolean isTouchInView(View view, MotionEvent event) {
fmLayoutWidth = 1280;// mRootLayout.getWidth();
fmLayoutHeight = 2289;// mRootLayout.getHeight();
System.out.println("fmLayoutWidth---" + fmLayoutWidth
+ "--fmLayoutHeight--" + fmLayoutHeight);
Bitmap bitmap = loadBitmapFromView(mRootLayout);
// x = (int) event.getRawX();
// y = (int) event.getRawY();
System.out.println("isTouchInView -x---" + x + "--yyyyyy--" + y);
int pixel1 = bitmap.getPixel(0, 0);
int pixel2 = bitmap.getPixel(71, 77);
int pixel3 = bitmap.getPixel(71, 0);
int pixel4 = bitmap.getPixel(0, 77);
String hex1 = convetRGBtoHexaDecimal(pixel1);
String hex2 = convetRGBtoHexaDecimal(pixel2);
String hex3 = convetRGBtoHexaDecimal(pixel3);
String hex4 = convetRGBtoHexaDecimal(pixel4);
System.out.println("----h1-----" + hex1+"----h2-----" + hex2+"----h3-----" + hex3+"----h4-----" + hex4);
/**
* if ([LeftpxColor isEqualToString:@"#FFE400"] || [LeftpxColor
* isEqualToString:@"#FFE50F"] || [RightpxColor
* isEqualToString:@"#FFE400"] || [RightpxColor
* isEqualToString:@"#FFE50F"] || [toppxColor
* isEqualToString:@"#FFE400"] || [toppxColor
* isEqualToString:@"#FFE50F"] || [BottompxColor
* isEqualToString:@"#FFE400"] || [BottompxColor
* isEqualToString:@"#FFE50F"])
*/
if (hex1.contains("#FFE400") || hex1.contains("#FFE50F")||hex2.contains("#FFE400") || hex2.contains("#FFE50F")||hex3.contains("#FFE400") || hex3.contains("#FFE50F")||hex4.contains("#FFE400") || hex4.contains("#FFE50F")) {
score++;
}
tvScore.setText("" + score);
return true;
}
private String convetRGBtoHexaDecimal(int pixel) {
// TODO Auto-generated method stub
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
return String.format("#XXX", redValue, greenValue, blueValue);
}
}
Revise this Paste