我有大约 20 张图片。我已经为每个图像和一个 ImageView 创建了按钮。单击按钮时,相应的图像将设置为 ImageView 并启用缩放和拖动。然后,在单击另一个按钮时将其替换以移至下一个/上一个图像。
现在,我必须删除按钮并通过实现快速垂直滑动来替换它。当我从下到上快速滑动时,它将移动到下一个图像。然后从上到下快速滑动移动到上一张图像。如果他们到达 first image 或 last image也禁用它。
我正在考虑 ImageView 列表来处理图像。然后,通过在 List 上维护当前 ImageView 的索引来实现快速滑动功能。
任何关于如何使用 Android 2.1实现快速滑动分页的正确方向的指导都非常感谢。
UPDATES1 我成功地使用 ImageSwitcher 使用按钮(下一个,上一个)上下滑动。我的下一个问题是如何通过快速滑动来移动 Next&Prev 来实现它。我将添加我当前的代码。
注意: TouchImageView 类支持缩放和拖动。当您缓慢滑动时,图像会拖动到该方向,但是当我快速向上或向下滑动时,我想分别移动到下一张和上一张图像。
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageSwitcher android:id="@+id/switcher1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_weight="0"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"/>
<Button android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"/>
</LinearLayout>
</LinearLayout>
活动:(注:TouchImageView 来自RecipeBook060 Zoom and Drag Extended ImageView,Slide Images 想法来自Slide Image)
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwticherActivity extends Activity
implements ViewFactory {
Integer[] imageIDs = { R.drawable.image_one, R.drawable.image_two,
R.drawable.image_three };
private ImageSwitcher imageSwitcher;
private Button nextButton;
private Button previousButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);
final Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
imageSwitcher.setImageResource(imageIDs[0]);
nextButton = (Button) findViewById(R.id.next);
nextButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Animation out= AnimationUtils.loadAnimation(ImageSwticherActivity.this, R.anim.slide_out_bottom);
Animation in= AnimationUtils.loadAnimation(ImageSwticherActivity.this, R.anim.slide_in_top);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
imageSwitcher.setImageResource(imageIDs[1]);
}
});
previousButton = (Button) findViewById(R.id.previous);
previousButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Animation out= AnimationUtils.loadAnimation(ImageSwticherActivity.this, R.anim.slide_out_top);
Animation in= AnimationUtils.loadAnimation(ImageSwticherActivity.this, R.anim.slide_in_bottom);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
imageSwitcher.setImageResource(imageIDs[0]);
}
});
}
@Override
public View makeView() {
//ImageView imageView = new ImageView(ImageSwticherActivity.this);
TouchImageView imageView = new TouchImageView(ImageSwticherActivity.this);
imageView.setBackgroundColor(0xFF000000);
//imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
ImageSwitcher.LayoutParams.FILL_PARENT, ImageSwitcher.LayoutParams.FILL_PARENT));
return imageView;
}
}
slide_in_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="-50%p" android:toYDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_in_top.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="50%p" android:toYDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_out_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0" android:toYDelta="-50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_out_top.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0" android:toYDelta="50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>