如何在android中背靠背4图像闪烁图像

时间:2015-02-26 12:07:24

标签: java android animation android-animation

我想将图像背对背闪烁至最多4张图像。这意味着1个图像显示n消失,当第1个图像消失时显示第2个图像,当第2个图像消失时显示第3个图像,此过程继续按照我的要求,图像在运行时加载。

2 个答案:

答案 0 :(得分:0)

如果图片是基于网址或上线的话,那么

int pos=0;
    String[] urlsOfimages;

     private Handler blnkHandler = new Handler() {

          public void handleMessage(Message msg) {
           switch(msg.what) {
           case STOPSPLASH:
               //Set image here form the urlsOfimages[pos]
            img.setVisibility(ImageView.GONE);
            resumeMessage = new Message();
            resumeMessage.what = RESUMESPLASH;
            resumeHandler.sendMessageDelayed(resumeMessage, SPLASHTIME);

            pos++;

//Logic could be improve by your self by sending What pram, i just give you and idea
            if(pos==5)
                pos=0;

           }
          }
         };

         private Handler resumeHandler = new Handler() {

          public void handleMessage(Message msg) {
           switch(msg.what) {
           case RESUMESPLASH:
            img.setVisibility(ImageView.VISIBLE);
            stopMessage.what = STOPSPLASH;
            blnkHandler.sendMessageDelayed(stopMessage, SPLASHTIME);
           }
          }
         };

和创建方法

blnkHandler.sendMessageDelayed(STOPSPLASH, SPLASHTIME);

如果图片在可绘制文件夹中可用,则

AnimationDrawable myAnimationDrawable;
    public void loadAnimation()
    {
        ImageView myAnimation = (ImageView)findViewById(R.id.progress_bar);
         myAnimationDrawable= (AnimationDrawable)myAnimation.getDrawable();
        myAnimationDrawable.start();        
    }
中的

<?xml version="1.0" encoding="utf-8"?>
<animation-list android:id="@+id/progress_horizontal" android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/loading_0" android:duration="500" />
        <item android:drawable="@drawable/loading_1" android:duration="500" />
        <item android:drawable="@drawable/loading_2" android:duration="500" />
        <item android:drawable="@drawable/loading_3" android:duration="500" />
        <item android:drawable="@drawable/loading_4" android:duration="500" />
     </animation-list> 

如果必须在运行时选择图像,则按语法创建动画列表

    animation = new AnimationDrawable();
    animation.addFrame(getResources().getDrawable(R.drawable.img1), 100);
    animation.addFrame(getResources().getDrawable(R.drawable.img2), 1000);
    animation.addFrame(getResources().getDrawable(R.drawable.img3), 1000);
    animation.setOneShot(false);

    ImageView imageAnim =  (ImageView) findViewById(R,id.img);
    imageAnim.setBackgroundDrawable(animation);
    imageAnim.post(new Runnable() {

        @Override
        public void run() {
            animation.start();   
        }
    });

答案 1 :(得分:0)

尝试以下代码: -

     private void loadingAmin(final TextView loading) {

            final Animation pokeLoadingAnim = new Animation() {

                    int step = 0;

                    @Override
                    protected void applyTransformation(float interpolatedTime,
                                    Transformation t) {
                            // TODO Auto-generated method stub
                            super.applyTransformation(interpolatedTime, t);
                            if (interpolatedTime == 0) {
                                    loading.setText("Loading");
                                    step = 1;
                            }
                            if ((interpolatedTime / 0.3) > step) {
                                    loading.setText(loading.getText() + ".");
                                    ++step;
                            }

                    }
            };
            pokeLoadingAnim.setDuration(2000);
            pokeLoadingAnim.setRepeatCount(Animation.INFINITE);
            pokeLoadingAnim.setInterpolator(new LinearInterpolator());
            pokeLoadingAnim.setRepeatMode(Animation.RESTART);
            loading.startAnimation(pokeLoadingAnim);
    }

调用此方法

            TextView layout = (TextView) getView().findViewById(R.id.loading);
            loadingAmin(layout);