Random Layout Animation
This example shows how you can animate contents of a layout and show them randomly.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it RandomLayoutAnimation.
2.) Create an anim folder into res directory and create and write following into res/anim/fade.xml:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_longAnimTime" />
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_longAnimTime" />
3.) Create and write following into res/anim/ layout_random_fade.xml:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="random"
android:animation="@anim/fade" />
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="random"
android:animation="@anim/fade" />
4.) Write following into your main.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/grid"
android:layoutAnimation="@anim/layout_random_fade"
<GridView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/grid"
android:layoutAnimation="@anim/layout_random_fade"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:layout_height="fill_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center" />
5.) Run for output.
Steps:
1.) Create a project named RandomLayoutAnimation and set the information as stated in the image.
1.) Create a project named RandomLayoutAnimation and set the information as stated in the image.
Build Target: Android 2.3.3
Application Name: RandomLayoutAnimation
Package Name: com.example. RandomLayoutAnimation
Activity Name: RandomLayoutAnimation
Min SDK Version: 10
Application Name: RandomLayoutAnimation
Package Name: com.example. RandomLayoutAnimation
Activity Name: RandomLayoutAnimation
Min SDK Version: 10
2.) Open RandomLayoutAnimation.java file and write following code there:
package com.example.RandomLayoutAnimation;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class RandomLayoutAnimation extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadApps();
setContentView(R.layout.main);
GridView grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(new AppsAdapter());
GridView grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(new AppsAdapter());
}
private List<ResolveInfo> mApps;
private void loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
}
}
public class AppsAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(RandomLayoutAnimation.this);
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(RandomLayoutAnimation.this);
ResolveInfo info = mApps.get(position % mApps.size());
i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
final int w = (int) (36 * getResources().getDisplayMetrics().density+ 0.5f);
i.setLayoutParams(new GridView.LayoutParams(w, w));
return i;
}
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
final int w = (int) (36 * getResources().getDisplayMetrics().density+ 0.5f);
i.setLayoutParams(new GridView.LayoutParams(w, w));
return i;
}
public final long getItemId(int position) {
return position;
}
}
}
return position;
}
}
}
3.) Compile and build the project.
No comments:
Post a Comment