gpt4 book ai didi

android - 移动、缩放和旋转 ImageView OnTouch 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:50 24 4
gpt4 key购买 nike

我花了几天时间尝试完成这项工作,检查了一些其他答案,但似乎没有一个对我有用:S

我将标签添加到 RelativeLayout,我希望标签在被选中后使用 OnTouch 方法移动、缩放和旋转(它有一个调用 setOnTouchListener 的 OnClickListener)请记住,我可以添加 N 个贴纸。

private void addSticker(ImageView sticker)
{

flMemeFrame.addView(sticker);

sticker.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
{
if (selectedView != null)
{
CancelSelection(selectedView);
}
selectedView = v;

v.setOnTouchListener(new View.OnTouchListener()
{
private float mScaleFactor = 0.5f;
private float mRotationDegree = 0.f;
private float mFocusX = 0.f;
private float mFocusY = 0.f;
private int mScreenHeight;
private int mScreenWidth;
private Matrix matrix = new Matrix();//Các lớp Matrix giữ một ma trận 3x3 để di chuyển tọa độ.
private int mImageWidth, mImageHeight;
private ScaleGestureDetector mScaleDetector;
private RotateGestureDetector mRotateDetector;
private MoveGestureDetector mMoveDetector;

class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 1.0f));
return true;
}
}

class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
@Override
public boolean onRotate(RotateGestureDetector detector) {
mRotationDegree -= detector.getRotationDegreesDelta();
return true;
}
}

class MoveListener extends MoveGestureDetector.SimpleOnMoveGestureListener {
@Override
public boolean onMove(MoveGestureDetector detector) {
PointF d = detector.getFocusDelta();
mFocusX += d.x;
mFocusY += d.y;

return true;
}
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
mImageHeight = v.getHeight();
mImageWidth = v.getWidth();

mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
mRotateDetector = new RotateGestureDetector(context, new RotateListener());
mMoveDetector = new MoveGestureDetector(context, new MoveListener());

mScaleDetector.onTouchEvent(event);
mRotateDetector.onTouchEvent(event);
mMoveDetector.onTouchEvent(event);
float scaleImageCenterX = (mImageWidth * mScaleFactor) / 2;
float scaleImageCenterY = (mImageHeight * mScaleFactor) / 2;

matrix.reset();
matrix.postScale(mScaleFactor, mScaleFactor);
matrix.postRotate(mRotationDegree, scaleImageCenterX, scaleImageCenterY);
matrix.postTranslate(mFocusX - scaleImageCenterX, mFocusY - scaleImageCenterY);

ImageView view = (ImageView) v;
view.setScaleType(ImageView.ScaleType.MATRIX);
view.setImageMatrix(matrix);

return true;
}
});
}
}
});
}

这里是我设置图片的地方

ImageView sticker = new ImageView(context);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
sticker.setLayoutParams(rlp);
addSticker(sticker);

应用程序编译并且没有崩溃,但贴纸没有任何 OnTouch :(

我试过的链接:

http://code.almeros.com/android-multitouch-gesture-detectors#.V1Y0sZMrLMW

rotation and scaling and move using multi touch in android

还有其他一些我现在找不到的链接

谁能帮忙?

编辑:CancelSelection 的代码(完全不相关,因为我唯一要做的就是将 selectedView 设置为 null 并删除背景,我还删除了 OnTouchListener,因为我不想移动/缩放/旋转未选中则查看)

@SuppressWarnings("deprecation")
public void CancelSelection(View v)
{
if(isCaptionEditPanelOpen)
{
rlQuickEdit.setVisibility(View.GONE);
rlQuickEdit = null;
isCaptionEditPanelOpen = false;
}
//toogleButtonVisibility(false);
v.setBackgroundColor(getResources().getColor(android.R.color.transparent));
selectedView = null;
v.setOnTouchListener(null);
}

我写了这个类,看看我是否可以在没有我的应用程序的所有复杂性的情况下让它工作,结果相同:

package com.andujardev.imagerotation;

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity
{
Context context;
ImageView imgRotate;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;

imgRotate = (ImageView) findViewById(R.id.imgRotate);

imgRotate.setOnTouchListener(new View.OnTouchListener()
{
private float mScaleFactor = 0.5f;
private float mRotationDegree = 0.f;
private float mFocusX = 0.f;
private float mFocusY = 0.f;
private int mScreenHeight;
private int mScreenWidth;
private Matrix matrix = new Matrix();
private int mImageWidth, mImageHeight;
private ScaleGestureDetector mScaleDetector;
private RotateGestureDetector mRotateDetector;
private MoveGestureDetector mMoveDetector;

class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 1.0f));
return true;
}
}

class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
@Override
public boolean onRotate(RotateGestureDetector detector) {
mRotationDegree -= detector.getRotationDegreesDelta();
return true;
}
}

class MoveListener extends MoveGestureDetector.SimpleOnMoveGestureListener {
@Override
public boolean onMove(MoveGestureDetector detector) {
PointF d = detector.getFocusDelta();
mFocusX += d.x;
mFocusY += d.y;

return true;
}
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
mImageHeight = v.getHeight();
mImageWidth = v.getWidth();

mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
mRotateDetector = new RotateGestureDetector(context, new RotateListener());
mMoveDetector = new MoveGestureDetector(context, new MoveListener());

mScaleDetector.onTouchEvent(event);
mRotateDetector.onTouchEvent(event);
mMoveDetector.onTouchEvent(event);
float scaleImageCenterX = (mImageWidth * mScaleFactor) / 2;
float scaleImageCenterY = (mImageHeight * mScaleFactor) / 2;

matrix.reset();
matrix.postScale(mScaleFactor, mScaleFactor);
matrix.postRotate(mRotationDegree, scaleImageCenterX, scaleImageCenterY);
matrix.postTranslate(mFocusX - scaleImageCenterX, mFocusY - scaleImageCenterY);

ImageView view = (ImageView) v;
view.setImageMatrix(matrix);

return true;
}
});
}
}

最佳答案

有趣的是,对于每个 onTouch 事件,您实例化一个新的ScaleGestureDetectorRotateGestureDetector MoveGestureDetector.

考虑到每个各自的手势检测器需要先前的触摸事件来确定和解释如何使用react,这无疑是您问题的重要组成部分,因为您为每个触摸事件创建了一个新的手势检测器。事实上,如果您查看 Almeros 探测器的 onTouchEvent(MotionEvent ev) 方法的源代码,它指出:

Applications should pass a complete and consistent event stream to this method.
* A complete and consistent event stream involves all MotionEvents from the initial
* ACTION_DOWN to the final ACTION_UP or ACTION_CANCEL.

对于初学者,您应该确保只创建一个单个 检测器对象,以便它可以处理所有触摸事件。

关于android - 移动、缩放和旋转 ImageView OnTouch 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37669742/

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