On 12 Sty, 19:40, Ansh <[email protected]> wrote:
> Hi guys
>
> I am new to animation in android.I am trying to translate childitem/ of a
> listview.First child view should translate from middle to top of the list
> and the list should also slide down during the translate animation and make
> the space of the view being translated.Please help me guys as it has eaten
> up my mind for past 3days.
>
> i am trying to achieve exactly the same in this
> video.http://youtu.be/xPLhfEJuz4k
here you have sample code to start with:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class AnimatedListActivity extends Activity implements
OnItemClickListener, OnClickListener, Runnable {
private final static String TAG = "AnimatedListActivity";
private ArrayAdapter<String> mAdapter;
private int mInsertPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lv);
ListView lv = (ListView) findViewById(R.id.lv);
lv.setClipChildren(false);
lv.setOnItemClickListener(this);
lv.setDivider(null);
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
mAdapter.add("10");
mAdapter.add("20");
mAdapter.add("30");
mAdapter.add("40");
mAdapter.add("50");
mAdapter.add("60");
mAdapter.add("70");
mAdapter.add("80");
mAdapter.add("90");
lv.setAdapter(mAdapter);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setPositiveButton(android.R.string.ok, this);
b.setTitle("Enter Integer");
EditText et = new EditText(this);
et.setId(99);
b.setView(et);
b.show();
}
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog d = (AlertDialog) dialog;
EditText et = (EditText) d.findViewById(99);
String newItem = et.getText().toString();
ListView lv = (ListView) findViewById(R.id.lv);
int cnt = mAdapter.getCount();
for (int i = 0; i < cnt; i++) {
String item = mAdapter.getItem(i);
if (newItem.compareTo(item) < 0) {
mAdapter.insert(newItem, i);
mInsertPosition = i;
lv.setSelection(i);
lv.post(this);
break;
}
}
}
@Override
public void run() {
ListView lv = (ListView) findViewById(R.id.lv);
// here animations start
int first = lv.getFirstVisiblePosition();
int last = lv.getLastVisiblePosition();
for (int k = 0; k < last - first + 1; k++) {
View child = lv.getChildAt(k);
int pos = lv.getPositionForView(child);
Animation animation = null;
if (pos == mInsertPosition) {
// instead of alpha animation you can make your
// new inserted item move here
animation = new AlphaAnimation(0, 1);
} else
if (pos > mInsertPosition) {
animation = new TranslateAnimation(
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0,
Animation.RELATIVE_TO_SELF, -1,
Animation.RELATIVE_TO_SELF, 0);
}
if (animation != null) {
animation.setDuration(750);
child.startAnimation(animation);
}
}
}
}
pskink
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en