Я пытаюсь создать слайд-шоу с изображением, похожим на this
Я нашел этот урок , который показывает мне, как заставить изображения плавно переходить друг в друга, что работает очень хорошо, но я не могу найти примеров, как заставить его выполнять перемещение / масштабирование одновременно время, поэтому я пытаюсь адаптировать его.
Я добавил масштабную анимацию и поместил ее вместе с альфа-анимацией в набор анимаций, но я не могу заставить ее работать правильно, она только выполняет анимацию на каждом другом изображении, а затем, когда масштабирование начинается, оно увеличивает один и переключается, а затем увеличивает масштаб.
Я довольно новичок в том, что Android еще не делал анимации, и мне трудно понять, как работает пример. Поэтому я с трудом могу это исправить.
Может кто-нибудь помочь мне понять, что я делаю неправильно? Я начинаю вырывать волосы!
Мой код Java:
public class TopListActivity extends Activity {
ImageView slide_0;
ImageView slide_1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
slide_0 = (ImageView) findViewById(R.id.slide_1);
slide_1 = (ImageView) findViewById(R.id.slide_2);
}
private static class AnimationTimer extends TimerTask implements
AnimationListener {
TopListActivity topList;
Vector<BitmapDrawable> images;
int count = 0;
public AnimationTimer(TopListActivity _topList) {
this.topList = _topList;
this.images = new Vector<BitmapDrawable>();
Resources resources = topList.getResources();
images.add((BitmapDrawable) resources.getDrawable(R.drawable.one));
images.add((BitmapDrawable) resources.getDrawable(R.drawable.two));
images.add((BitmapDrawable) resources.getDrawable(R.drawable.three));
images.add((BitmapDrawable) resources.getDrawable(R.drawable.four));
images.add((BitmapDrawable) resources.getDrawable(R.drawable.five));
images.add((BitmapDrawable) resources.getDrawable(R.drawable.six));
if (this.images.size() > 0) {
this.topList.slide_0.setBackgroundDrawable(this.images.get(0));
if (this.images.size() > 1) {
this.topList.slide_1.setBackgroundDrawable(this.images
.get(1));
}
}
this.count = 1;
}
public void launch() {
if (this.images.size() >= 2) {
(new Timer(false)).schedule(this, 100);
}
}
@Override
public void run() {
this.doit();
this.cancel();
}
private void doit() {
if ((this.count % 2) == 0) {
AnimationSet set = new AnimationSet(false);
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setStartOffset(5000);
animation.setDuration(3000);
animation.setFillAfter(true);
ScaleAnimation zoom = new ScaleAnimation(1, 1.20f, 1, 1.20f);
zoom.setStartOffset(0);
zoom.setDuration(8000);
zoom.setFillAfter(true);
set.addAnimation(animation);
set.addAnimation(zoom);
set.setAnimationListener(this);
this.topList.slide_1.startAnimation(set);
} else {
AnimationSet set = new AnimationSet(false);
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setStartOffset(5000);
animation.setDuration(3000);
animation.setFillAfter(true);
ScaleAnimation zoom = new ScaleAnimation(1.20f, 1, 1.20f, 1);
zoom.setStartOffset(0);
zoom.setDuration(8000);
zoom.setFillAfter(true);
set.addAnimation(animation);
set.addAnimation(zoom);
set.setAnimationListener(this);
this.topList.slide_1.startAnimation(set);
}
}
public void onAnimationEnd(Animation animation) {
if ((this.count % 2) == 0) {
this.topList.slide_1.setBackgroundDrawable(this.images
.get((this.count + 1) % (this.images.size())));
} else {
this.topList.slide_0.setBackgroundDrawable(this.images
.get((this.count + 1) % (this.images.size())));
}
this.count++;
this.doit();
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
}
@Override
public void onResume() {
super.onResume();
(new AnimationTimer(this)).launch();
}
}
и мой макет:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/slide_1"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/slide_2"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>