gpt4 book ai didi

android - 拖动,缩放没有矩阵android的imageview

转载 作者:行者123 更新时间:2023-11-29 01:42:11 30 4
gpt4 key购买 nike

我需要一个解决方案来实现拖动和缩放,在不使用矩阵的情况下围绕屏幕 onTouch 事件路由 ImageView。我搜索了很多但所有答案都是用矩阵完成的,因为我在屏幕矩阵中有多个对象不适合我,我想拖动和缩放 ImageView 对象而不是 ImageView 中的矩阵,经过一些尝试我实现了一个可以拖动 View 的类但放大它会打扰,如果有人能帮助我,谢谢,如果我的语言能力不好,如果我有拼写错误,我深表歉意。

package com.example.painting1;

import android.graphics.PointF;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class Drag implements OnTouchListener {

ImageView img;

// We can be in one of these 3 states
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;

private int mode = NONE;

private PointF mid = new PointF();

PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
PointF StartPT = new PointF(); // Record Start Position of 'img'

float oldDist = 1f;

public Drag( ImageView img ) {
// TODO Auto-generated constructor stub
this.img = img;
img.setOnTouchListener(this);
}

private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}

/**
* Calculate the mid point of the first two fingers
*/
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}

@Override
public boolean onTouch(View v, MotionEvent event)
{
int evid = event.getAction();
int moid = MotionEvent.ACTION_MASK;

switch (evid & moid)
{
case MotionEvent.ACTION_MOVE :

if (mode == DRAG) {
PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
img.setX((int)(StartPT.x+mv.x));
img.setY((int)(StartPT.y+mv.y));
StartPT = new PointF( img.getX(), img.getY() );
}

else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
//matrix.set(savedMatrix);
float scale = (newDist / oldDist);
//matrix.postScale(scale, scale, mid.x, mid.y);

img.layout((int)scale, (int)scale, (int)mid.x, (int)mid.y);

img.setScaleX(scale);
img.setScaleY(scale);
}
}

break;

case MotionEvent.ACTION_DOWN :

mode = DRAG;

DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF( img.getX(), img.getY() );
break;
case MotionEvent.ACTION_UP :
// Nothing have to do
case MotionEvent.ACTION_POINTER_UP :
mode = NONE;
break;

case MotionEvent.ACTION_POINTER_DOWN :

oldDist = spacing(event);
if (oldDist > 10f) {
//savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
default :
break;
}
return true;
}
}

最佳答案

**这里是缩放、拖动、可触摸ImageView的完整源代码..

它在对图像执行拖动和缩放时工作流畅。**

package com.admin.imagemovementzoom;

import android.annotation.SuppressLint;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.squareup.picasso.Picasso;


public class FullmageActivity extends AppCompatActivity implements View.OnTouchListener {

private ImageView img_fullimage;
private String picturePath;


private static final String TAG = "Touch";
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullmage);

picturePath = getIntent().getStringExtra("image");

FindId();

Log.d("adaa", "file://" + picturePath);

Picasso.get().load("file://" + picturePath).into(img_fullimage);

img_fullimage.setOnTouchListener(this);
img_fullimage.setScaleType(ImageView.ScaleType.MATRIX);

}


private void FindId() {
img_fullimage = findViewById(R.id.img_full);
}


/**
* Called when a touch event is dispatched to a view. This allows listeners to
* get a chance to respond before the target view.
*
* @param v The view the touch event has been dispatched to.
* @param event The MotionEvent object containing full information about
* the event.
* @return True if the listener has consumed the event, false otherwise.
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;

// Dump touch event to log
dumpEvent(event);

// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x,
event.getY() - start.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}

view.setImageMatrix(matrix);
return true; // indicate event was handled
}

/**
* Show an event in the LogCat view, for debugging
*/
private void dumpEvent(MotionEvent event) {
String names[] = {"DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
"POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?"};
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid ").append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";");
}
sb.append("]");
Log.d(TAG, sb.toString());
}

/**
* Determine the space between the first two fingers
*/
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}

/**
* Calculate the mid point of the first two fingers
*/
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}

关于android - 拖动,缩放没有矩阵android的imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23841420/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com