Breaking News
Loading...
Friday 19 October 2012

Create Animation using Matrix

07:25
The former exercise demonstrate how to "Using Matrix create scaled bitmap". In this exercise, we will create animation using matrix.

Create Animation using Matrix


package com.example.androidmatrixanimation;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Matrix;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.Transformation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

Button myButton;
ImageView myImage1, myImage2;

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

myButton = (Button)findViewById(R.id.mybutton);
myImage1 = (ImageView)findViewById(R.id.myimage1);
myImage2 = (ImageView)findViewById(R.id.myimage2);

myButton.setOnClickListener(myOnClickListener);
myImage1.setOnClickListener(myOnClickListener);
myImage2.setOnClickListener(myOnClickListener);

}

OnClickListener myOnClickListener
= new OnClickListener(){

@Override
public void onClick(View v) {
applyAnimation(v);

}
};

private void applyAnimation(View v){
MyAnimation myAnimation = new MyAnimation();
myAnimation.setDuration(5000);
myAnimation.setFillAfter(true);
myAnimation.setInterpolator(new OvershootInterpolator());

v.startAnimation(myAnimation);
}

public class MyAnimation extends Animation{

@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
super.applyTransformation(interpolatedTime, t);

final Matrix matrix = t.getMatrix();
matrix.setScale(interpolatedTime, interpolatedTime);
}
}

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/mybutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:id="@+id/myimage1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/myimage2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@drawable/ic_launcher" />

</LinearLayout>




download filesDownload the files.

Download the APK.

0 comments:

Post a Comment

 
Toggle Footer