I want to create an animate img, so when I click a button I can see the animation.
in my project, I've just create buttons and images. In the drawable folder, I put the three png images and a frame_animation.xml, I set the images and the duration. In the java file, I put this code:
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setBackgroundResource(R.drawable.frame_animation);
myFrameAnimation=(AnimationDrawable) img.getBackground();
}
});
but when I run the app, the animation doesn't work (I can see only one image, not all the three image)
You have to also START the animation, setting the resource is not enough.
In your case simply add myFrameAnimation.start();
Full example:
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setBackgroundResource(R.drawable.frame_animation);
myFrameAnimation=(AnimationDrawable) img.getBackground();
myFrameAnimation.start();
}
});
Seems like you just left it out by accident, since you already got the AnimationDrawable from your resource.
Related
If I want to change a image to another by the click of a button and then back to the previous image again by the click of the same button on imageview in android studio how to do that in short and easiest way? As I am new to it I am not familiar with all the functions of imageview.
For example:-
I wrote this code to do what I needed after a lot of failure in finding a easier way.
int i=0;
public void change(View v){
int img[] = {R.drawable.cat1,R.drawable.cat2};
ImageView cat = findViewById(R.id.imageView2);
if(i==0)
{cat.setImageResource(img[1]);
i=1;}
else {cat.setImageResource(img[0]);
i=0;}
}
Before I was trying to do something like this:-
public void change(View v){
ImageView cat = findViewById(R.id.imageView2);
if(cat.getDrawable()==R.drawable.cat2;)
{cat.setImageResource(R.drawable.cat1);}
else
{cat.setImageResource(R.drawable.cat1};
}
But it kept giving error that they have different type and I also tried some other functions named getId() but it didnt work either...
So my main objective is, is there a function through which I can campare the resource of image view directly with the image in drawable folder and how to implement it in if else or some other conditional statement?
The first approach should work, but the i value seems not tightly coupled to the ImageView. So, instead you can set a tag to the ImageView that equals to the current drawable id:
Initial tag:
ImageView cat = findViewById(R.id.imageView2);
cat.setImageResource(R.drawable.cat1);
cat.setTag(R.drawable.cat1);
And click listener callback:
public void change(View v){
ImageView cat = findViewById(R.id.imageView2);
int tag = (int) cat.getTag();
if(tag == R.drawable.cat2){
cat.setImageResource(R.drawable.cat1);
cat.setTag(R.drawable.cat1);
} else {
cat.setImageResource(R.drawable.cat2);
cat.setTag(R.drawable.cat2);
}
}
You could try StateListDrawable, LevelListDrawable, with each state/level, it will change image depend on your state/level
I want to create a button animation with two image on an ImageView programmiticaly only and by using onClickListener. I have two image that i can set with setImageResource() fonction but i don't know how to play the button animation before the button action launches.
Please, could you explain a bit more what you are wanting? Do you want to replace the ImageButton resource when you click on it? You can achieve it setting the OnClickListener
new View.OnClickListener() {
public void onClick(View v) {
imageButtonView.setImageResource(idOfTheNewResource);
}
};
Is this what you are wanting?
I'm new to Android and I'm trying to change the content of an ImageView with a button and if I press the button again the image changes back. I thought it would be easy with an if-else statement but I have been looking around in the ImageView API and I don't see the method that allows me to get the image that is being displayed in that moment... Any ideas?
Here is my code so far...
boton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imagen.setImageResource(R.mipmap.imag1);
}
});
I didn't copy the rest of the code because I dont think it's necessary
You can get the image of the button with using this method:
Bitmap bitmap = ((BitmapDrawable)imagen.getDrawable()).getBitmap();
To set image of a button with another bitmap you can use:
imagen.setImageBitmap(bitmap);
I'm new to andorid, and I'm trying to do a simple thing:
when button A is clicked, I want to replace the image displayed
on ImageButton B.
I've tried all sort of things like:
msortByButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Test change image
mdefineLocationButton.setBackgroundResource(0);
mdefineLocationButton.invalidate();
mdefineLocationButton.setBackgroundResource(R.drawable.notdefined);
mdefineLocationButton.invalidate();
mdefineLocationButton.refreshDrawableState();
}
});
But it seems that the new image is painted, but on top of it the old image
is painted also. (I can see the old image, below it I can see edges of the new image).
Any idea how to do this right?
Thanks,
Omer
Use setImageResource (int resId)
(http://developer.android.com/reference/android/widget/ImageView.html#setImageResource(int))
ImageButtons can have a background and an actual image src. My guess is initially you set the image src and now in code you are setting the background. So both show.
#Override
public void onClick(View v) {
mdefineLocationButton.setImageResource(R.drawable.notdefined);
}
I am using sequence for animation in my project. I have set onClickListener to that image where I put animation sequences. now when first time I run the code, and click on image, it start run animation correctly but then it never run again on click. I have to again run the code. So how I can run animation sequences more than one time? I have put my codebelow to start animation sequence.
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
animation_door = (AnimationDrawable) image.getBackground();
animation_door.start();
} });
Well, before clicking your button again you have to stop the animation also, so you can try it like this,
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
animation_door = (AnimationDrawable) image.getBackground();
animation_door.stop();
animation_door.start();
}
});
Is your animation set as a oneShot? It's most likely in the end state and you just need to hint it back to the start state.
AnimationDrawable door = (AnimationDrawable) image.getBackground();
door.setOneShot(false);
door.start();
Keep in mind this will most likely lead to a looping animation, which you probably don't want. You could consider using an Animation Set instead since you'll have a bit more control over the animation itself.