简体   繁体   English

如何通过更改imageview的颜色使闪烁像动画一样?

[英]How to make blink like animation by changing color of imageview?

I am making a music game. 我正在做一个音乐游戏。 If the user press hint button to know which key to press. 如果用户按提示按钮知道要按哪个键。 I want to show a blink effect on the ImageView by changing the bitmap color programmatically from one color to another repeatedly(switching between two colors) for 1 second. 我想通过以编程方式将bitmap颜色从一种颜色重复更改为另一种颜色(在两种颜色之间切换)1秒钟,以在ImageView上显示眨眼效果。

This is how I am changing the color of bitmap programmatically: 这就是我以编程方式更改位图颜色的方式:

nextImage.setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.MULTIPLY);

I think we can use Handler for this but I am not getting how to product blink effect. 我认为我们可以为此使用Handler ,但是我没有得到如何产生眨眼效果的方法。

you can use handler 你可以使用处理程序

Structure
 -onClick 
   -change color
  wait 1 second
   -change color
   action() //what click shuld do on btn

You can achieve what you want is like this. 您可以实现这样的目标。

create 3 drawable files. 创建3个可绘制文件。

first drawable 第一个可绘制

//color1.xml //color1.xml

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle">
 <solid android:color="#hexcodeofcolor1" />
 <corners android:bottomRightRadius="8dip"
    android:bottomLeftRadius="8dip"  
    android:topRightRadius="8dip"
    android:topLeftRadius="8dip"/>
 </shape>

color2.xml color2.xml

 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle">
 <solid android:color="#hexcodeofcolor2" />
 <corners android:bottomRightRadius="8dip"
    android:bottomLeftRadius="8dip"  
    android:topRightRadius="8dip"
    android:topLeftRadius="8dip"/>
  </shape>

imageviewtempgif.xml imageviewtempgif.xml

  <?xml version="1.0" encoding="utf-8"?>
  <animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
android:useLevel="true">

<item
    android:drawable="@drawable/hexcodeofcolor1"
    android:duration="500"
    android:useLevel="true"/>
<item
    android:drawable="@drawable/hexcodeofcolor2"
    android:duration="500"
    android:useLevel="true"/>
  </animation-list>

And in your code 在你的代码中

imageView.setImageResource(R.drawable.imageviewtempgif);
    ((Animatable) imgWiFi.getDrawable()).start();

Once you done with the animation you can set whatever background you want. 完成动画后,您可以设置所需的任何背景。

may be something like this. 可能是这样的。

imageView.setImageResource(R.drawable.imageviewtempgif);
    ((Animatable) imgWiFi.getDrawable()).start();
 final Handler handler = new Handler();
 handler.postDelayed(new Runnable() {
 @Override
  public void run() {
  //set to normal background.
  }
  }, 1000);

I achieved the blink effect by the following way: 我通过以下方式实现了眨眼效果:

final String[] colors = {"#00BCD4","#FFFFFF","#00BCD4","#FFFFFF","#00BCD4","#FFFFFF","#00BCD4","#FFFFFF"};
                        for(int i=0;i<8;i++){
                            final int j = i;
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    nextImage.setColorFilter(Color.parseColor(colors[j]), PorterDuff.Mode.MULTIPLY);
                                }
                            },200 * j);

Hope this might help someone else. 希望这可以帮助其他人。 If there are better ways of doing it. 如果有更好的方法可以做到这一点。 Do suggest. 提出建议。

You can achieve this blending two colors like this keep changing this two colors 您可以像这样实现两种颜色的融合,请不断更改这两种颜色

/**
     * Blend {@code color1} and {@code color2} using the given ratio.
     *
     * @param ratio of which to blend. 0.0 will return {@code color1}, 0.5 will give an even blend,
     *              1.0 will return {@code color2}.
     */
    public static int blendColors(int color1, int color2, float ratio) {
        final float inverseRatio = 1f - ratio;
        float a = (Color.alpha(color1) * inverseRatio) + (Color.alpha(color2) * ratio);
        float r = (Color.red(color1) * inverseRatio) + (Color.red(color2) * ratio);
        float g = (Color.green(color1) * inverseRatio) + (Color.green(color2) * ratio);
        float b = (Color.blue(color1) * inverseRatio) + (Color.blue(color2) * ratio);
        return Color.argb((int) a, (int) r, (int) g, (int) b);
    }

Create a color file in res > color folder, namely icon_click_color.xml with followed code: 在res> color文件夹中创建一个颜色文件,即icon_click_color.xml ,其代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:color="@color/colorDefault" />
    <item android:state_focused="true" android:state_pressed="true" android:color="@color/colorPressed" />
    <item android:state_focused="false" android:state_pressed="true" android:color="@color/colorPressed" />
    <item android:color="@color/colorDefault" />
</selector>

Then, in you ImageView, set the tint property, for example: 然后,在ImageView中,设置tint属性,例如:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_menu_setting"
    app:tint="@color/icon_click_color">
</ImageView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM