Android 上的居中缩放
我成功地创建了一个缩放(扩展视图),以便将 PNG 加载到可绘制对象中,并通过缩放和平移功能将其显示在屏幕上。请参阅视图的代码(Zoom.java):
public class Zoom extends View {
private Drawable image;
private int zoomControler = 200;
private int shiftleft = 0;
private int shiftup = 0 ;
public float oldX = 0;
public float oldY = 0;
public Zoom(Context context) {
super(context);
image = context.getResources().getDrawable(R.drawable.androidmarker);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
image.setBounds(getWidth() / 2 - zoomControler - shiftleft,
getHeight() / 2 - zoomControler - shiftup,
getWidth()/ 2 + zoomControler - shiftleft,
getHeight()/ 2 + zoomControler - shiftup);
image.draw(canvas);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
zoomControler += 10;
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
zoomControler -= 10;
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
{
shiftleft +=10;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
{
shiftleft-=10;
}
if (zoomControler < 10)
zoomControler = 10;
invalidate();
return true;
}
@Override
public boolean onTouchEvent(MotionEvent e) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
oldX = e.getX();
oldY = e.getY();
break;
case MotionEvent.ACTION_MOVE:
shiftleft+=(int) (-(e.getX() - oldX) ) / 2;
shiftup+=(int) (-(e.getY() - oldY) / 2);
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
invalidate();
return true;
}
}
问题是如何获得屏幕居中缩放?目前,缩放的中心始终是 PNG 文件的中心...我不知道如何修改 setBounds 以获得此行为...知道吗? 谢谢 ;)
I've managed to create a Zoom (extends View) in order to load a PNG into a Drawable object and have it on the screen with zooming and panning function. See the code of the View (Zoom.java) :
public class Zoom extends View {
private Drawable image;
private int zoomControler = 200;
private int shiftleft = 0;
private int shiftup = 0 ;
public float oldX = 0;
public float oldY = 0;
public Zoom(Context context) {
super(context);
image = context.getResources().getDrawable(R.drawable.androidmarker);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
image.setBounds(getWidth() / 2 - zoomControler - shiftleft,
getHeight() / 2 - zoomControler - shiftup,
getWidth()/ 2 + zoomControler - shiftleft,
getHeight()/ 2 + zoomControler - shiftup);
image.draw(canvas);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
zoomControler += 10;
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
zoomControler -= 10;
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
{
shiftleft +=10;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
{
shiftleft-=10;
}
if (zoomControler < 10)
zoomControler = 10;
invalidate();
return true;
}
@Override
public boolean onTouchEvent(MotionEvent e) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
oldX = e.getX();
oldY = e.getY();
break;
case MotionEvent.ACTION_MOVE:
shiftleft+=(int) (-(e.getX() - oldX) ) / 2;
shiftup+=(int) (-(e.getY() - oldY) / 2);
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
invalidate();
return true;
}
}
The problem is how I get a screen centered zoom ? For the moment, the center of the zoom is always the center of the PNG file...I don't know how to modified setBounds in order to get this behaviour... Any idea ?
Thanks ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论