gpt4 book ai didi

java - 合并两个 onTouch 事件

转载 作者:行者123 更新时间:2023-11-30 10:12:33 25 4
gpt4 key购买 nike

对于我的应用程序,我需要组合两个 onTouch 事件。第一个我想获得 imageView 的点击像素的颜色,另一个事件应该使图片可缩放。我单独尝试过它,它似乎可以独立工作,但是当我尝试将这两个东西结合起来时,它就不再起作用了。他们为什么不一起工作?

我的代码:

public class MainActivity extends AppCompatActivity {

private ImageView imageView;
private Bitmap bitmap;
private ScaleGestureDetector mScaleGestureDetector;
private float mScaleFactor = 1.0f;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache(true);

mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());

imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

mScaleGestureDetector.onTouchEvent(event);

bitmap = imageView.getDrawingCache();
int pixel = bitmap.getPixel((int) event.getX(), (int) event.getY());
String text = "x = " + event.getX() + ", y = " + event.getY();
Log.d("Position", text);
int redValue = Color.red(pixel);
int greenValue = Color.green(pixel);
int blueValue = Color.blue(pixel);

return true;
}
});
}



private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

@Override
public boolean onScale(ScaleGestureDetector scaleGestureDetector){
mScaleFactor *= scaleGestureDetector.getScaleFactor();
mScaleFactor = Math.max(1.0f,
Math.min(mScaleFactor, 1.7f));
imageView.setScaleX(mScaleFactor);
imageView.setScaleY(mScaleFactor);

return true;

}
}

最佳答案

 imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

mScaleGestureDetector.onTouchEvent(event);

bitmap = imageView.getDrawingCache();
int pixel = bitmap.getPixel((int) event.getX(), (int) event.getY());
String text = "x = " + event.getX() + ", y = " + event.getY();
Log.d("Position", text);
int redValue = Color.red(pixel);
int greenValue = Color.green(pixel);
int blueValue = Color.blue(pixel);

return false;
}
});

我猜是因为你在这里消耗了触摸。尝试返回 false。

关于java - 合并两个 onTouch 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51848959/

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