사이즈가 큰 이미지 뷰 처리하기
- 사이즈가 큰 이미지는 한 눈에 보기 힘들다. 그래서 TouchListener를 사용하여 좌우로 볼 수 있는 리스너를 구현하고자 하였다.
ImageView imageView; // 이미지뷰 ( 이미 리소스는 가지고 온 상태 )
Bitmap bitmap; // 이미지뷰에서 볼 비트맵 ( 이미 비트맵은 가지고 온 상태 )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.util.DisplayMetrics; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.WindowManager; | |
import android.widget.ImageView; | |
/** | |
* Created by user on 2016-11-25. | |
*/ | |
public class ImageViewController { | |
public static void setBitmapTouchListener(Context context, final ImageView imageView, Bitmap bitmap) { | |
final int bitmapWidth = bitmap.getWidth(); | |
final int bitmapHeight = bitmap.getHeight(); | |
DisplayMetrics metrics = new DisplayMetrics(); | |
WindowManager mgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); | |
mgr.getDefaultDisplay().getMetrics(metrics); | |
// set maximum scroll amount (based on center of image) | |
int maxX = (int)((bitmapWidth / 2) - (metrics.widthPixels / metrics.densityDpi / 2)); | |
int maxY = (int)((bitmapHeight / 2) - (metrics.heightPixels / metrics.densityDpi / 2)); | |
// set scroll limits | |
final int maxLeft = (maxX * -1); | |
final int maxRight = maxX; | |
final int maxTop = (maxY * -1); | |
final int maxBottom = maxY; | |
// set touchlistener | |
imageView.setOnTouchListener(new View.OnTouchListener() | |
{ | |
float downX, downY; | |
int totalX, totalY; | |
int scrollByX, scrollByY; | |
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; | |
} | |
} | |
imageView.scrollBy(scrollByX, scrollByY); | |
downX = currentX; | |
downY = currentY; | |
break; | |
} | |
return true; | |
} | |
}); | |
} | |
} |
- 이미지 레이아웃
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:layout_behavior="@string/appbar_scrolling_view_behavior" | |
tools:context="com.gluesys.tilemaptest.MainActivity" | |
android:id="@+id/activity_main_content" | |
tools:showIn="@layout/activity_main"> | |
<ImageView | |
android:id="@+id/scroll_mapview" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:scaleType="center" | |
/> | |
</RelativeLayout> |
중요한 건 ImageView의 scaleType이 center이여야 한다는 거
- 사용하기
ImageViewController.setBitmapTouchListener(getApplicationContext(), imageView, bitmap);
출처 : http://stackoverflow.com/questions/3058164/android-scrolling-an-imageview
반응형
'Android Story' 카테고리의 다른 글
[ Android ] FFMpeg 패키지 연동하기 (15) | 2016.12.13 |
---|---|
[ Android ] inflate (0) | 2016.12.07 |
[ Android ] 다음 맵에서 지원하지 않는 점선 라인.. 해결?? (0) | 2016.11.26 |
[ Android ] 마커 이미지에 인덱스 합성 (0) | 2016.11.26 |
[ Android ] adb shell screenrecord (0) | 2016.11.24 |
[ Android ] GPS 구하기 (8) | 2016.11.23 |
[ Android ] Theme.AppCompat 사용시, statusbar가 UI와 겹칠때 (0) | 2016.11.17 |
[ Android ] 프로그래스 바 반시계 방향으로 돌기 ( ProgressBar counterclockwise ) (0) | 2016.11.04 |