2011-07-07 41 views
3

我试图创建一个图像幻灯片类似于this创建幻灯片淡出图像转换成相互转换

我发现this tutorial这说明我如何使图像褪色相互转化,其中工程真的很好,但我无法找到任何示例如何让它在同一时间进行移动/缩放,所以我正在尝试去适应它。

我已经添加了一个缩放动画,并将其与动画集中的alpha动画放在一起,但是我无法让它正常工作,它只对每个其他图像执行动画,然后当缩放开始时它会缩放单向和切换,然后缩放另一种方式。

我相当新的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> 
+0

你们看透了这一点? – Ian

+0

不...还在努力! – Bex

回答

8

最后对它进行了排序......由于多个动画无法与动画结束事件紧密结合,所以它非常啰嗦,所以可能不是最好的方法,但它起作用,我很高兴!

谁想要知道如何..或想适应它,使之更好地在这里它是:

主要活动(MyTransition。JAVA):

import android.os.Handler; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 

import java.util.Vector; 

public class MyTransition extends AppCompatActivity { 

    ImageView slide_0; 
    ImageView slide_1; 
    ImageView lastSlide; 
    Vector<Integer> imageIds; 
    LinearLayout linLayout; 
    int count = 0; 

    private Handler transparencyHandler = new Handler(); 
    private Handler timerHandler = new Handler(); 
    private Runnable transparencyTimer = new Runnable() { 
     public void run() { 
      if (lastSlide == slide_0) { 
       slide_1.setBackgroundColor(getResources().getColor(android.R.color.transparent)); 
       slide_1.setImageResource(0); 
      } else { 
       slide_0.setBackgroundColor(getResources().getColor(android.R.color.transparent)); 
       slide_0.setImageResource(0); 
      } 
     } 
    }; 
    private Runnable timer = new Runnable() { 
     public void run() { 
      if (lastSlide == slide_0) { 
       slide_1.setImageResource(0); 
       slide_1.setImageResource(imageIds.get((count + 1) 
         % (imageIds.size()))); 
       slide_1.startAnimation(AnimationUtils 
         .loadAnimation(MyTransition.this, 
           R.anim.transition_down)); 
       lastSlide = slide_1; 
      } else { 
       slide_0.setImageResource(0); 
       slide_0.setImageResource(imageIds.get((count + 1) 
         % (imageIds.size()))); 
       slide_0.startAnimation(AnimationUtils 
         .loadAnimation(MyTransition.this, 
           R.anim.transition_up)); 
       lastSlide = slide_0; 
      } 
      count++; 
      transparencyHandler.postDelayed(transparencyTimer, 1000); 
      timerHandler.postDelayed(timer, 8000); 
     } 
    }; 

    @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); 
     imageIds = new Vector<Integer>(); 
     imageIds.add(R.drawable.first_image); 
     imageIds.add(R.drawable.second_image); 

     // Load Image 1 
     slide_0.setImageResource(imageIds.get(0)); 
     slide_0.startAnimation(AnimationUtils.loadAnimation(this, 
       R.anim.transition_down)); 

     lastSlide = slide_0; 
     timerHandler.postDelayed(timer, 8000); 
    } 
} 

布局(test2.xml):

<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" 
     android:scaleType="centerCrop" /> 
    <ImageView 
     android:id="@+id/slide_2" 
     android:layout_gravity="center_vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scaleType="centerCrop" /> 
</FrameLayout> 

动画(transition_down.xml):

<set 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shareInterpolator="false"> 
<alpha 
    android:fromAlpha="0.0" 
    android:toAlpha="2.0" 
    android:duration="2000" 
    android:repeatCount="0" 
    android:fillAfter="true" /> 
    <scale 
     android:fromXScale="1" 
     android:toXScale="1.2" 
     android:fromYScale="1" 
     android:toYScale="1.2" 
     android:duration="10000" 
     android:repeatCount="0" 
     android:fillAfter="true" /> 
    <alpha 
     android:fromAlpha="2.0" 
     android:toAlpha="0.0" 
     android:duration="2000" 
     android:repeatCount="0" 
     android:startOffset="8000" 
     android:fillAfter="true" /> 
</set> 
+0

感谢您分享此代码。我已经实现了Java并使用了内置的android.R.anim.fade_in转换。 – tiptopjat

+1

其中是transaction_up.xml的代码 – Uday

+0

最好的工作:)其工作完美兄弟 – amisha

0

要将两个独立的动画。这就是他们间隔的原因。

作为一名初学者,我建议养成使用xml制作动画(以及所有可能的资源)的习惯。它更加冗长,稍后更容易更改资源(以及允许您在运行时根据屏幕大小,语言等自动更改资源)。在实现一些自定义功能之前,还要习惯于真正用尽所有的Android小部件。 就动画资源而言,请看示例animation resources

对于视图转换,我通常会推荐一个ViewFlipper。但是,由于您需要自定义转换,因此您可能需要降级并使用ViewnAnimator。我知道你想循环动画,但首先得到这个动画工作。我认为这可能还是与ViewFlipper工作,这将是这个样子:

<ViewFlipper 
     android:id="@+id/VF" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inAnimation="@anim/yourFirstAnimation 
     android:flipInterval="@integer/flip_interval_time"> 
     <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" /> 
    </ViewFlipper> 

onCreate(Bundle b) { 
    setContentView(R.layout.main); 
    mVF = (ViewFlipper) findViewById(R.id.VF); 
    mVF.startFlipping(); 
} 

然后,当你想循环动画: 你flipInterval设置为0(我认为),不喜欢你的OnAnimationEnd做事件:

public void onAnimationEnd(Animation animation) { 
    Animation an = myAnimationContainer.getRandomAnimation(); 
    mVF.setInAnimation(an); 
    mVF.showNext(); 

} 

您不应该需要同步flipInterval时间,因为这一次真的取决于动画的长度。