我是新來的android編程...我試圖移動一個新添加到佈局使用手勢(特別是ontouchlistener)的imageview ..它設法添加imageview,但ontouchlistener只能望塵莫及,直到ACTION_DOWN ..這裏是我的代碼:不能使用ontouchlistener拖動自定義imageview
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將顯示相應的文字。但它僅顯示「創建」,沒有「動」或「向上」 ...圖像不會移動...如何解決這個問題?
謝謝,它終於工作!!在我以前的嘗試中,我已經給出了返回true,但它只是沒有作品......再次感謝!! :) –