如何在android中设置手指触摸功能?

发布于 2024-11-15 22:43:40 字数 1873 浏览 3 评论 0原文

我正在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;
}
}

I am developing a small application in android in which i have few image in my application i want when user touch with one finger images can move left or right side and when user can touch with two fingers it could be zoom how can i do this please refer some tutorial code for me.
here is my code
and i used view flipper in 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;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情癖 2024-11-22 22:43:40

查看 这个分步教程,它包含如何放大/缩小图片并移动它的代码示例。

Check out this step-by-step tutorial, it contains code examples how to zoom in/out a picture and move it around.

情丝乱 2024-11-22 22:43:40

在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 位置并确定应该向下的内容...
将看看是否有任何好的教程/示例可以更好地解释这些......

Handle the MotionEvent in the onTouchEvent(MotionEvent) of the Activity.
In this check for the 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;
}

A sequence of events would be issued, mostly with the actions as follows:

  • ACTION_DOWN - one finger touch down
  • ACTION_MOVE -> the finger is moved
  • ACTION_UP - one finger touch is removed
  • ACTION_POINTER_DOWN - second finger touch down
  • ACTION_POINTER_UP - second finger touch up

You will have to check the X,Y positions in the event and determine what should be donw...
will see if there are any good tutorials/samples to explain these better...

花心好男孩 2024-11-22 22:43:40

motionEventgetPointerCount() 告诉您有多少个手指被触摸。提供触摸次数

谢谢

getPointerCount() of motionEvent tells you how many fingers are touch. In gives number of touch

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文