activity_main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ViewFlipper
android:layout_margin="6dip"
android:id="@+id/view_flipper"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/photo1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/photo2"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/photo3"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/photo4"/>
</ViewFlipper>
</LinearLayout>
MainActivity :
public class MainActivity extends Activity {
private ViewFlipper vf;
private float lastX;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vf = (ViewFlipper) findViewById(R.id.view_flipper);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
float currentX = touchevent.getX();
if (lastX < currentX)
{
if (vf.getDisplayedChild()==0)
break;
vf.setInAnimation(this, R.anim.in_from_left);
vf.setOutAnimation(this, R.anim.out_to_right);
vf.showNext();
}
if (lastX > currentX)
{
if (vf.getDisplayedChild()==1)
break;
vf.setInAnimation(this, R.anim.in_from_right);
vf.setOutAnimation(this, R.anim.out_to_left);
vf.showPrevious();
}
break;
}
case MotionEvent.ACTION_MOVE:
{
float tempX = touchevent.getX();
int scrollX = (int) (tempX - lastX);
//vf.scrollBy(scrollX, 0);
break;
}
}
return false;
}
}
图像顺序如下:photo1,photo4,photo3,photo2。如何制作:photo1,photo2,photo3,photo4?
谢谢
答案 0 :(得分:8)
图像的顺序没问题,但代码有问题。请参阅下文如何解决问题:
public class MainActivity extends Activity {
private ViewFlipper vf;
private float lastX;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vf = (ViewFlipper) findViewById(R.id.view_flipper);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
float currentX = touchevent.getX();
if (lastX < currentX)
{
if (vf.getDisplayedChild()==0)
break;
vf.setInAnimation(this, R.anim.in_from_left);
vf.setOutAnimation(this, R.anim.out_to_right);
// vf.showNext();
vf.showPrevious();
}
if (lastX > currentX)
{
// if (vf.getDisplayedChild()==1)
if (vf.getDisplayedChild()==vf.getChildCount()-1)
break;
vf.setInAnimation(this, R.anim.in_from_right);
vf.setOutAnimation(this, R.anim.out_to_left);
// vf.showPrevious();
vf.showNext();
}
break;
}
case MotionEvent.ACTION_MOVE:
{
float tempX = touchevent.getX();
int scrollX = (int) (tempX - lastX);
//vf.scrollBy(scrollX, 0);
break;
}
}
return false;
}
}