我正在製作應用程序,其中我想在屏幕上拖動圖像。現在所有的東西都可以正常工作,但是如果用戶從左向右滑動,則圖像會進一步向右移動。我不想這樣做。如何在Android中將圖像拖出一半界限
我想要的是圖像不應該出去一半的邊界寬度。我的意思是圖像視圖應該只在視圖/邊界的一邊。我希望它在每個座標的邊界都有一半,我的意思是左,右,上和下。它不應該消失。爲此,我正在使用觸摸監聽器。現在,我能夠做旋轉,縮放和拖動(儘管它走出去的束縛)
所以現在我想它不應該走出去的束縛,這裏是我使用的代碼:
@Override
public boolean onTouch(View v, MotionEvent event) {
// ImageView view = (ImageView) v;
//view.setScaleType(ImageView.ScaleType.MATRIX);
float scale;
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
ImageView imageView = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
lastEvent = null;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDistance = spacing(event);
if (oldDistance > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
lastEvent = new float[4];
lastEvent[0] = event.getX(0);
lastEvent[1] = event.getX(1);
lastEvent[2] = event.getY(0);
lastEvent[3] = event.getY(1);
d = rotation(event);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
lastEvent = null;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
float newX = event.getX() - start.x;
float newY = event.getY() - start.y;
if (newX <= 0-(imageView.getDrawable()
.getIntrinsicWidth())){
Log.v("Bounds", "0-drawable width");
break;
}else if (newX >= screenWidth-(imageView.getDrawable()
.getIntrinsicWidth())){
Log.v("Bounds", "screen wdith-drawable width");
break;
}else if(newY <= 0-(imageView.getDrawable()
.getIntrinsicHeight())){
Log.v("Bounds", "Y 0-drawable width");
break;
}else if(newY >= (screenHeight-imageView.getDrawable()
.getIntrinsicHeight())) {
Log.v("Bounds", "y Screen-drawable width");
break;
}
matrix.set(savedMatrix);
matrix.postTranslate(newX, newY);
} else if (mode == ZOOM && event.getPointerCount() == 2) {
float newDistance = spacing(event);
if (newDistance > 10f) {
matrix.set(savedMatrix);
scale = newDistance/oldDistance;
float[] values = new float[9];
matrix.getValues(values);
float currentScale = values[Matrix.MSCALE_X];
if (scale * currentScale > MAX_ZOOM)
scale = MAX_ZOOM/currentScale;
else if (scale * currentScale < MIN_ZOOM)
scale = MIN_ZOOM/currentScale;
matrix.postScale(scale, scale, mid.x, mid.y);
}
if (lastEvent != null) {
newRot = rotation(event);
float r = newRot - d;
/* changes must be
float[] values = new float[9];
matrix.getValues(values);
float tx = values[2];
float ty = values[5];
float sx = values[0];
float xc = (view.getWidth()/2) * sx;
float yc = (view.getHeight()/2) * sx;
matrix.postRotate(r, tx + xc, ty + yc);
*/
float[] values = new float[9];
matrix.getValues(values);
float tx = values[2];
float ty = values[5];
float sx = values[0];
float xc = (imageView.getWidth()/2) * sx;
float yc = (imageView.getHeight()/2) * sx;
matrix.postRotate(r, tx + xc, ty + yc);
//I commented
matrix.postRotate(r, imageView.getMeasuredWidth()/2,
imageView.getMeasuredHeight()/2);
}
}
break;
}
imageView.setImageMatrix(matrix);
return true;
}
Eidt
,這是我的XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/rl_capture">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/fl_camera">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="content_desc_overlay"
android:src="@drawable/ic_launcher"
android:id="@+id/img_view"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_overlay"
android:src="@drawable/groom01"
android:scaleType="matrix"
/>
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/btn_screenshot"
android:layout_gravity="right|top"
/>
</RelativeLayout>
別讓我掛着 – stewjacks
你在說什麼? –
➡️➡️➡️➡️➡️➡️➡️✋ – stewjacks