1. 程式人生 > >AnimationDrawable監聽播放結束及ImageSwitcher動畫圖片切換,帶動畫

AnimationDrawable監聽播放結束及ImageSwitcher動畫圖片切換,帶動畫

//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);
}

ImageSwitcher動畫圖片切換

<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);
}
<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);
}