我是android编程新手...我正在尝试使用手势(特别是ontouchlistener)移动一个新创建的imageview,它添加到布局中......它设法添加了imageview,但ontouchlistener只捕获到ACTION_DOWN..这是我的代码:
package com.calengine;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
public class coba extends CALEngineActivity implements OnClickListener, OnTouchListener{
private ImageView movableimage;
private FrameLayout layout;
private LayoutParams params;
int status;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainform2);
layout = (AbsoluteLayout) findViewById(R.id.absLayoutAction);
status = 0;
movableimage = new ImageView(this);
movableimage.setImageResource(R.drawable.icon_launcher);
layout.addView(movableimage);
movableimage.setOnTouchListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public boolean onTouch(View v, MotionEvent event) {
TextView txt = (TextView) findViewById(R.id.textView1);
LayoutParams layoutParams = (LayoutParams) movableimage.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
txt.setText("Created");
break;
case MotionEvent.ACTION_MOVE:
txt.setText("Moving");
int x_cord = (int)event.getRawX();
int y_cord = (int)event.getRawY();
layoutParams.leftMargins = x_cord - 25;
layoutParams.topMargins = y_cord - 75;
movableimage.setLayoutParams(layoutParams);
break;
case MotionEvent.ACTION_UP:
txt.setText("Up");
break;
default:
break;
}
return false;
}
}
我假设对于这些事件,textview 将显示相应的文本。但它只显示“已创建”,没有“移动”或“向上”……图像也不会移动……如何解决这个问题?