我试图创建一个简单的应用程序,在图像视图中加载位图图像,可以缩放和滚动。我可以放大和缩小并滚动图像视图,但它总是超出边框(黑屏)。虽然我复制了一个非常有用的代码来帮助我,但imageview仍然没有屏幕。能帮我解决这个问题吗? 发布完整代码,有点太长了。 提前谢谢。
package something;
public class LoadMap extends Activity {
ZoomControls zoom;
ImageView img;
ImageButton linkButton;
float startX, startY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_map);
Resources res=getResources();
final Bitmap mBitmap = BitmapFactory.decodeResource(res, R.drawable.map);
BitmapDrawable bDrawable = new BitmapDrawable(res, mBitmap);
//get the size of the image and the screen
int bitmapWidth = bDrawable.getIntrinsicWidth();
int bitmapHeight = bDrawable.getIntrinsicHeight();
@SuppressWarnings("deprecation")
int screenWidth = this.getWindowManager().getDefaultDisplay().getWidth();
@SuppressWarnings("deprecation")
int screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
img = (ImageView) this.findViewById(R.id.imageView1);
// set maximum scroll amount (based on center of image)
final int maxX = (int)((bitmapWidth / 2) - (screenWidth/ 2));
final int maxY = (int)((bitmapHeight / 2) - (screenHeight / 2));
// set scroll limits
final int maxLeft = (maxX * -1);
final int maxRight = maxX;
final int maxTop = (maxY * -1);
final int maxBottom = maxY;
zoom = (ZoomControls) findViewById(R.id.zoomControls1);
linkButton = (ImageButton) findViewById(R.id.info);
startX = img.getScaleX();
startY = img.getScaleY();
img.setImageBitmap(mBitmap);
final int pointX = (int) img.getX();
final int pointY = (int)img.getY();
img.setOnTouchListener(new View.OnTouchListener() {
float downX, downY;
int totalX, totalY;
int scrollByX, scrollByY;
@Override
public boolean onTouch(View view, MotionEvent event) {
float currentX, currentY;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
currentX = event.getX();
currentY = event.getY();
scrollByX = (int)(downX - currentX);
scrollByY = (int)(downY - currentY);
// scrolling to left side of image (pic moving to the right)
if (currentX > downX) {
if (totalX == maxLeft) {
scrollByX = 0;
}
if (totalX > maxLeft) {
totalX = totalX + scrollByX;
}
if (totalX < maxLeft) {
scrollByX = maxLeft - (totalX - scrollByX);
totalX = maxLeft;
}
}
// scrolling to right side of image (pic moving to the left)
if (currentX < downX) {
if (totalX == maxRight) {
scrollByX = 0;
}
if (totalX < maxRight) {
totalX = totalX + scrollByX;
}
if (totalX > maxRight) {
scrollByX = maxRight - (totalX - scrollByX);
totalX = maxRight;
}
}
// scrolling to top of image (pic moving to the bottom)
if (currentY > downY) {
if (totalY == maxTop) {
scrollByY = 0;
}
if (totalY > maxTop) {
totalY = totalY + scrollByY;
}
if (totalY < maxTop) {
scrollByY = maxTop - (totalY - scrollByY);
totalY = maxTop;
}
}
// scrolling to bottom of image (pic moving to the top)
if (currentY < downY) {
if (totalY == maxBottom) {
scrollByY = 0;
}
if (totalY < maxBottom) {
totalY = totalY + scrollByY;
}
if (totalY > maxBottom) {
scrollByY = maxBottom - (totalY - scrollByY);
totalY = maxBottom;
}
}
img.scrollBy(scrollByX, scrollByY);
downX = currentX;
downY = currentY;
break;
}
return true;
}
});
linkButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("https://www.facebook.com/infopoint.bg");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
zoom.setOnZoomInClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
float x = img.getScaleX();
float y = img.getScaleY();
img.setScaleX((float) (x+1));
img.setScaleY((float) (y+1));
}
});
zoom.setOnZoomOutClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
float x = img.getScaleX();
float y = img.getScaleY();
img.scrollTo(pointX, pointY);
if((x>startX) & (y>startY)) {
img.setScaleX(startX);
img.setScaleY(startY);
}
}
});
}
}
我对onTouch方法很感兴趣,我在这里尝试&#34;计算最大边框,但我可以将顶部和底部滚动到黑屏,当尝试向左和向右滚动时,我只能滚动图像的3/4。任何帮助将不胜感激。