9

我想让我的 ImageView 看起来闪烁。更准确地说,在像单击按钮这样的事件中,我希望 ImageView 将其 src 更改 1 秒钟,然后再更改回来,然后再等。然后它应该停止,并且 ImageView 应该具有与以前相同的 src。我试图根据另一个问题做某事,但它不起作用......

private class MyHandler extends Handler {
    public ImageView imgView;

    @Override
    public void handleMessage(Message msg) {
        if (imgView != null) {
            switch (msg.what) {
            case 0:
                imgView.setImageResource(R.drawable.red_img);
                break;
            case 1:
                imgView.setImageResource(R.drawable.white_img);
                break;

            }
        }
        super.handleMessage(msg);
    }
}

(...)

MyHandler blinker = new MyHandler();
blinker.imgView = imgView;
for (int j = 0; j < 5; j++) {
    Message msg = new Message();
    if (j % 2 == 0) {
        msg.what = 0;
    } else {
        msg.what = 1;
    }

    blinker.sendMessageDelayed(msg, j * 300);
}

有谁知道如何做到这一点(如果可以做到的话)。谢谢!

4

4 回答 4

24

或者您可以通过在 xml 中定义它来简单地使用AnimationDrawable

<animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/state1" android:duration="1000" />
    <item android:drawable="@drawable/state2" android:duration="1000" />
</animation-list>

并呼吁start()它:

ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setImageResource(R.drawable.my_blinking_drawable);
AnimationDrawable frameAnimation = (AnimationDrawable) img.getDrawable();
frameAnimation.start();
于 2013-08-15T08:28:01.340 回答
3

以下代码将在屏幕上放置一个 ImageView,当单击 ImageView 时,它将在 2 个不同的图像之间闪烁 5 秒:

int numberOfTimesToBlink = 4;
long blinkInterval = 1000;  // 1 second

final ImageView blinkingImageView = (ImageView)findViewById(R.id.blinkingImageView);

// setBackgroundDrawable is deprecated but it still works, and the newer method (setBackground) has min API level of 16
blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage));
blinkingImageView.setTag("yourFirstImage");

final CountDownTimer blinkTimer = new CountDownTimer((numberOfTimesToBlink+1)*1000, blinkInterval) {
    @Override
    public void onTick(long millisUntilFinished) {
        if (blinkingImageView.getTag() == "yourFirstImage") {
            blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourSecondImage));
            blinkingImageView.setTag("yourSecondImage");
        }
        else if (blinkingImageView.getTag() == "yourSecondImage") {
            blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage));
            blinkingImageView.setTag("yourFirstImage");
        }
    }

    @Override
    public void onFinish() {

    }
};

blinkingImageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        blinkTimer.start();
    }
});

和 xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ImageView android:id="@+id/blinkingImageView"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />

</RelativeLayout>
于 2013-08-14T15:33:42.123 回答
1

您可以更改临时源图像并启动 Timer 1 秒,之后可以再次显示原始图像:

image.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
        ImageView img = (ImageView) v;
        img.setImageResource(R.drawable.temporary_image);
        Timer t = new Timer();
        t.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        img.setImageResource(R.drawable.main_image)
                    }
                });
            }
        }, 1000);
    }
});
于 2013-08-14T14:31:11.760 回答
0
<?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <alpha android:fromAlpha="0.0"
            android:toAlpha="1.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:duration="600"
            android:repeatMode="reverse"
            android:repeatCount="infinite"/>
    </set>

或者看看这个

关联

于 2013-08-14T14:25:38.330 回答