Java 映射比屏幕大的图像
我有一个android应用程序,带有可平移(可滚动)图像。 用户应该能够在屏幕上按下大理石,但只有当按下棕色大理石时,才会启动特定操作。 因为图像是可滚动的,当用户移动图像时,棕色的弹珠会在屏幕上有其他位置,所以我想知道我应该怎么做。 添加:屏幕截图和平移图像的代码。Java 映射比屏幕大的图像,java,android,image,screen,Java,Android,Image,Screen,我有一个android应用程序,带有可平移(可滚动)图像。 用户应该能够在屏幕上按下大理石,但只有当按下棕色大理石时,才会启动特定操作。 因为图像是可滚动的,当用户移动图像时,棕色的弹珠会在屏幕上有其他位置,所以我想知道我应该怎么做。 添加:屏幕截图和平移图像的代码。 公共类Map1扩展活动{ //物理显示宽度和高度。 私有静态int displayWidth=0; 私有静态int-displayHeight=0; /**在首次创建活动时调用*/ @抑制警告(“弃用”) @凌驾 创建时的公共v
公共类Map1扩展活动{
//物理显示宽度和高度。
私有静态int displayWidth=0;
私有静态int-displayHeight=0;
/**在首次创建活动时调用*/
@抑制警告(“弃用”)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//displayWidth和displayHeight将根据屏幕的不同而变化
//要动态获取这些,我们应该钩住onSizeChanged()。
//这个简单的例子只使用横向模式,所以可以得到它们
//一旦启动,在整个过程中使用这些值。
显示=((WindowManager)
getSystemService(Context.WINDOW_服务)).getDefaultDisplay();
displayWidth=display.getWidth();
displayHeight=display.getHeight();
//SampleView构造函数必须最后构造,因为它需要
//显示宽度和显示高度。
setContentView(新的SampleView(this));
}
私有静态类SampleView扩展视图{
私有静态位图bmLargeImage;//大到可以滚动的位图
private static Rect displayRect=null;//显示到的Rect
private Rect scrollRect=null;//Rect我们用它在位图上滚动
private int scrollRectX=0;//scroll rect的当前左位置
private int scrollRectY=0;//scroll rect的当前顶部位置
private float scrollByX=0;//x要滚动的数量
私有浮动滚动y=0;//滚动y的数量
private float startX=0;//从一个动作到下一个动作的轨迹x
private float startY=0;//从一个动作移动到下一个动作时跟踪y
公共样本视图(上下文){
超级(上下文);
//主画布绘制的目标矩形。它永远不会改变。
displayRect=新的Rect(0,0,displayWidth,displayHeight);
//Scroll rect:这将用于在屏幕上“滚动”
//内存中的位图。如上所述初始化。
scrollRect=新的Rect(0,0,displayWidth,displayHeight);
//将大位图加载到内存的屏幕外区域。
bmLargeImage=BitmapFactory.decodeResource(getResources(),
R.可拉深的大理石);
}
@凌驾
公共布尔onTouchEvent(运动事件){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
//记住我们最初的下降事件位置。
startX=event.getRawX();
startY=event.getRawY();
打破
case MotionEvent.ACTION\u移动:
float x=event.getRawX();
float y=event.getRawY();
//计算移动更新。这将发生多次
//在一个动作手势的过程中。
scrollByX=x-startX;//移动更新x增量
scrollByY=y-startY;//移动更新y增量
startX=x;//将初始值重置为最新值
startY=y;
invalidate();//强制重画
打破
}
return true;//此事件已完成,请使用它
}
@凌驾
受保护的void onDraw(画布){
//我们的移动更新是在相反方向的移动中计算的
//从我们想要移动滚动条的方式。将此视为拖动到
//左侧与向右滑动滚动条相同。
int newScrollRectX=scrollRectX-(int)scrollByX;
int newscollrecty=scrollRectY-(int)scrollByY;
//不要滚动位图的左边缘或右边缘。
if(newScrollRectX<0)
newScrollRectX=0;
else if(newScrollRectX>(bmLargeImage.getWidth()-displayWidth))
newScrollRectX=(bmLargeImage.getWidth()-displayWidth);
//不要滚动位图的上边缘或下边缘。
if(newscollrecty<0)
newscollrecty=0;
else if(newscollrecty>(bmLargeImage.getHeight()-displayHeight))
newScrollRectY=(bmLargeImage.getHeight()-displayHeight);
//我们有更新的滚动矩形坐标,设置它们并绘制。
scrollRect.set(newScrollRectX、newScrollRectY、,
newScrollRectX+displayWidth、newScrollRectY+displayHeight);
油漆=新油漆();
drawBitmap(bmLargeImage、scrollRect、displayRect、paint);
//重置当前滚动坐标以反映最新更新,
//所以我们可以重复这个更新过程。
scrollRectX=newScrollRectX;
scrollRectY=newScrollRecy;
}
}
public class Map1 extends Activity {
// Physical display width and height.
private static int displayWidth = 0;
private static int displayHeight = 0;
/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// displayWidth and displayHeight will change depending on screen
// orientation. To get these dynamically, we should hook onSizeChanged().
// This simple example uses only landscape mode, so it's ok to get them
// once on startup and use those values throughout.
Display display = ((WindowManager)
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
displayWidth = display.getWidth();
displayHeight = display.getHeight();
// SampleView constructor must be constructed last as it needs the
// displayWidth and displayHeight we just got.
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private static Bitmap bmLargeImage; //bitmap large enough to be scrolled
private static Rect displayRect = null; //rect we display to
private Rect scrollRect = null; //rect we scroll over our bitmap with
private int scrollRectX = 0; //current left location of scroll rect
private int scrollRectY = 0; //current top location of scroll rect
private float scrollByX = 0; //x amount to scroll by
private float scrollByY = 0; //y amount to scroll by
private float startX = 0; //track x from one ACTION_MOVE to the next
private float startY = 0; //track y from one ACTION_MOVE to the next
public SampleView(Context context) {
super(context);
// Destination rect for our main canvas draw. It never changes.
displayRect = new Rect(0, 0, displayWidth, displayHeight);
// Scroll rect: this will be used to 'scroll around' over the
// bitmap in memory. Initialize as above.
scrollRect = new Rect(0, 0, displayWidth, displayHeight);
// Load a large bitmap into an offscreen area of memory.
bmLargeImage = BitmapFactory.decodeResource(getResources(),
R.drawable.marbles);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Remember our initial down event location.
startX = event.getRawX();
startY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float x = event.getRawX();
float y = event.getRawY();
// Calculate move update. This will happen many times
// during the course of a single movement gesture.
scrollByX = x - startX; //move update x increment
scrollByY = y - startY; //move update y increment
startX = x; //reset initial values to latest
startY = y;
invalidate(); //force a redraw
break;
}
return true; //done with this event so consume it
}
@Override
protected void onDraw(Canvas canvas) {
// Our move updates are calculated in ACTION_MOVE in the opposite direction
// from how we want to move the scroll rect. Think of this as dragging to
// the left being the same as sliding the scroll rect to the right.
int newScrollRectX = scrollRectX - (int)scrollByX;
int newScrollRectY = scrollRectY - (int)scrollByY;
// Don't scroll off the left or right edges of the bitmap.
if (newScrollRectX < 0)
newScrollRectX = 0;
else if (newScrollRectX > (bmLargeImage.getWidth() - displayWidth))
newScrollRectX = (bmLargeImage.getWidth() - displayWidth);
// Don't scroll off the top or bottom edges of the bitmap.
if (newScrollRectY < 0)
newScrollRectY = 0;
else if (newScrollRectY > (bmLargeImage.getHeight() - displayHeight))
newScrollRectY = (bmLargeImage.getHeight() - displayHeight);
// We have our updated scroll rect coordinates, set them and draw.
scrollRect.set(newScrollRectX, newScrollRectY,
newScrollRectX + displayWidth, newScrollRectY + displayHeight);
Paint paint = new Paint();
canvas.drawBitmap(bmLargeImage, scrollRect, displayRect, paint);
// Reset current scroll coordinates to reflect the latest updates,
// so we can repeat this update process.
scrollRectX = newScrollRectX;
scrollRectY = newScrollRectY;
}
}