If you think the Android project adventure.datetime listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package ca.cmput301f13t03.adventure_datetime.view.treeView;
/*www.java2s.com*/import android.graphics.*;
class Camera
{
// these will be overwritten early on
privatestaticfinalfloat DEFAULT_WIDTH = 500;
privatestaticfinalfloat DEFAULT_HEIGHT = 1000;
// x, y position of the top left of the screen in world coordinates
privatefloat x = 0;
privatefloat y = 0;
// size of the view
privatefloat m_viewWidth = DEFAULT_WIDTH;
privatefloat m_viewHeight = DEFAULT_HEIGHT;
// coords we want to keep centered
privatefloat m_xTarget = 0 - m_viewWidth / 2;
privatefloat m_yTarget = 0 - m_viewWidth / 2;
privatefloat m_zoomLevel = 1.0f;
private Matrix m_transform = null;
private Matrix m_inverse = null;
private Object m_syncLock = new Object();
publicvoid DrawLocal(Canvas canvas, Paint paint, Path p)
{
synchronized (m_syncLock)
{
p.transform(this.GetTransform());
canvas.drawPath(p, paint);
p.transform(this.GetInverseTransform());
}
}
publicvoid DrawLocal(Canvas canvas, Paint paint, Bitmap image, int x, int y)
{
synchronized (m_syncLock)
{
Matrix trans = new Matrix(GetTransform());
trans.setTranslate(x - this.x, y - this.y);
canvas.drawBitmap(image, trans, paint);
}
}
publicvoid DrawLocal(Canvas canvas, Paint paint, String text, int centerX, int centerY)
{
synchronized (m_syncLock)
{
float points[] = { centerX, centerY };
this.GetTransform().mapPoints(points);
canvas.drawText(text, points[0], points[1], paint);
}
}
private Matrix GetTransform()
{
Matrix result = null;
synchronized (m_syncLock)
{
if(m_transform == null)
{
m_transform = new Matrix();
m_transform.setScale(m_zoomLevel, m_zoomLevel);
m_transform.setTranslate(-this.x, -this.y);
}
result = m_transform;
}
return result;
}
private Matrix GetInverseTransform()
{
Matrix result = null;
synchronized (m_syncLock)
{
if(m_inverse == null)
{
m_inverse = new Matrix();
m_inverse.setScale(1.0f / m_zoomLevel, 1.0f / m_zoomLevel);
m_inverse.setTranslate(this.x, this.y);
}
result = m_inverse;
}
return result;
}
publicfloat GetXTarget()
{
return m_xTarget;
}
publicfloat GetYTarget()
{
return m_yTarget;
}
publicvoid ResizeView(float width, float height)
{
synchronized (m_syncLock)
{
m_viewWidth = width;
m_viewHeight = height;
m_inverse = null;
m_transform = null;
}
LookAt(m_xTarget, m_yTarget);
}
publicvoid LookAt(float x, float y)
{
synchronized (m_syncLock)
{
m_xTarget = x;
m_yTarget = y;
this.x = m_xTarget - m_viewWidth / 2;
this.y = m_yTarget - m_viewHeight / 2;
m_inverse = null;
m_transform = null;
}
}
publicvoid ScreenCordsToWorldCords(float[] screenCords)
{
synchronized (m_syncLock)
{
GetInverseTransform().mapPoints(screenCords);
}
}
}