Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make simulate animation in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 53
    Comment on it

    In the below example I have created simulate animation in android. Here I have added first TextView and ImageView in activity_main.xml layout ,In MainActivity I have used Runnable Thread. On this animation image will be rotate like watch. You can see below example code it clearly describe you How to make simulate animation in android.

     

    Step(1)activity_main.xml layout-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Animation"
            android:layout_marginTop="20dp"
            android:textAlignment="center"
            android:textColor="#ee3a60df"
            android:textSize="20dp"
            />
        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
        <ImageView
            android:id="@+id/myImageView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="center"
            />
    </LinearLayout>

    Step(2)- MainActivity-
     

    public class MainActivity extends AppCompatActivity {
    
        int i =0;
        TextView anim;
        ImageView image;
    
        Handler handler = new Handler(){
    
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                update_i();
            }
        };
    
        private void update_i() {
            switch (i){
                case 0:
                    i++;
                    image.setImageResource(R.drawable.one);
                    break;
                case 1:
                    i++;
                    image.setImageResource(R.drawable.two);
                    break;
                case 2:
                    i++;
                    image.setImageResource(R.drawable.three);
                    break;
                case 3:
                    i=0;
                    image.setImageResource(R.drawable.four);
                    break;
            }
            anim.setText(String.valueOf(i));
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            anim = (TextView) findViewById(R.id.text);
            image = (ImageView) findViewById(R.id.myImageView);
    
    
        }
        @Override
        protected void onStart(){
            super.onStart();
    
            Thread myThread= new Thread(new Runnable() {
                @Override
                public void run() {
                    while(true){
                        try{
                            handler.sendMessage(handler.obtainMessage());
                            Thread.sleep(1000);
    
                        }
                        catch (Throwable t){
    
                        }
                    }
    
                }
            });
            myThread.start();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: