Pages

Sunday 10 February 2013

  Android Blink Text :


Main Activity :

import android.app.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        blinkText();
     
    }
 
    private void blinkText(){
        final Handler handler = new Handler();
        new Thread(new Runnable() {
            public void run() {
            int timeToBlink = 1000;  
            try{
            Thread.sleep(timeToBlink);
        }catch (Exception e) {
       
        }
            handler.post(new Runnable() {
                public void run() {
                    TextView txt = (TextView) findViewById(R.id.tv);
                    if(txt.getVisibility() == View.VISIBLE){
                        txt.setVisibility(View.INVISIBLE);
                    }else{
                        txt.setVisibility(View.VISIBLE);
                    }
                    blinkText();
                }
                });
            }}).start();
   }
 
    public void blinkText2(){
    TextView myText = (TextView) findViewById(R.id.tv );

    Animation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(50);
    anim.setStartOffset(20);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setRepeatCount(Animation.INFINITE);
    myText.startAnimation(anim);
    }
 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
 
 
}

main.xlm :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity"
        android:id="@+id/tv" />
    
</RelativeLayout>




Android Blink Animation as LED Bulb :


 Create  a new folder in res  as anim-->

 tween.xml

<set >
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="100"
    android:repeatMode="reverse"
    android:repeatCount="infinite" />
</set>

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="80dp" >

        <FrameLayout
            android:id="@+id/rightIcon"
            android:layout_width="30.0dip"
            android:layout_height="30.0dip" >

            <ImageView
                android:id="@+id/bsecond"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_red"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/afirst"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_yellow"
                android:visibility="visible" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/rightIcon"
            android:layout_width="30.0dip"
            android:layout_height="30.0dip" >

            <ImageView
                android:id="@+id/bfourth"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_green"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/athird"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_red"
                android:visibility="visible" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/rightIcon"
            android:layout_width="30.0dip"
            android:layout_height="30.0dip" >

            <ImageView
                android:id="@+id/bsix"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_red"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/afifth"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_green"
                android:visibility="visible" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/rightIcon"
            android:layout_width="30.0dip"
            android:layout_height="30.0dip" >

            <ImageView
                android:id="@+id/bsecond"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_red"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/cfirst"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_yellow"
                android:visibility="visible" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/rightIcon"
            android:layout_width="30.0dip"
            android:layout_height="30.0dip">

            <ImageView
                android:id="@+id/bsix"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_red"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/cfifth"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_green"
                android:visibility="visible" />
        </FrameLayout>

  </TableRow>

</RelativeLayout>


MainActivity


import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends Activity {

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

private void animate() {

ImageView myImageView4 = (ImageView) findViewById(R.id.afirst);
ImageView myImageView5 = (ImageView) findViewById(R.id.athird);
ImageView myImageView6 = (ImageView) findViewById(R.id.afifth);
ImageView myImageView7 = (ImageView) findViewById(R.id.cfirst);
ImageView myImageView9 = (ImageView) findViewById(R.id.cfifth);


Animation myFadeInAnimation = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.tween);

myImageView4.startAnimation(myFadeInAnimation);
myImageView5.startAnimation(myFadeInAnimation);
myImageView6.startAnimation(myFadeInAnimation);
myImageView7.startAnimation(myFadeInAnimation);
myImageView9.startAnimation(myFadeInAnimation);

}

}

OutPut :