2
나는이 코드를 사용하여 이미지를 스크롤하고 확대/축소 할 수 있지만 이미지가 작더라도 코드가 전체 화면을 채울 수 있도록 이미지를 늘립니다. 대화 상자에 이미지가 포함되어 있고 전체 화면을 차지하지 않고 코드를 수정하여 확대/축소 및 스크롤을 가능하게하고 싶습니다.이미지 오버 줌
편집 : 아래 코드를 사용하여 이미지를 스크롤하는 데 한계가 있거나 이미지의 크기를 제한 할 수 있습니까? 첫 번째 디스플레이 (가운데)에 기본 이미지 크기가 있으며 이는 최소 축소입니다. 이미지가 최대 화면 이동 크기 (실제 이미지 크기)에 도달하면 기본 크기로 돌아갑니다. 가능하다면, 또한 내가의 확대에 손실되지 않도록 이미지를 스크롤 할 수있는 영역을 제어 할 수
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