I am begineer in Android and i want to use "anim" folder xml file, that has to be executed from particular x,y coordinates.. i.e. x,y coordinates values get from event.getX()and event.getY() methods. How to start animation from getx(),gety() methods, anyone please help me to do this. Thanks in advance.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (AbsoluteLayout) findViewById(R.id.container);
mainLayout.setOnTouchListener(this);
letterView = (ImageView) findViewById(R.id.letterView);
letterView.setOnTouchListener(this);
letterView = (ImageView) findViewById(R.id.letterView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean eventConsumed = true;
int x = (int)event.getX();
int y = (int)event.getY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
if (v == letterView) {
eventConsumed = false;
}
setAbsoluteLocationCentered(letterView, x, y);
animSequential = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.flyingbird);
animSequential.setAnimationListener(this);
letterView.startAnimation(animSequential);
}
return eventConsumed;
}
private void setSameAbsoluteLocation(View v1, View v2) {
AbsoluteLayout.LayoutParams alp2 = (AbsoluteLayout.LayoutParams) v2.getLayoutParams();
setAbsoluteLocation(v1, alp2.x, alp2.y);
}
private void setAbsoluteLocationCentered(View v, int x, int y) {
setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
}
private void setAbsoluteLocation(View v, int x, int y) {
AbsoluteLayout.LayoutParams alp = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
alp.x = x;
alp.y = y;
v.setLayoutParams(alp);
}
@Override
public void onAnimationEnd(Animation animation) {
if (animation == animSequential) {
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
"anim folder xml file is as follows,
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://ift.tt/nIICcg"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<translate
android:duration="800"
android:fillAfter="false"
android:fromYDelta="80%p"
android:startOffset="300"
android:toXDelta="75%p" />
</set>
Main activity Xml file as follows,
<AbsoluteLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
tools:context="com.xample.puzzle_grid.MainActivity"
tools:ignore="MergeRootFrame" >
<ImageView
android:id="@+id/letterView"
android:layout_width="wrap_content"
android:layout_height="91dp"
android:layout_x="180dp"
android:layout_y="63dp"
android:background="@drawable/puppy" />
</AbsoluteLayout>
No comments:
Post a Comment