Create the sample application that rotate a green rectangle shape / image and after that double the size of that object and then shrink back to our starting size. - tween animation example with code - android programming
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dharmik.p10">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dharmik.p10">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.dharmik.p10.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/iv_1"
tools:ignore="ContentDescription"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.dharmik.p10.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/iv_1"
tools:ignore="ContentDescription"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
//strings.xml
<resources>
<string name="app_name">p10</string>
</resources>
<string name="app_name">p10</string>
</resources>
//anim/all.xml
<?xml version="1.0" encoding="utf-8"?>
<set>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="4000"></alpha>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="4000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="100%"
android:pivotY="50%"
android:duration="2000"></rotate>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="6000"
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.0"
android:duration="4000"></scale>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="10000"
android:fromXDelta="0%"
android:toXDelta="200%"
android:fromYDelta="0%"
android:toYDelta="1000%"
android:duration="2000"></translate>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="12000"
android:fromXDelta="0%"
android:toXDelta="-200%"
android:fromYDelta="0%"
android:toYDelta="-1000%"
android:duration="2000"></translate>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="14000"
android:fromXScale="1.0"
android:toXScale="0.5"
android:fromYScale="1.0"
android:toYScale="0.5"
android:duration="4000"></scale>
</set>
<set>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="4000"></alpha>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="4000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="100%"
android:pivotY="50%"
android:duration="2000"></rotate>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="6000"
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.0"
android:duration="4000"></scale>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="10000"
android:fromXDelta="0%"
android:toXDelta="200%"
android:fromYDelta="0%"
android:toYDelta="1000%"
android:duration="2000"></translate>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="12000"
android:fromXDelta="0%"
android:toXDelta="-200%"
android:fromYDelta="0%"
android:toYDelta="-1000%"
android:duration="2000"></translate>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="14000"
android:fromXScale="1.0"
android:toXScale="0.5"
android:fromYScale="1.0"
android:toYScale="0.5"
android:duration="4000"></scale>
</set>
//MainActivity.java
package
com.example.dharmik.p10;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.iv_1);
ShapeDrawable rect=new ShapeDrawable(new RectShape());
rect.setIntrinsicHeight(100);
rect.setIntrinsicWidth(200);
rect.getPaint().setColor(Color.GREEN);
iv.setImageDrawable(rect);
Animation anm= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.all);
iv.setAnimation(anm);
}
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.iv_1);
ShapeDrawable rect=new ShapeDrawable(new RectShape());
rect.setIntrinsicHeight(100);
rect.setIntrinsicWidth(200);
rect.getPaint().setColor(Color.GREEN);
iv.setImageDrawable(rect);
Animation anm= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.all);
iv.setAnimation(anm);
}
}
//Output
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment