package com.example.animateframe; import com.example.animateframe.model.girl; import android.app.Activity; import android.content.Intent; import android.graphics.drawable.AnimationDrawable; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.Button; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ ImageView img; Button btnStart, btnStop; AnimationDrawable animation; MediaPlayer mMediaPlayerHEAD; SeekBar seekbarS, seekbar; static int heart = 0; static int level = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Remove title bar // this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Remove notification bar // this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, // WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main_frame); // startService(new Intent(getBaseContext(), MusicService.class)); mMediaPlayerHEAD = MediaPlayer .create(getApplicationContext(), R.raw.s1); img = (ImageView) findViewById(R.id.imageView1); seekbarS = (SeekBar) findViewById(R.id.seekbarS); seekbar = (SeekBar) findViewById(R.id.seekbar); // img.setBackgroundDrawable(animation); // img.setBackgroundResource(resid) // img.setImageBitmap(null); /* * btnStart = (Button)findViewById(R.id.btnStart); //btnStop = * (Button)findViewById(R.id.btnStop); * * btnStart.setOnClickListener(new View.OnClickListener() { * * @Override public void onClick(View v) { // TODO Auto-generated method * stub img.setImageBitmap(null); animation.start(); } }); * * btnStop.setOnClickListener(new View.OnClickListener() { * * @Override public void onClick(View v) { // TODO Auto-generated method * stub animation.stop(); } }); */ img.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // if (level == 2) return false; int defaultScreenWidth = 480; int defaultScreenHeight = 800; //int defaultScreenWidth = 240; //int defaultScreenHeight = 320; // TODO Auto-generated method stub int[] viewCoords = new int[2]; img.getLocationOnScreen(viewCoords); int touchX = (int) event.getX(); int touchY = (int) event.getY(); int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate //Toast.makeText(getBaseContext(),"left = " + img.getLeft() + " top = " + img.getTop() + "bottom = " + img.getBottom() + " right = " + img.getRight() + touchX + " " + touchY + " imageX =" + imageX, Toast.LENGTH_SHORT).show(); double scaleX = (img.getRight() / defaultScreenWidth) ; double scaleY = (img.getBottom() / defaultScreenHeight); if (img.getRight() >= defaultScreenWidth ) { // man hinh lon hon scaleX = (img.getRight() / defaultScreenWidth) ; scaleY = (img.getBottom() / defaultScreenHeight); // touchX = (int) (scaleX * touchX); // touchY = (int) (scaleY * touchY); }else { // man hinh nho hon scaleX = (defaultScreenWidth / img.getRight()) ; scaleY = (defaultScreenHeight / img.getBottom() ); // touchX = (int) (scaleX * touchX); // touchY = (int) (scaleY * touchY); } scaleX = ( (double) img.getRight() / (double) defaultScreenWidth) ; //Toast.makeText(getBaseContext(), ""+ img.getRight() + "/" + defaultScreenWidth + "=" + scaleX + "* x=" + touchX + " Y =" + touchY , Toast.LENGTH_SHORT).show(); scaleY = ( (double)img.getBottom() / (double)defaultScreenHeight); int touchX2 = (800 / defaultScreenWidth ) * touchX; int touchY2 = (1280 / defaultScreenHeight ) * touchY; //Toast.makeText(getBaseContext(), ""+ (135 * scaleX) + "*" + scaleX + "* x=" + touchX + " Y =" + touchY , Toast.LENGTH_SHORT).show(); // Toast.makeText(getBaseContext()," x=" + touchX + " Y =" + touchY , Toast.LENGTH_SHORT).show(); if ((touchX >=(135 * scaleX)) && (touchX < (320* scaleX)) && (touchY >=150* scaleY) && (touchY < 280* scaleY)) // 480 x 800 // if ((touchX >= ( 100 * scaleX )) && (touchX < 200 * scaleX) && (touchY >=80 * scaleY) && (touchY < 280 * scaleY)) { if (animation.isRunning()) { animation.stop(); } if (level == 1) { img.setBackgroundResource(R.drawable.head); }else { img.setBackgroundResource(R.drawable.gc_next); } animation = (AnimationDrawable) img.getBackground(); animation.start(); if (level == 1){ if (SoundOn()) mMediaPlayerHEAD.start(); checkIfAnimationDone(animation,girl.STANDING); } else { if (SoundOn()){ MediaPlayer mMediaPlayerBUST = MediaPlayer .create(getApplicationContext(), R.raw.s8); mMediaPlayerBUST.start(); } checkIfAnimationDone(animation,girl.GA_NEXT); } } // bust if ((touchX >=150 * scaleX) && (touchX < 370 * scaleX) && (touchY >=390 * scaleY) && (touchY < 500 * scaleY)) { if (animation.isRunning()) { animation.stop(); } if (level == 1) img.setBackgroundResource(R.drawable.bustup); else img.setBackgroundResource(R.drawable.ge_next); animation = (AnimationDrawable) img.getBackground(); animation.start(); if (SoundOn()) { if (level == 1) { MediaPlayer mMediaPlayerBUST = MediaPlayer .create(getApplicationContext(), R.raw.s4); mMediaPlayerBUST.start(); }else { MediaPlayer mMediaPlayerBUST = MediaPlayer .create(getApplicationContext(), R.raw.s6); mMediaPlayerBUST.start(); } } heart += 10; seekbar.setProgress(heart); // Toast.makeText(getBaseContext()," heart= " + heart + " level " + level, Toast.LENGTH_SHORT).show(); // checkIfAnimationDone(animation,girl.GA_NEXT); // level++; if (heart <= 100) { checkIfAnimationDone(animation,girl.STANDING); } else { // Toast.makeText(getBaseContext()," heart=" + heart, Toast.LENGTH_SHORT).show(); if (level != 2 ) level = 2; checkIfAnimationDone(animation,girl.GA_NEXT); seekbar.setProgress(0); } } return false; } }); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); img.setImageBitmap(null); animation = standing(); animation.start(); } @Override public void onPause() { super.onPause(); // Always call the superclass method first if (SoundOn()) stopService(new Intent(getBaseContext(), MusicService.class)); // mMediaPlayerHEAD.stop(); } @Override public void onResume() { super.onPause(); // Always call the superclass method first if (SoundOn()) startService(new Intent(getBaseContext(), MusicService.class)); } private void checkIfAnimationDone(AnimationDrawable anim, final int doing) { final AnimationDrawable a = anim; int timeBetweenChecks = 300; Handler h = new Handler(); h.postDelayed(new Runnable() { public void run() { if (a.getCurrent() != a.getFrame(a.getNumberOfFrames() - 1)) { checkIfAnimationDone(a, doing); } else { // Toast.makeText(getApplicationContext(), // "ANIMATION DONE!", Toast.LENGTH_SHORT).show(); animation.stop(); if (doing == girl.STANDING) animation = standing(); if (doing == girl.GA_NEXT) animation = ga_next(); animation.start(); } } }, timeBetweenChecks); }; protected AnimationDrawable ga_next() { img.setBackgroundResource(R.drawable.ga_next); // Get the background, which has been compiled to an AnimationDrawable // object. AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); return frameAnimation; } protected AnimationDrawable nguc() { img.setBackgroundResource(R.drawable.bustup); // Get the background, which has been compiled to an AnimationDrawable // object. AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); return frameAnimation; } protected AnimationDrawable head() { img.setBackgroundResource(R.drawable.head); // Get the background, which has been compiled to an AnimationDrawable // object. AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); return frameAnimation; } protected AnimationDrawable standing() { img.setBackgroundResource(R.drawable.standing); // Get the background, which has been compiled to an AnimationDrawable // object. AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); return frameAnimation; // Start the animation (looped playback by default). // frameAnimation.start(); /* * * int duration = 150; BitmapDrawable frame1 = * (BitmapDrawable)getResources().getDrawable(R.drawable.a001); * BitmapDrawable frame2 = * (BitmapDrawable)getResources().getDrawable(R.drawable.a002); * BitmapDrawable frame3 = * (BitmapDrawable)getResources().getDrawable(R.drawable.a003); * BitmapDrawable frame4 = * (BitmapDrawable)getResources().getDrawable(R.drawable.a004); * BitmapDrawable frame5 = * (BitmapDrawable)getResources().getDrawable(R.drawable.a005); * BitmapDrawable frame6 = * (BitmapDrawable)getResources().getDrawable(R.drawable.a006); * BitmapDrawable frame7 = * (BitmapDrawable)getResources().getDrawable(R.drawable.a007); * BitmapDrawable frame8 = * (BitmapDrawable)getResources().getDrawable(R.drawable.a008); * BitmapDrawable frame9 = * (BitmapDrawable)getResources().getDrawable(R.drawable.a009); * BitmapDrawable frame10 = * (BitmapDrawable)getResources().getDrawable(R.drawable.a010); * * AnimationDrawable animation2 = new AnimationDrawable(); * * animation2.addFrame(frame1, duration); animation2.addFrame(frame2, * duration); animation2.addFrame(frame3, duration); * animation2.addFrame(frame4, duration); animation2.addFrame(frame5, * duration); animation2.addFrame(frame6, duration); * animation2.addFrame(frame7, duration); animation2.addFrame(frame8, * duration); animation2.addFrame(frame9, duration); * animation2.addFrame(frame10, duration); * * animation2.setOneShot(false); return animation2; */ } private boolean SoundOn() { return false; } }
Tuesday, 11 June 2013
getX and getY different screen android
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment