我正在android中开发一个小应用程序,其中我的应用程序中很少有图像,当用户用一个手指触摸图像时,图像可以向左或向右移动,当用户用两个手指触摸图像时,图像可以缩放。我怎么做?请参考一些教程代码。
这是我的密码
我在xmlvdfdjf中使用了视图翻转器

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

最佳答案

在活动的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;
}

将发布一系列事件,主要采取以下行动:
动作-一个手指触地
动作-移动->手指移动
动作-取消一个手指触摸
指针向下-第二指向下触碰
动作指针向上-第二指触摸
你将不得不检查X,Y在事件中的位置,并确定应该做什么…
会看看是否有好的教程/样本来更好地解释这些…

07-28 09:47