我想滑动ViewAnimator
从右到左的视图以获取以前的视图。
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/btn_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/btn_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PREVIOUS" />
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT" />
</LinearLayout>
<ViewAnimator
android:id="@+id/view_animator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/btn_layout">
</ViewAnimator>
</RelativeLayout>
这是我的活动
package com.example.ronem.testing;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewAnimator;
/**
* Created by ronem on 3/17/16.
*/
public class ViewAnimatorActivity extends AppCompatActivity {
Button btnPrev, btnNext;
ViewAnimator viewAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_animator_layout);
btnPrev = (Button) findViewById(R.id.btn_previous);
btnNext = (Button) findViewById(R.id.btn_next);
viewAnimator = (ViewAnimator) findViewById(R.id.view_animator);
addImageView();
Animation inAnimation = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
viewAnimator.setInAnimation(inAnimation);
viewAnimator.setOutAnimation(outAnimation);
btnPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewAnimator.showPrevious();
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewAnimator.showNext();
}
});
}
private void addImageView() {
int[] drawable = new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.images};
for (int i = 0; i < drawable.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(drawable[i]);
imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
viewAnimator.addView(imageView);
}
}
}
每当我单击 Next 按钮时 imageView 从左向右滑动,效果很好,但是当我单击上一个按钮时,它会显示上一个 imageView,这看起来不错,但唯一的问题是图像视图再次从左向右滑动,但我想单击上一个按钮时从右向左滑动图像。我应该向 ViewAnimator 添加什么属性?任何建议都会有所帮助。