简体   繁体   中英

image in image button blinks once when the animation of outline of the imageview ends

public class MainActivity extends Activity {

ValueAnimator animator;
ImageButton testImageButton;
float rad=1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    testImageButton = findViewById(R.id.ib_test);
    testImageButton.setOutlineProvider(outlineProvider);
    testImageButton.setClipToOutline(true);

    animator = (ValueAnimator) 
    AnimatorInflater.loadAnimator(this,R.animator.radius);
    animator.addUpdateListener(mListener);

}


public void clickedAnimator(View v){
    animator.start();
}


public void testMe(View v){
    l("testMe");
}

ValueAnimator.AnimatorUpdateListener mListener = new 
                            ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        float i= (float) valueAnimator.getAnimatedValue();
        rad = i;
        testImageButton.setOutlineProvider(outlineProvider);
    }
};


ViewOutlineProvider outlineProvider = new ViewOutlineProvider() {
    @Override
    public void getOutline(View view, Outline outline) {
        outline.setRoundRect(0,0,view.getWidth(),view.getHeight(),        
                                          rad*((float)view.getWidth())/2);
    }
};



public void l(String s){
    Log.v(getClass().getSimpleName(),s);
}

}

I am trying to animate the outline of an ImageView from Circle to Rectangle by constantly updating the ViewOutlineProvider that is set to the imageview.

Everything works fine but when the animation ends, the Image in the imageView Blinks once.

The value of rad changes from 1 to 0 in 500ms.

Hmmm, this line might help you: android:fillAfter="true"

Place this in your animation XML file inside the set attribute like:

 <set [...]
     android:fillAfter="true">

This helped me on a number of occasions when the animation was bugging at the end. Might work for you, might not, it's worth trying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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