public class MainActivity extends Activity {
ViewTouchImage imgView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imgView = new ViewTouchImage(getApplicationContext());
imgView.invalidate();
setContentView(imgView);
imgView.setImageResource(R.drawable.checkbox);
}
}
class ViewTouchImage extends ImageView implements OnTouchListener {
private static final String TAG = "ViewTouchImage";
private static final boolean D = false;
private Matrix matrix = new Matrix();
private Matrix savedMatrix = new Matrix();
private Matrix savedMatrix2 = new Matrix();
private static final int NONE = 0;
private static final int DRAG = 1;
private int mode = NONE;
private PointF start = new PointF();
private PointF mid = new PointF();
private static final int WIDTH = 0;
private static final int HEIGHT = 1;
private boolean isInit = false;
public ViewTouchImage(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOnTouchListener(this);
setScaleType(ScaleType.MATRIX);
}
public ViewTouchImage(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ViewTouchImage(Context context) {
this(context, null);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
if (D)
Log.i(TAG, "onLayout");
super.onLayout(changed, left, top, right, bottom);
if (isInit == false) {
init();
isInit = true;
}
}
@Override
public void setImageBitmap(Bitmap bm) {
if (D)
Log.i(TAG, "setImageBitmap");
super.setImageBitmap(bm);
isInit = false;
init();
}
@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
isInit = false;
init();
}
@Override
public void setImageResource(int resId) {
super.setImageResource(resId);
isInit = false;
init();
}
protected void init() {
matrixTurning(matrix, this);
setImageMatrix(matrix);
setImagePit();
}
public void setImagePit() {
float[] value = new float[9];
this.matrix.getValues(value);
int width = this.getWidth();
int height = this.getHeight();
Drawable d = this.getDrawable();
if (d == null)
return;
int imageWidth = d.getIntrinsicWidth();
int imageHeight = d.getIntrinsicHeight();
int scaleWidth = (int) (imageWidth * value[0]);
int scaleHeight = (int) (imageHeight * value[4]);
value[2] = 0;
value[5] = 0;
if (imageWidth > width || imageHeight > height) {
int target = WIDTH;
if (imageWidth < imageHeight)
target = HEIGHT;
if (target == WIDTH)
value[0] = value[4] = (float) width / imageWidth;
if (target == HEIGHT)
value[0] = value[4] = (float) height / imageHeight;
scaleWidth = (int) (imageWidth * value[0]);
scaleHeight = (int) (imageHeight * value[4]);
if (scaleWidth > width)
value[0] = value[4] = (float) width / imageWidth;
if (scaleHeight > height)
value[0] = value[4] = (float) height / imageHeight;
}
matrix.setValues(value);
setImageMatrix(matrix);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY()
- start.y);
}
break;
}
// 매트릭스 값 튜닝.
matrixTurning(matrix, view);
view.setImageMatrix(matrix);
return true;
}
private void matrixTurning(Matrix matrix, ImageView view) {
float[] value = new float[9];
matrix.getValues(value);
float[] savedValue = new float[9];
savedMatrix2.getValues(savedValue);
int width = view.getWidth();
int height = view.getHeight();
Drawable d = view.getDrawable();
if (d == null)
return;
int imageWidth = d.getIntrinsicWidth();
int imageHeight = d.getIntrinsicHeight();
int scaleWidth = (int) (imageWidth * value[0]);
int scaleHeight = (int) (imageHeight * value[4]);
if (value[2] < width - scaleWidth)
value[2] = width - scaleWidth;
if (value[5] < height - scaleHeight)
value[5] = height - scaleHeight;
if (value[2] > 0)
value[2] = 0;
if (value[5] > 0)
value[5] = 0;
matrix.setValues(value);
savedMatrix2.set(matrix);
}
}
위의 코드는 이미지뷰를 드래그해서 이동하는 코드인데요
실행해보니 드래그해서 이동이 되지 않고있습니다.
제대로 작동해야되는것 같은데 이유를 모르겠네요;;
아시는분 도움 부탁드립니다!