Saturday, 22 June 2013

Animation

1. MainXml File

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/car_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#66ccff"
   

    tools:context=".MainActivity" >

    <ImageView
    android:id="@+id/sun"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/sun"
    android:paddingLeft="100dp"
    android:paddingTop="45dp"
    android:src="@drawable/sun" />

    <ImageView
    android:id="@+id/cloud1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/cloud"
    android:paddingLeft="170dp"
    android:paddingTop="70dp"
    android:src="@drawable/cloud" />
<ImageView
    android:id="@+id/cloud2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/cloud"
    android:paddingLeft="200dp"
    android:paddingTop="90dp"
    android:src="@drawable/cloud" />
<ImageView
    android:id="@+id/ground"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:contentDescription="@string/ground"
    android:padding="40dp"
    android:src="@drawable/ground" />
<ImageView
    android:id="@+id/window"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="@string/window"
    android:src="@drawable/window" />
<ImageView
    android:id="@+id/wheel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/wheel"
    android:padding="3dp"
    android:src="@drawable/steering_wheel" />
</RelativeLayout>


2. Main Java File

package com.example.design1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ImageView wheel = (ImageView)findViewById(R.id.wheel);
        AnimatorSet wheelSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.wheel_spin);
        wheelSet.setTarget(wheel);
        wheelSet.start();
        
      //get the sun view
        ImageView sun = (ImageView)findViewById(R.id.sun);
        //load the sun movement animation
        AnimatorSet sunSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.sun_swing);
        //set the view as target
        sunSet.setTarget(sun);
        //start the animation
        sunSet.start();
        
        ValueAnimator skyAnim = ObjectAnimator.ofInt
           (findViewById(R.id.car_layout), "backgroundColor",
           Color.rgb(0x66, 0xcc, 0xff), Color.rgb(0x00, 0x66, 0x99));
        skyAnim.setDuration(3000);
        skyAnim.setRepeatCount(ValueAnimator.INFINITE);
        skyAnim.setRepeatMode(ValueAnimator.REVERSE);
        skyAnim.setEvaluator(new ArgbEvaluator());
        skyAnim.start();
        
        ObjectAnimator cloudAnim = ObjectAnimator.ofFloat(findViewById(R.id.cloud1), "x", -350);
        cloudAnim.setDuration(3000);
        cloudAnim.setRepeatCount(ValueAnimator.INFINITE);
        cloudAnim.setRepeatMode(ValueAnimator.REVERSE);
        cloudAnim.start();
        ObjectAnimator cloudAnim2 = ObjectAnimator.ofFloat(findViewById(R.id.cloud2), "x", -300);
        cloudAnim2.setDuration(3000);
        cloudAnim2.setRepeatCount(ValueAnimator.INFINITE);
        cloudAnim2.setRepeatMode(ValueAnimator.REVERSE);
        cloudAnim2.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.main, menu);
        return true;
    }
    
}


3. Place ground.xml in drawable folder

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:shape="rectangle" >
    <solid android:color="#339933" />
</shape>

4. Place sun.xml in drawable folder

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:shape="oval" >
    <gradient
        android:endColor="#ffff6600"
        android:gradientRadius="150"
        android:startColor="#ffffcc00"
        android:type="radial"
        android:useLevel="false" />
    <size
        android:height="100dp"
        android:width="100dp" />
</shape>

5. Place window.xml in drawable folder

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#00000000" />
    <stroke

        android:width="40dp"
        android:color="#cccccc" />
</shape>

cloud.png


steering_wheel.png

Place above images in drawable folder.





No comments:

Post a Comment