Fajar Khan Fajar Khan - 2 months ago 17
Android Question

Blinking border of a View

I have a

View
and a border which i wanted to blink specific time. I tried to to make blink but the problem is whole view is blinking. i know its becource i mentioned
View
inside a
Linearlayout
which background set to a border stock.

Here is the code:

xml:
<LinearLayout
android:layout_width="14dp"
android:layout_height="14dp
android:id="@+id/rect2"
android:background="@drawable/cir"
>

<View
android:layout_width="5dp"
android:layout_height="5dp"
android:background="#14FFFFFF"
android:layout_marginTop="3dp"
android:layout_marginLeft="3dp"
android:id="@+id/inrect2"
/>
</LinearLayout>


java:

//somecode
setContentView(R.layout.activity_main);
re2=(LinearLayout)findViewById(R.id.rect2);
myView2= findViewById(R.id.inrect2);

((GradientDrawable)re1.getBackground()).setStroke(1,Color.WHITE);
AlphaAnimation blinkanimation= new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
blinkanimation.setDuration(500); // duration - half a second
blinkanimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
blinkanimation.setRepeatCount(28); // Repeat animation infinitely
blinkanimation.setRepeatMode(Animation.REVERSE);
re1.startAnimation(blinkanimation);


re1.startAnimation(blinkanimation)
seems start animating whole linear layout,Is there anyway i can make blink only borders?
Any help would be appreciated! Thanks!

Answer

Instead of blinking the view, try blinking the outer linear layout, the view and linear layout should have padding between them in order to give a border like effect.

[Edit]

  <Linearlayout  
    android:background="#00000000"
    android:layout_gravity="center"
    android:layout_width="20dp" 
    android:layout_height="20dp" 
 />

  <View
    android:layout_width="16dp" 
    android:layout_height="16dp" 
    android:layout_gravity="center"
    android:gravity="center"
    android:background="#AA000000" />

</FrameLayout>