我正在开发一个想要全屏显示图像的 Android 应用程序。然后我想在这个图像上显示一个小“窗口”:
- 窗口放置在用户触摸屏幕的位置。
- 此窗口的内容是放大的原始图像的一部分。放大的图像部分也由用户触摸屏幕的位置定义。
这样,当用户在屏幕上移动手指时,“窗口”会跟随手指并显示原始图像的缩放版本。
直到现在,我能够做任何我想做的事情,除了用缩放的图像更新“窗口”的内容。
我写了以下代码:
public class DetectEyesActivity extends Activity implements OnTouchListener {
//DetectEyesView detectEyesView;
//LinearLayout mLinearLayout;
TextView text;
ImageView imgView;
Bitmap imgBitmap;
ImageView touchView2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.detecteyeslayout);
Intent intent = this.getIntent();
byte[] image = intent.getByteArrayExtra("Image");
text = (TextView) findViewById(R.id.detectEyesTextView1);
imgView = (ImageView) findViewById(R.id.detectEyesImageView1);
imgBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
imgView.setImageBitmap(imgBitmap);
//imgView.setOnTouchListener(this);
text.setTextSize(30);
text.setText("A");
touchView2 = (ImageView) findViewById(R.id.detectEyesImageView2);
Bitmap temp = Bitmap.createBitmap(imgBitmap, 200, 200, 200, 200);
touchView2.setImageBitmap(temp);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//event.getX()
int y = (int)event.getY();
int x = (int)event.getX();
float aux = (float)imgBitmap.getHeight() / (float)imgView.getHeight();
//float imgBitmapHeight =
//float imgBitmapWidth = (float)imgBitmap.getWidth()/aux
float imgDrawableX = (imgView.getWidth() - (float)imgBitmap.getWidth()/aux ) / 2 + imgView.getX();
float imgDrawableY = (imgView.getHeight() - (float)imgBitmap.getHeight()/aux ) / 2 + imgView.getY();
text.setText("TouchX = " + x + " TouchY = " + y + " ImageX: "+ imgDrawableX + " ImageY: "+ imgDrawableY + " aux = " + aux);
Bitmap temp = Bitmap.createBitmap(imgBitmap, x, y, 50, 50);
//touchView2.setImageBitmap(temp);
touchView2.setVisibility(0);
touchView2.bringToFront();
touchView2.layout(x-200, y-200, x-30, y-30);
touchView2.postInvalidate();
touchView2.invalidate();
Log.v("x,y",event.getY()+","+event.getX());
return true;
}
使用此代码,窗口跟随手指而不更新其内容。当我取消注释该行时
//touchView2.setImageBitmap(temp);
该窗口根本不会出现。尽管如此,textView 的内容仍在不断更新。所以我知道 onTouchEvent 正在执行。
有谁知道为什么会这样?
先感谢您。