Created
October 24, 2012 18:42
-
-
Save FROGGS/3947990 to your computer and use it in GitHub Desktop.
things
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 behman.app.monk; | |
import android.os.Bundle; | |
import android.widget.FrameLayout; | |
import android.app.Activity; | |
public class MainActivity extends Activity { | |
private MonkView monkview; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
monkview = new MonkView(this); | |
FrameLayout fl = (FrameLayout) findViewById(R.id.framelayout0); | |
fl.addView(monkview); | |
} | |
} | |
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 behman.app.monk; | |
import android.content.Context; | |
import android.content.res.Resources; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.os.Handler; | |
import android.view.KeyEvent; | |
import android.view.MotionEvent; | |
import android.view.View; | |
public class MonkView extends View | |
{ | |
private float xpos = -1; | |
private float ypos = -1; | |
private Bitmap monkBitmap; | |
private int touchX; | |
private int touchY; | |
int p; | |
public MonkView(Context c) { | |
super(c); | |
// lade Hintergrund | |
this.setBackgroundResource(R.drawable.hintergrund); | |
// Lade Monk | |
Resources resmonk1 = getResources(); | |
monkBitmap = BitmapFactory.decodeResource(resmonk1, R.drawable.monk_black_front); | |
setFocusable(true); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
if (xpos == -1 && ypos == -1) { | |
xpos = getWidth()/2; | |
ypos = getHeight()/2; | |
} | |
Paint iconPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
if (monkBitmap != null) { | |
switch (p) { | |
case 1: | |
Resources resmonk1 = getResources(); | |
monkBitmap = BitmapFactory.decodeResource(resmonk1, R.drawable.monk_black_front); | |
break; | |
case 2: | |
Resources resmonk2 = getResources(); | |
monkBitmap = BitmapFactory.decodeResource(resmonk2, R.drawable.monk_moveleft_leftfoot_front); | |
break; | |
case 3: | |
Resources resmonk3 = getResources(); | |
monkBitmap = BitmapFactory.decodeResource(resmonk3, R.drawable.monk_moveleft_rightfoot_front); | |
break; | |
case 4: | |
Resources resmonk4 = getResources(); | |
monkBitmap = BitmapFactory.decodeResource(resmonk4, R.drawable.monk_moveright_leftfoot_front); | |
break; | |
case 5: | |
Resources resmonk5 = getResources(); | |
monkBitmap = BitmapFactory.decodeResource(resmonk5, R.drawable.monk_moveright_rightfoot_front); | |
break; | |
case 6: | |
Resources resmonk6 = getResources(); | |
monkBitmap = BitmapFactory.decodeResource(resmonk6, R.drawable.monk_black_leftfoot_front); | |
break; | |
case 7: | |
Resources resmonk7 = getResources(); | |
monkBitmap = BitmapFactory.decodeResource(resmonk7, R.drawable.monk_black_rightfoot_front); | |
break; | |
case 8: | |
Resources resmonk8 = getResources(); | |
monkBitmap = BitmapFactory.decodeResource(resmonk8, R.drawable.monk_black_leftfoot_front_back); | |
break; | |
case 9: | |
Resources resmonk9 = getResources(); | |
monkBitmap = BitmapFactory.decodeResource(resmonk9, R.drawable.monk_black_rightfoot_front_back); | |
break; | |
} | |
canvas.drawBitmap(monkBitmap, xpos - monkBitmap.getWidth()/2, ypos - monkBitmap.getHeight()/2, iconPaint); | |
} | |
} | |
@Override | |
public boolean onKeyDown(int keyCode, KeyEvent event) { | |
if(keyCode == KeyEvent.KEYCODE_DPAD_UP) { | |
ypos -=5; | |
} | |
if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { | |
xpos +=5; | |
} | |
if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { | |
ypos +=5; | |
} | |
if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) { | |
xpos -=5; | |
} | |
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { | |
xpos = getWidth()/2; | |
ypos = getHeight()/2; | |
} | |
//invalidate(); | |
return super.onKeyDown(keyCode, event); | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
int aktion = event.getAction(); | |
if(aktion == MotionEvent.ACTION_DOWN){ | |
touchX = (int) event.getX(); | |
touchY = (int) event.getY(); | |
if (xpos < touchX) | |
{ | |
while(xpos <= (touchX-1)) | |
{ | |
p=4; | |
xpos++ ; | |
p=5; | |
//invalidate(); | |
} | |
} | |
else if(xpos > touchX) | |
{ | |
while(xpos >= (touchX +1)) | |
{ | |
p=2; | |
xpos--; | |
p=3; | |
} | |
} | |
else if (ypos < touchY) | |
{ | |
while(ypos <= (touchY-1)) | |
{ | |
p=6; | |
ypos++; | |
p=7; | |
} | |
} | |
else if(ypos > touchY) | |
{ | |
while((ypos >= touchY+1)) | |
{ | |
p=8; | |
ypos--; | |
p=9; | |
} | |
} | |
} | |
invalidate(); | |
return super.onTouchEvent(event); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment