this is the java code to rotate an image when click on button. Image rotate it's perfect but if I click button again when rotation is not ended the animation restart itself, not end the animation. How can I wait the end of the animation? I found Animation.AnimationListener, I think that onAnimationEnd works great for me, but I'm not be able to integrate it in my code... please help me :-)
package com.example.helloword;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class Rotation_test extends Activity {
private float statdegree = (float) 0.0;
private float enddegree = (float) 90.0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rotation_test);
// ---------------------------------------------------------------------------------------------
Button buttonRotateCenter = (Button) findViewById(R.id.rotatecenter);
final ImageView floatingImage = (ImageView) findViewById(R.id.floatingimage);
// AnimationRotation
final Animation animationRotateCenter = new RotateAnimation(statdegree,
enddegree, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animationRotateCenter.setDuration(5000L);
animationRotateCenter
.setInterpolator(new AccelerateDecelerateInterpolator());
// \AnimationRotation
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
floatingImage.startAnimation(animationRotateCenter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_rotation_test, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
You have to take a boolean variable which tracks whether animation is in progress or not. then use this variable in animation listener and button click as below code
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
if(!anim_in_progress)
floatingImage.startAnimation(animationRotateCenter);
}
});
animationRotateCenter.setAnimationListener(new anim_listener());
}
boolean anim_in_progress=false;
class anim_listener implements AnimationListener
{
#Override
public void onAnimationEnd(Animation arg0) {
anim_in_progress=false;
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
anim_in_progress=true;
}
}
Never tried this, but might work
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
if(!floatingImage.hasTransientState())
floatingImage.startAnimation(animationRotateCenter);
}
});
Related
Sir, we are developing an app to teach small children write alphabets.. Here we show a screen with two images where one image is on the right side it is just to show the image of the alphabet, and the the other one is a light image where the user can draw on image for practise. I want to validate the image drawn by the user whether he has drawn correctly on the base image or not.!
(Note: I exactly dont know what is the use of "Scaling" which i used in it.)Can i please get a solution because i tried a lot but cudnt get an answer
The code is:
enter code here
package com.example.androidhive;
import com.example.androidhive.ColorPickerDialog.OnColorSelectedListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.support.v4.view.MotionEventCompat;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class FullImgTwoActivity extends Activity implements OnTouchListener {
int pp;
int pickupLinesItemIndex;
int position;
PaintView paintView;
ViewFlipper layout;
int size = 0;
int progress = 0;
ImageView imageView1;
private Bitmap bmp;
private Bitmap operation;
GetScaling scaling;
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
paintView = new PaintView(this);
scaling=new GetScaling(this);
layout = (ViewFlipper) findViewById(R.id.view1);
Intent i = getIntent();
position = i.getExtras().getInt("id");
final ImageAdapterTwo imageAdapterTwo = new ImageAdapterTwo(this);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setImageResource(imageAdapterTwo.mThumbIdsx[position]);
bmp=imageView1.getDrawingCache();
layout.setBackgroundResource(imageAdapterTwo.mThumbIdsy[position]);
paintView.setLayoutParams(new LinearLayout.LayoutParams(500, 500));
layout.addView(paintView);
ImageView back = (ImageView) findViewById(R.id.imageView3);
back.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (pickupLinesItemIndex < imageAdapterTwo.mThumbIdsw.length) {
if (position <= 0) {
Toast.makeText(getApplicationContext(), "This is the starting alphabet", 90)
.show();
} else {
imageView1
.setImageResource(imageAdapterTwo.mThumbIdsx[--position]);
layout
.setBackgroundResource(imageAdapterTwo.mThumbIdsy[position]);
}
}
}
});
ImageView nexta = (ImageView) findViewById(R.id.imageView4);
nexta.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (pickupLinesItemIndex > imageAdapterTwo.mThumbIdsw.length) {
//if (position <=imageAdapter.mThumbIds.length) {
Toast.makeText(getApplicationContext(), "This level is done", 90)
.show();
} else {
imageView1
.setImageResource(imageAdapterTwo.mThumbIdsx[++position]);
layout
.setBackgroundResource(imageAdapterTwo.mThumbIdsy[position]);
//}
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.select_color_menu:
showColorPickerDialogDemo();
break;
case R.id.select_clear_menu:
paintView.clear();
paintView.invalidate();
break;
case R.id.pointers_select_menu:
// pointerDialog();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void pointerDialog() {
// TODO Auto-generated method stub
final Dialog d = new Dialog(FullImgTwoActivity.this);
d.setTitle("Increase pointer size");
d.setContentView(R.layout.pointerssize);
SeekBar bar = (SeekBar) d.findViewById(R.id.seekBar1);
Button save = (Button) d.findViewById(R.id.save);
Button cancel = (Button) d.findViewById(R.id.cancel);
bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
progress = progresValue;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Display the value in textview
size = seekBar.getMax();
}
});
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
paintView.setSize(progress);
d.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.dismiss();
}
});
d.show();
}
private void showColorPickerDialogDemo() {
int initialColor = Color.WHITE;
ColorPickerDialog colorpick = new ColorPickerDialog(
FullImgTwoActivity.this, initialColor,
new OnColorSelectedListener() {
#Override
public void onColorSelected(int color) {
paintView.setColo(color);
}
});
colorpick.show();
}
public void Checkimage(View v) {
scaling.checktheimage(imageView1);
scaling.checkpaintimage(paintView);
boolean paint=scaling.bmapaint;
boolean image=scaling.bmapimage;
if(paint!=image){
AlertDialog.Builder al=new AlertDialog.Builder(FullImgTwoActivity.this);
al.setTitle("Please Try Again");
al.setIcon(R.drawable.checkno);
al.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
paintView.clear();
paintView.invalidate();
}
}) ;
al.setNegativeButton("NO",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent in=new Intent(FullImgTwoActivity.this, GridLayoutTwo.class);
startActivity(in);
finish();
}
});
AlertDialog dialog=al.create();
dialog.show();
}else {
AlertDialog.Builder al=new AlertDialog.Builder(FullImgTwoActivity.this);
al.setTitle("Please Go To Next Letter");
al.setIcon(R.drawable.checkno);
al.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent in=new Intent(FullImgTwoActivity.this, GridLayoutTwo.class);
startActivity(in);
finish();
}
}) ;
al.setNegativeButton("NO",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
paintView.clear();
paintView.invalidate();
}
});
AlertDialog dialog=al.create();
dialog.show();
}
}
//#Override
public boolean onCreateOptionsMenu1(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_full_img_two, menu);
return true;
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}
My scale/zoom animation won't start with the finish(). I hope there's a way to do it without deleting the said statement because it is necessary not to go back to the current activity.
Here's the code:
package com.capstone.mainmobidyx.filipino;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import com.capstone.mainmobidyx.R;
public class F_FilipinoYunit1 extends Activity implements OnClickListener {
Button btnLesson1, btnLesson2, btnLesson3;
Intent lesson1, lesson2, lesson3;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.f_yunit1);
btnLesson1 = (Button) findViewById(R.id.btnY1Lesson1);
btnLesson1.setOnClickListener(this);
btnLesson2 = (Button) findViewById(R.id.btnY1Lesson2);
btnLesson2.setOnClickListener(this);
btnLesson3 = (Button) findViewById(R.id.btnY1Lesson3);
btnLesson3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Animation zoomAnim = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
v.startAnimation(zoomAnim);
switch (v.getId()) {
case R.id.btnY1Lesson1:
lesson1 = new Intent(v.getContext(), SX_Lesson01.class);
startActivity(lesson1);
finish();
break;
case R.id.btnY1Lesson2:
lesson2 = new Intent(v.getContext(),
SX_ScienceLesson02Menu.class);
startActivity(lesson2);
finish();
break;
case R.id.btnY1Lesson3:
lesson2 = new Intent(v.getContext(),
SX_Lesson03Menu.class);
startActivity(lesson2);
finish();
break;
}
}
}
SOLUTION:
Using the method onAnimation end. I can now start the animation before finishing the activity by inserting the codes inside the method.
here is the working code:
#Override
public void onClick(final View v) {
// TODO Auto-generated method stub
final Animation zoomAnim = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
zoomAnim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation anim) {
switch (v.getId()) {
case R.id.btnY1Lesson1:
lesson1 = new Intent(v.getContext(), SX_Lesson01.class);
startActivity(lesson1);
finish();
break;
case R.id.btnY1Lesson2:
lesson2 = new Intent(v.getContext(),
SX_ScienceLesson02Menu.class);
startActivity(lesson2);
finish();
break;
case R.id.btnY1Lesson3:
lesson2 = new Intent(v.getContext(),
SX_Lesson03Menu.class);
startActivity(lesson2);
finish();
break;
}
}
}
public void onAnimationRepeat(Animation arg0) {}
public void onAnimationStart(Animation arg0) {}
});
v.startAnimation(zoomAnim);
Set an animation end listener on the animation and finish() / startActivity() when the animation has ended :
http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
example :
final Animation animation = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation anim) {
// Start your new activity and finish() the current activity here!
}
public void onAnimationRepeat(Animation arg0) {}
public void onAnimationStart(Animation arg0) {}
}
animation.startAnimation()
Consider:
package com.example.practicealpha;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
Button next,previous;
ImageView image;
Integer[] id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
id[0] = R.drawable.aa;
id[1] = R.drawable.bb;
id[2] = R.drawable.cc;
id[3] = R.drawable.dd;
next=(Button)findViewById(R.id.buttonNext);
previous=(Button)findViewById(R.id.buttonPrevious);
image=(ImageView)findViewById(R.id.imageView1);
// image.setImageDrawable(id[0]);
// image.setImageResource(id[i]);
/*
next.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
if (i<=3)
{
i++;
image.setImageResource(id[i]);
if(i==4)
next.setEnabled(false);
}
}
});
previous.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
if(i>=1)
{
i--;
image.setImageResource(id[i]);
if(i==0)
next.setEnabled(false);
}
}
});
*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
id[0] = R.drawable.aa;
This line is causing my app to stop forcefully. How can I fix this problem?
I am trying to intialise an integer array with images id and then trying to go through the image listed in drawable folder.
Change
Integer[] id;
to
int[] id;
You have to initialize the ids for the size:
int[] id = new int[size];
You cannot use the id array directly. Initialise it first:
id[0] = R.drawable.aa;
i am working on android app
in which i have an animated image.
my code is
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth()/2;
left = new TranslateAnimation(0, hight, width, hight);
left1= new TranslateAnimation( 480, 10, 0, 10);
left.setDuration(2000);
left.setAnimationListener(this);
b1 =(ImageView)findViewById( R.id.balloon);
b1.setOnClickListener(this);
b1.startAnimation(left);
#Override
public void onClick(View v) {
Toast.makeText(this, "Clicked", 27).show();
}
using this code i am able to animate ballon or my picture but i the onclick lisnter only works when animation is completed i want onclicklistner should work during animation how to do this.
sorry for bad english
Override onAnimationStart and onAnimationEnd functions in your AnimationListener. Keep a variable to check whether animation is playing when the image clicked.
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView imageView;
private boolean animationPlaying;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TranslateAnimation animation = new TranslateAnimation( 480, 10, 0, 10);
animation.setDuration(2000);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
animationPlaying = true;
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
animationPlaying = false;
}
});
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.startAnimation(animation);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(animationPlaying) {
Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_SHORT).show();
} else {
Log.d("ANIMATION", "click missed because animation was not playing");
}
}
});
}
}
sorry for this again button i ant figure out the correct placement here. i just want it on this class only to exit the app when back pressed but i cant figure out what exactly to write or where to write it. thanks for the help. code of class below.
package com.example.whattodo2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class Title extends Activity {
Button reset, rts;
ImageView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title);
reset = (Button) findViewById(R.id.reset);
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
double rand = Math.random();
if(rand < 0.5){
Intent reset1 = new Intent(Title.this, MainActivity.class);
startActivity(reset1);
} else {
Intent reset2 = new Intent(Title.this, Question36.class);
startActivity(reset2);
}
}
});
rts = (Button) findViewById(R.id.rts);
rts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent rts=new Intent(Title.this,Rts.class);
startActivity(rts);
}
});
final Animation a = AnimationUtils.loadAnimation(this, R.animator.animation);
a.reset();
final ImageView rImage = (ImageView) findViewById(R.id.title);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
layout.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
rImage.startAnimation(a);
func(); //A call to the function.
}
});
}
protected void func() {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.title, menu);
return true;
}
}
Use like this:
public class Title extends Activity {
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
super.onBackPressed();
}
}
hope this will give you some solution.
Before onCreate in all the activities:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent intent=new Intent (Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// finish();
}
return true;
}