gpt4 book ai didi

android - onTouchListener 在 ImageView 中无法正常工作

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

我正在开发一个 android 应用程序,在我的应用程序中,我动态创建了一些 ImageViews 并将该 ImageViews 设置为 RelativeLayout。所以我想独立地拖动布局中的每个图像。现在它工作但不正常,当我拖动图像然后图像开始振动并缓慢移动。我想在此图像中实现双指缩放。这可能吗?以及如何消除移动图像时的振动?如果有人知道,请帮助我..

谢谢

这是我的onTouchListener()

    canvasImage[i].setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {


x1 = motionEvent.getX();
y1 = motionEvent.getY();

//Log.v("vvvvvv", "" + x1 + " " + y1);
final int action = motionEvent.getAction();

switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN:

String w = imglayout.getTop() + " " + imglayout.getHeight();
Log.v("wwww", "" + w);

x2 = motionEvent.getX();
y2 = motionEvent.getY();

dx = x2 - canvasImage[finalI].getX();
dy = y2 - canvasImage[finalI].getY();

//Log.v("cccccc", "" + x2 + " " + y2);
moving = true;
break;
case MotionEvent.ACTION_UP:
moving = false;
break;
case MotionEvent.ACTION_MOVE:


if (moving) {

canvasImage[finalI].setX(motionEvent.getX() - dx);
canvasImage[finalI].setY(motionEvent.getY() - dy);

//Log.v("qqqqqq", "" + canvasImage[finalI].getX());
}
break;
}
//mainLayout.invalidate();
return true;
}

});

这是我的setCanvasImage()方法,用于将图像设置到RelativeLayout

public void setCanvasImage() {


int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;


final int imgCount = dbobj.getFromTemp();
canvasImage = new ImageView[imgCount];
imglayout = (RelativeLayout) findViewById(R.id.canvas);

final String[] strImage = dbobj.getdImage();

imglayout.removeAllViews();


for (int i = 0; i < imgCount; i++) {

canvasImage[i] = new ImageView(this);
canvasImage[i].setTag("img_" + i);
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);

if (tabletSize) {

int imgWidth = 130;
int imgHeight = 130;

RelativeLayout.LayoutParams paramss = new RelativeLayout.LayoutParams(imgWidth, imgHeight);
paramss.setMargins(posLeft, posTop, 0, 0);
canvasImage[i].setLayoutParams(paramss);

canvasImage[i].setScaleType(ScaleType.FIT_XY);
canvasImage[i].setTag(strImage[i]);
setImage(strImage[i], canvasImage[i]);
imglayout.addView(canvasImage[i]);


}
if( screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL){


int imgWidth = 100;
int imgHeight = 100;
RelativeLayout.LayoutParams paramss = new RelativeLayout.LayoutParams(imgWidth, imgHeight);
paramss.setMargins(posLeft, posTop, 0, 0);
canvasImage[i].setLayoutParams(paramss);
canvasImage[i].setTag(strImage[i]);
setImage(strImage[i], canvasImage[i]);
imglayout.addView(canvasImage[i]);
}

posLeft = posLeft + 15;
}
}

RelativeLayout xml 文件

<RelativeLayout
android:layout_below="@+id/btnhorscrolvw"
android:layout_width="match_parent"
android:layout_height="500dp"
android:id="@+id/canvas"
android:background="#ffff"
android:orientation="horizontal"
>
</RelativeLayout>

最佳答案

我认为这会奏效。

float dX, dY;

@Override
public boolean onTouch(View view, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
break;

case MotionEvent.ACTION_MOVE:

view.animate()
.x(event.getRawX() + dX)
.y(event.getRawY() + dY)
.setDuration(0)
.start();
break;
default:
return false;
}
return true;
}

关于android - onTouchListener 在 ImageView 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42409215/

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