[英]Animation of different images in list
我有不同的圖像,比方說100張左右。 現在,我想在它們上應用動畫。 我希望我的ImageView在指定的間隔后獲取每個圖像,但是當圖像發生變化時,每個圖像應該是FadeIn或FadeOut。 我將我的圖像放在@ drawable / [list_of_images] .xml文件中:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/a1" android:duration="1000"/>
<item android:drawable="@drawable/a2" android:duration="2000"/>
<item android:drawable="@drawable/a3" android:duration="3000"/>
<item android:drawable="@drawable/a4" android:duration="500"/>
然后我可以使用以下方法根據ImageView中的時間間隔成功更改這些圖像:
public class AnimTest extends Activity
{
AnimationDrawable myAnim;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.anim);
ImageView myIV = (ImageView) findViewById(R.id.image_view);
myIV.setBackgroundResource(R.drawable.list_of_images.xml);
myAnim = (AnimationDrawable) myIV.getBackground();
}
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
myAnim.start();
return true;
}
return super.onTouchEvent(event);
}
}
問題是我無法弄清楚如何對每一個圖像應用淡入淡出效果,而它們會被上面的動畫改變。 我可以在整個圖像列表中應用淡化動畫,但不能在每個圖像上執行此操作。 我是否正確的方向來實現這一功能? 如果沒有,請引導我走正確的道路。
此致,Khawar
您可以嘗試在動畫上設置重復計數到imagecount-1,然后在動畫中添加AnimationListener,在每次重復和開始時更改ImageView的背景圖像。
這是一個使用RoboGuice的簡單示例(它使代碼更清晰,但對於您的問題沒有任何區別): https : //github.com/bostonandroid/batgirl/blob/master/src/org/roboguice/蝙蝠女/ Batgirl.java
public class Batgirl extends RoboActivity {
// Views
@InjectView(R.id.content) LinearLayout linearLayout;
// Resources
@InjectResource(R.anim.spin) Animation spin;
@InjectResource(R.integer.max_punches) int MAX_PUNCHES;
// Other Injections
@Inject ChangeTextAnimationListener changeTextAnimationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up the animation
linearLayout.setAnimation(spin);
spin.setAnimationListener(changeTextAnimationListener);
spin.setRepeatCount(MAX_PUNCHES - 1);
spin.start();
}
}
/**
* A simple animation listener that swaps out the text between repeats.
*/
class ChangeTextAnimationListener implements AnimationListener {
@InjectView(R.id.hello) TextView helloView;
@Inject Fist fist;
@Inject PackageInfo info;
public void onAnimationRepeat(Animation animation) {
onAnimationStart(animation);
}
public void onAnimationStart(Animation animation) {
helloView.setText( getNextTextString() ); // getNextTextString() not shown in this example
}
public void onAnimationEnd(Animation animation) {
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.