我已经尝试了几天让它工作,查看了其他答案,但似乎都对我无效:S
我向RelativeLayout添加了一个贴纸,希望在选中后使用OnTouch方法移动、缩放和旋转该贴纸(它具有调用setOnTouchListener的OnClickListener),请注意我可以添加N个贴纸。
这个应用程序已经编译完成并且没有崩溃,但是贴纸在触摸时没有任何反应 :(
我尝试过的链接:
我向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);
这个应用程序已经编译完成并且没有崩溃,但是贴纸在触摸时没有任何反应 :(
我尝试过的链接:
http://code.almeros.com/android-multitouch-gesture-detectors#.V1Y0sZMrLMW
还有其他一些链接,我现在找不到了
有人能帮忙吗?
编辑: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;
}
});
}
}