2011-06-17 18 views
2

我正在开发一个小型应用程序,在android中我有几个图像在我的应用程序中,当用户用一根手指触摸图像时可以移动左侧或右侧,当用户可以用两根手指触摸它可以缩放我怎么办这请为我参考一些教程代码。 这里是我的代码 和我使用视图的鳍状肢的XML v fdjf如何在android中设置手指触摸功能?

public class Jaap extends Activity implements OnTouchListener{ 

float downXValue; 
int counter = 0; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

// Set main.XML as the layout for this Activity 
setContentView(R.layout.jaap); 

// Add these two lines 
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main); 
layMain.setOnTouchListener((OnTouchListener) this); 

// Add a few countries to the spinner 

} 

public boolean onTouch(View arg0, MotionEvent arg1) { 

// Get the action that was done on this touch event 
switch (arg1.getAction()) 
{ 
case MotionEvent.ACTION_DOWN: 
{ 
// store the X value when the user's finger was pressed down 
downXValue = arg1.getX(); 
break; 
} 

case MotionEvent.ACTION_UP: 
{ 
// Get the X value when the user released his/her finger 
float currentX = arg1.getX(); 

// going backwards: pushing stuff to the right 
if (downXValue < currentX) 
{ 
// Get a reference to the ViewFlipper 
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details); 
// Set the animation 
// vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)); 
// Flip! 
if(counter > 0){ 
vf.showPrevious(); 
counter--; 
} 
} 
// going forwards: pushing stuff to the left 
if (downXValue > currentX) 
{ 
// Get a reference to the ViewFlipper 
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details); 
// Set the animation 
// vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); 
// Flip! 
if(counter < 131){ 
vf.showNext(); 
counter++; 
} 
} 
break; 
} 
} 

// if you return false, these actions will not be recorded 
return true; 
} 
} 

回答

1

在活动的onTouchEvent(MotionEvent)中处理MotionEvent。 在此检查MotionEvent.getAction()。

switch(MotionEvent.GetAction()) { 
case ACTION_DOWN: 
//handle the finger down functionality here 
break; 

case ACTION_POINTER_DOWN: 
//handle the second finger down functionality here 
break; 
} 

的一系列事件将发行,大多是用行动如下:

  • ACTION_DOWN - 一个手指触摸下来
  • ACTION_MOVE - >手指移动
  • ACTION_UP - 一个手指触摸被删除
  • ACTION_POINTER_DOWN - 第二根手指触地
  • ACTION_POINTER_UP - 第二根手指r力度高达

您必须检查X,Y位置的事件,并确定应该是什么donw ... 将看看是否有任何好的教程/样本,以解释这些好...

+0

我试图执行这个代码,但是有一个问题,如果图像得到放大和我拖累了也,但我不能移动到下一个图像 – 2011-06-20 06:44:14

3

退房this一步一步的教程,它包含的代码示例如何放大/缩小图片和移动它。

1

getPointerCount() of motionEvent告诉你有多少手指触摸。在给出了触摸的数量

感谢

相关问题