【问题标题】:How to set finger touch functionality in android?如何在android中设置手指触摸功能?
【发布时间】:2011-06-17 12:29:04
【问题描述】:

我正在 android 中开发一个小型应用程序,其中我的应用程序中我想要的图像很少这样做请为我参考一些教程代码。 这是我的代码 我在 xml v fdjf 中使用了视图翻转器

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;
}
}

【问题讨论】:

    标签: android swap zooming


    【解决方案1】:

    查看this 分步教程,其中包含如何放大/缩小图片并四处移动的代码示例。

    【讨论】:

      【解决方案2】:

      在Activity的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 - 第二根手指向上触摸

      您必须检查事件中的 X、Y 位置并确定应该向下的位置... 将看看是否有任何好的教程/示例可以更好地解释这些......

      【讨论】:

      • 我尝试实现此代码,但存在一个问题,如果图像被缩放并且我也拖动它但我无法移动到下一张图像
      • @Avi,是的,我在一些手机中也看到了这样的功能。缩放图像时,拖动(或轻弹)不会移动到下一个图像。只有在图像恢复到原始大小后,才能进行轻弹。 /n 你能告诉我们放大的图像是在哪里绘制的吗?在这种情况下,触摸事件在哪里以及如何处理?...当图像缩放时,拖动(或轻弹)可能会被忽略...
      • 将我的代码放在我的问题中看看它看看我的代码
      【解决方案3】:

      motionEvent 中的getPointerCount() 告诉您有多少手指被触摸。在给出触摸次数

      谢谢

      【讨论】:

        猜你喜欢
        • 2020-07-28
        • 1970-01-01
        • 2017-02-28
        • 1970-01-01
        • 2011-09-24
        • 2012-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多