2
我從啓用圖像的滾動和縮放的tut中獲得該代碼,但即使圖像很小,代碼也會拉伸圖像,以便填充整個屏幕。我希望它有一個可能包含圖像的對話框,並通過修改代碼來啓用縮放和滾動,而不佔用整個屏幕。Image Zoom Over縮放
編輯:使用下面的代碼可以限制滾動或限制圖片的大小嗎? 第一個顯示屏上有一個默認圖像尺寸(居中),這也是最小縮小。當圖像達到其最大可滾動大小(實際圖像大小)時,它將回到默認大小。如果可能的話,也控制在圖像可以滾動,所以我不會在縮放丟失的區域
public class ZoomHelloActivity extends Activity {
// Physical display width and height.
private static int displayWidth = 0;
private static int displayHeight = 0;
static String img="";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Display display = ((WindowManager)
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
displayWidth = display.getWidth();
displayHeight = display.getHeight();
//get intent
Bundle extras = getIntent().getExtras();
if (extras != null) {
Toast.makeText(this.getBaseContext(),"Inside: "+img.toString(),
Toast.LENGTH_SHORT).show();
img = extras.getString("img");
// and get whatever type user account id is
}
else{ Toast.makeText(this.getBaseContext(),"ERROR ni",
Toast.LENGTH_SHORT).show();}
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.
int assignImg;
assignImg = Integer.parseInt(img);
bmLargeImage = BitmapFactory.decodeResource(getResources(),
assignImg);
}
@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) {
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;
}
}
}
編輯: 通緝輸出:
謝謝,我試了一下代碼,顯示的圖片很小。是否有可能限制圖像的大小,以便有足夠的區域進行滾動和縮放,而不會對圖像造成過度影響? – rahstame
如果我理解得很好 - 如果圖像比屏幕本身小,放大時它會始終伸展。你想要做的是把圖像放在屏幕的中心(不管大小)並放大。您可以在屏幕上設置一定的固定尺寸,並使用它將圖像放入該「框架」中,以防圖像超出尺寸,如果尺寸較小,則適合「框架」。請確認我的理解。 – g00dy
感謝您的回覆g00dy!^^。是的,第一個顯示屏上有一個默認圖像尺寸(居中),這也是最小縮小。當圖像達到其最大可滾動大小(實際圖像大小)時,它將回到默認大小。如果可能的話,還要控制圖像可以滾動的區域,這樣我就不會在放大時丟失。 – rahstame