package MyBitMap_1.code;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
public class MyView extends View {
private static int mScreenHeight;
private static int mScreenWidth;
private double fScale = 1.0;
private int xOffset = 0;
private int yOffset = 0;
private int xDist = 0;
private int yDist = 0;
private int xOffset_T = 0;
private int yOffset_T = 0;
private Bitmap bitMap1;
public MyView(Context context )
{
super(context);
Initialize();
}
private void Initialize()
{
DisplayMetrics dm = getResources().getDisplayMetrics();
mScreenWidth = dm.widthPixels;
mScreenHeight = dm.heightPixels;
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.trail );
bitMap1 = bmp;
xDist = mScreenWidth;
yDist = mScreenHeight;
}
@Override
protected void onDraw(Canvas canvas )
{
canvas.drawColor(Color.WHITE);
xDist = (int)(mScreenWidth);
yDist = (int)(mScreenHeight);
Rect src = new Rect((int)(xOffset*fScale),(int)(yOffset*fScale),(int)((xDist+xOffset)*fScale),(int)((yDist+yOffset)*fScale));
Log.i("Rollrock","OnDraw" + xOffset + " " + xDist );
Rect dest = new Rect(0,0,mScreenWidth,mScreenHeight);
canvas.drawBitmap(bitMap1, src,dest, null);
}
@Override
public boolean onKeyDown(int keyCode , KeyEvent event )
{
Log.i("Rollrock", "" + keyCode + " " + event.getAction() + " " + event.getKeyCode());
if( KeyEvent.KEYCODE_DPAD_LEFT == keyCode )
{
}
else if( KeyEvent.KEYCODE_DPAD_RIGHT == keyCode )
{
}
else if( KeyEvent.KEYCODE_DPAD_UP == keyCode )
{
fScale += 0.1;
this.invalidate();
}
else if( KeyEvent.KEYCODE_DPAD_DOWN == keyCode )
{
fScale -= 0.1;
this.invalidate();
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event )
{
int action = event.getAction();
int x = 0;
int y = 0;
switch(action)
{
case MotionEvent.ACTION_DOWN:
xOffset_T = (int)event.getX();
yOffset_T = (int)event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
x = (int)event.getX();
y = (int)event.getY();
xOffset += xOffset_T - x;
yOffset += yOffset_T - y;
this.invalidate();
break;
}
return true;
}
}