gpt4 book ai didi

android - 在 android 中使用 ScaleGestureDetector 时需要帮助

转载 作者:行者123 更新时间:2023-11-29 22:10:03 25 4
gpt4 key购买 nike

我使用以下代码来检测 View 的 ScaleGesture。 ScaleGestureDetector 工作正常。我在布局中显示了两张图片。在检测 ScaleGesture 时,我缩小和增加了图像的大小。我还需要点击或触摸图像来做一些处理。如果我使用 setOnClickListener 而不是 setOnTouchListener,则 ScaleGestureDetector 不起作用。用两个手指触摸屏幕时,setOnTouchListener 在 ScaleGestureDetector 之前触发。如何同时实现 ontouch 和 scaleGesture。

package com.pinch.detect;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class PinchDetectorActivity extends Activity {
TextView textGestureAction;
ImageView img1,img2;
static Bitmap bm, bm1;
String url1="url1111";
String url2="url2222";

private ScaleGestureDetector scaleGestureDetector;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textGestureAction = (TextView)findViewById(R.id.GestureAction);
img1=(ImageView)findViewById(R.id.img_left);
img2=(ImageView)findViewById(R.id.img_right);

try {
URL aURL = new URL(url1);
// parseBitmap(aURL);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();

/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);

/* Decode url-data to a bitmap. */
bm = BitmapFactory.decodeStream(bis);

bis.close();
is.close();

/* Apply the Bitmap to the ImageView that will be returned. */

img1.setImageBitmap(bm);

URL aURL1 = new URL(url2);
URLConnection conn1 = aURL1.openConnection();
conn1.connect();
InputStream is1 = conn1.getInputStream();

BufferedInputStream bis1 = new BufferedInputStream(is1);

bm1 = BitmapFactory.decodeStream(bis1);

bis1.close();
is1.close();

img2.setImageBitmap(bm1);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
scaleGestureDetector = new ScaleGestureDetector(this,
new MySimpleOnScaleGestureListener());

img1.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "clicked image 1", Toast.LENGTH_SHORT).show();
return false;
}
});

img2.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "clicked image 2", Toast.LENGTH_SHORT).show();
return false;
}
});

/* img1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "clicked image 1", Toast.LENGTH_SHORT).show();
}
});
img2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "clicked image 2", Toast.LENGTH_SHORT).show();
}
});*/
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
scaleGestureDetector.onTouchEvent(event);
return true;
}

public class MySimpleOnScaleGestureListener extends
SimpleOnScaleGestureListener {

@Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub

float scaleFactor = detector.getScaleFactor();
if(scaleFactor > 1){
Log.v("inside scale factor if","if");
textGestureAction.setText("Scale Out: " + String.valueOf(scaleFactor));

Bitmap resizedbitmap = Bitmap.createScaledBitmap(bm, 200,
480, true);
img1.setImageBitmap(resizedbitmap);
Bitmap resizedbitmap1 = Bitmap.createScaledBitmap(bm1, 200,
480, true);
img2.setImageBitmap(resizedbitmap1);

}else{
Log.v("inside scale factor else","else");
textGestureAction.setText("Scale In: " + String.valueOf(scaleFactor));


Bitmap resizedbitmap = Bitmap.createScaledBitmap(bm, 400,
480, true);

img1.setImageBitmap(resizedbitmap);

Bitmap resizedbitmap1 = Bitmap.createScaledBitmap(bm1, 400,
480, true);

img2.setImageBitmap(resizedbitmap1);


}

return true;
}
}
}

最佳答案

onTouchEvent() 应该这样实现:

@Override
public boolean onTouchEvent(MotionEvent event) {
boolean handled = scaleGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event) || handled;
}

关于android - 在 android 中使用 ScaleGestureDetector 时需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9838111/

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