//java代码动态加载动画 或者res/anim/中加载
private void setRepaymentDate(NewProductInfoBean productInfo) { ArrayList<Integer> res = new ArrayList<>(); res.add(R.mipmap.day_left_1); res.add(R.mipmap.day_left_2); res.add(R.mipmap.day_left_3); res.add(R.mipmap.day_left_4); res.add(R.mipmap.day_left_5); res.add(R.mipmap.day_left_6); res.add(R.mipmap.day_left_7); res.add(R.mipmap.day_left_8); res.add(R.mipmap.day_left_9); res.add(R.mipmap.day_left_10); res.add(R.mipmap.day_left_11); res.add(R.mipmap.day_left_12); res.add(R.mipmap.day_left_13); res.add(R.mipmap.day_left_14); res.add(R.mipmap.over_due); mllOverdueDay.setVisibility(View.GONE); ad = new AnimationDrawable(); isOverdue = false; if (productInfo.getData().getOrderDetail().getStatus() == 17)//逾期 { handler.removeMessages(100); mllOverdue.setVisibility(View.VISIBLE); for (int i = 0; i < res.size(); i++) { ad.addFrame(getResources().getDrawable(res.get(i)), 100); } ad.setOneShot(true); mimRepaymentProgress.setBackground(ad); ad.start(); handler.sendEmptyMessageDelayed(100, 1500); isOverdue = true; } else { mtvRepaymentStatement.setText(R.string.home_prepayment); mllOverdue.setVisibility(View.GONE); for (int i = 0; i < 15 - productInfo.getData().getOrderDetail().getRepayDay(); i++) { ad.addFrame(getResources().getDrawable(res.get(i)), 100); } ad.setOneShot(true); mimRepaymentProgress.setBackground(ad); ad.start(); } }
Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { switch (msg.what) { case 100: AnimationSet animationSet = new AnimationSet(true); ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1, 0.5f, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(100); animationSet.addAnimation(scaleAnimation); //启动动画 mllOverdueDay.startAnimation(animationSet); mllOverdueDay.setVisibility(View.VISIBLE); break; } return false; } });
当启动其他Fragment,Activity并未对本界面进行销毁时AnimationDrawable会停止(回到第一张图片),
返回本界面是可以再次ad.start();//启动动画handler.sendEmptyMessageDelayed(100, 1400);//1400整个动画所需时间,(如有多次启动动画的代码需要handler.removeMessages(100);
<ImageSwitcher android:id="@+id/image_SwitcherTop" android:layout_width="match_parent" android:layout_height="250dp" android:layout_centerHorizontal="true" android:layout_marginTop="80dp" />
imgIds = new int[]{R.mipmap.icon_splash_top_1, R.mipmap.icon_splash_top_2, R.mipmap.icon_splash_top_3}; imageSwitcherTop.setFactory(this); imageSwitcherTop.setImageResource(imgIds[currentPosition % imgIds.length]);
extends BaseActivity implements ViewSwitcher.ViewFactory @Override public View makeView() { return new ImageView(this); }
左右滑动事件监听(可以添加动画效果)
@Override public boolean onTouchEvent(MotionEvent event) { //继承了Activity的onTouchEvent方法,直接监听点击事件 if (event.getAction() == MotionEvent.ACTION_DOWN) { //当手指按下的时候 downx = event.getX(); } if (event.getAction() == MotionEvent.ACTION_UP) { //当手指离开的时候 upx = event.getX(); if(firstTime){ if (downx > upx && currentPosition < imgIds.length - 1) { currentPosition++; imageSwitcherTop.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in)); imageSwitcherTop.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.lift_out)); imageSwitcherTop.setImageResource(imgIds[currentPosition % imgIds.length]); } else if (upx > downx && currentPosition > 0) { currentPosition--; imageSwitcherTop.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in)); imageSwitcherTop.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_out)); imageSwitcherTop.setImageResource(imgIds[currentPosition % imgIds.length]); } tvTrynow.setVisibility(currentPosition == (imgIds.length - 1) ? View.VISIBLE : View.GONE); setImageBackground(currentPosition); } } return super.onTouchEvent(event); }