如何通过改变imageview的颜色使动画像眨眼一样?

问题描述 投票:1回答:5

我正在制作音乐游戏。如果用户按提示按钮以知道要按哪个键。我想通过将ImageView颜色以编程方式从一种颜色改为另一种颜色(在两种颜色之间切换)1秒钟来显示对bitmap的眨眼效果。

这就是我如何以编程方式更改位图的颜色:

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

我想我们可以使用Handler,但我没有得到如何产品眨眼效果。

android bitmap android-imageview
5个回答
0
投票

你可以使用处理程序

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

0
投票

你可以实现你想要的就是这样。

创建3个可绘制文件。

第一个可绘制的

//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

 <?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

  <?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>

在你的代码中

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

完成动画后,您可以设置所需的任何背景。

可能是这样的。

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

0
投票

我通过以下方式实现了眨眼效果:

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

希望这可能会帮助别人。如果有更好的方法可以做到这一点。建议。


0
投票

你可以实现这种混合两种颜色,这样就可以改变这两种颜色

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

0
投票

在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>

然后,在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>
© www.soinside.com 2019 - 2024. All rights reserved.