1

I have a custom drawable which I want to make it blink.Currently I can see the drawable being shown next to the text but no blinking affect

public class CustomDrawable extends Drawable {
private Context mContext;
private int mColor;

public CustomDrawable(Context context, int color) {
    mContext = context;
    mColor = color;
}

@Override
public void draw(Canvas canvas) {
    int x = 30;
    int y = 30;
    int radius = 15;
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(mContext.getResources().getColor(R.color.transparent));
    paint.setAntiAlias(true);
    canvas.drawPaint(paint);
    paint.setColor(mColor);
    canvas.drawCircle(x / 2, y / 2, radius, paint);
}

@Override
public void setAlpha(int alpha) {

}

@Override
public void setColorFilter(ColorFilter cf) {

}

@Override
public int getOpacity() {
    return 0;
}

}

How can I make the custom drawable so that it blinks I tried changing the alpha but there was no effect on it.

 mTextView = (TextView) findViewById(R.id.textView);
    AnimationDrawable drwable = new AnimationDrawable();
    drwable.addFrame(new CustomDrawable(this, Color.RED), 50);
    drwable.setAlpha(255);
    drwable.setEnterFadeDuration(500);
    mTextView.setCompoundDrawables(null, drwable, null, null);
    drwable.start()
luckysing_noobster
  • 1,933
  • 5
  • 24
  • 50

1 Answers1

0

Use xml animation for your drawable.

create a folder anim in res folder. Then create blink.xml file.

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

Then use it as below:

Animation blink;
.
.
// load your animation
animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.blink);
.
.
// set animation listener
blink.setAnimationListener(this);


mydrawable.startAnimation(blink);