Android Story

[ Android ] 사이즈가 큰 이미지 뷰

WhiteDuck 2016. 11. 25. 10:04


사이즈가 큰 이미지 뷰 처리하기



  • 사이즈가 큰 이미지는 한 눈에 보기 힘들다. 그래서 TouchListener를 사용하여 좌우로 볼 수 있는 리스너를 구현하고자 하였다.



ImageView imageView; // 이미지뷰 ( 이미 리소스는 가지고 온 상태 )

Bitmap bitmap; // 이미지뷰에서 볼 비트맵 ( 이미 비트맵은 가지고 온 상태 )



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;
}
});
}
}




  • 이미지 레이아웃

<?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

반응형