How to create a fade in ImageView to change it to other image (i.e. from Image 1 to Image 2) when imageView is clicked and vica versa
I have created another click method but it is not working because i think as I have already put image-2 aplha to 0.
public void fade(View view)
{
ImageView mickey=findViewById(R.id.mickey);
ImageView mouse = findViewById(R.id.imageView2);
mickey.animate().alpha(0f).setDuration(2000);
mouse.animate().alpha(1f).setDuration(2000);
}
Here is a simple method to play around with: :)
boolean firstImage = false;
private void fadeFlip(View view) {
AlphaAnimation aa = new AlphaAnimation(1f,0f);
aa.setDuration(1*1000);
view.startAnimation(aa);
aa.start();
if(firstImage) {
view.setBackgroundResource(R.drawable.ic_launcher);
} else {
view.setBackgroundResource(R.drawable.image_2);
}
AlphaAnimation bb = new AlphaAnimation(0f,1f);
bb.setDuration(1*1000);
view.startAnimation(bb);
bb.start();
firstImage = !firstImage;
}
Related
I am creating a ticTacToe app, and when I click on some ImageView, I set resource of that ImageView to a specific resource(X image). Now, the problem is I want to set "O" image to some other random ImageView,
public void imageViewClicked(View view) {
ImageView counter = (ImageView) view;
counter.setImageResource(R.drawable.x);
}
Keep IDs of all avaliable ImageViews in single collection in your activity:
private List<Integer> images = new ArrayList<>();
onCreate() {
images.add(R.id.image1);
images.add(R.id.image2);
//..
}
When user clicks on some ImageView, remove it from the mentioned collection, then select random view from the rest and set resource:
onClick(View view) {
images.remove(view.getId());
int rnd = new Random().nextInt(images.size() - 1);
int id = images.get(rnd);
findViewById(id).setImageResource(R.drawable.o);
images.remove(rnd);
}
Hope it helps.
You can storage your drawable resource in a variable and manage it by turn (after each play).
First turn mydrawableResource = R.drawable.x.
Second turn mydrawableResource = R.drawable.o.
Then you set:
public void imageViewClicked(View view) {
ImageView counter = (ImageView) view;
counter.setImageResource(mydrawableResource );
}
When I click a button it changes my ImageView from pic1 to pic2, I use this...
ImageView myImageView = (ImageView) findViewById(R.id.myImageView );
myImageView.setImageResource(R.drawable.pic2);
and it changes my ImageView to pic2 now I want to be able to click the button again and change it back to pic 1 using...
myImageView.setImageResource(R.drawable.pic1);
but I need some way to make a getImageResource so I can run an if statement on which pic is showing and display the other when the button is clicked. For Example if pic 2 is showing it will check which pic is showing and returns pic2 so it knows when the button is clicked to switch it to pic1
Set a flag to identify which image is set.
By default pic1 is selected, add the flag
boolean flag = true;
Then on imageview click listener check for the flag, if the flag is set then set the imageview with pic2 else pic2.
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(flag) {
imageView.setImageResource(R.drawable.pic2);
} else {
imageView.setImageResource(R.drawable.pic1);
}
flag = !flag;
}
});
Try Below code and see if it works for you:
Write this before onCreate: private boolean imageIs = false;
Write this in onCreate:
btnImageChange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!imageIs) {
imageV.setImageResource(R.mipmap.ic_launcher);
imageIs = true;
} else {
imageV.setImageResource(R.mipmap.ic_launcher_round);
imageIs = false;
}
}
});
You can add a variable, which will have a value when you click the button like
int x= R.drawable.pic2;
And when you click the button for changing the image to pic2 your code will be like
ImageView myImageView = (ImageView) findViewById(R.id.myImageView );
myImageView.setImageResource(R.drawable.pic2);
x=R.drawable.pic2;
And if you want to add pic1 again by clicking the button you can use if loop like
ImageView myImageView = (ImageView) findViewById(R.id.myImageView );
if(x=R.drawable.pic2){
myImageView.setImageResource(R.drawable.pic1);
}
else if(x=R.drawable.pic1){
myImageView.setImageResource(R.drawable.pic2);
x=R.drawable.pic2;
}
else{
myImageView.setImageResource(R.drawable.pic1);
}
I want to have a favorite button on my app like in Gmail
So, when the user click on the star, the star became yellow, and when the user clicked it again, it turn back to normal
How can i make this happen with my custom image?
i have two images
when its not favorited (heart-grey.png)
and when its favorited (heart-red.png)
You can use visibilty to do this.You have to define the xml to have both images at the same postion(It can be done using Relative layout).
final ImageView play = (ImageView) findViewById(R.id.play);
final ImageView play2 = (ImageView) findViewById(R.id.play2);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Drawable playImage = play.getDrawable();
if (playImage.isVisible()){
play.setVisibility(View.GONE);
play2.setVisibility(View.VISIBLE);
mediaPlayer.start(); }
}
});
play2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable playImage2 = play2.getDrawable();
if (playImage2.isVisible()){
play2.setVisibility(View.GONE);
play.setVisibility(View.VISIBLE);
mediaPlayer.pause();
}
If your star is an ImageButton you can do something like this :
starSelected.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//add a condition to detect if it is already a favorite or not
if(starSelected.getDrawable() == R.drawable.theimage2) {
starSelected.setImageResource(R.drawable.thenewimage);
}else{
starSelected.setImageResource(R.drawable.thenewimage2);
}
}
});
Hope it helps
In your java code create a boolean flag:
boolean isSelected = false
Set an onClickListener inside your onCreate for the star (ImageView, Button whatever it is). Inside onClick, check for the flag like this:
if (isSelected) {
// change image src to unselected
isSelected = false;
} else {
// change image src to selected
isSelected = true;
}
and also you can save the boolean state with SharedPreferences to make sure you get the correct state every time.
How can I change Image of an Imageview?
I want to get the image associated; if it is img1 I want set the image to img2, if it is img2 I want to set the image to img2.
first set the tag of imageview in xml to 1
final ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (Integer.parseInt(imageview.getTag().toString()) == 1) {
imageview.setBackgroundResource(R.drawable.image2);
imageview.setTag(2);
} else {
imageview.setBackgroundResource(R.drawable.image1);
imageview.setTag(1);
}
}
});
I think you are looking for ImageView.setTag() and ImageView.getTag().It checks if the image is associated with the Image view as #ρяσѕρєя K mentioned in comment.
There are two version of setTag one which takes object as an argument and other takes key and object as an argument
I created an ImageView and in my activity every few seconds i change the picture to a different one. I also have a button than when press, is supposed to get me the resource id of the picture currently being displayed, the problem is that everytime i do:
ImageView view = (ImageView) findViewById(R.id.imageView1);
It always gives me the resourceId defined in the xml, not the one i changed dynamically, how can i achieve this? here's the code in my activity class:
public void startImages(View v) {
...
delayedImage(0);
}
private void delayedImage(final int index) {
//stop at the 5th picture
if (index > 5) return;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
String drawableId = "picture" +index;
int resourceId = getResources().getIdentifier(drawableId, "drawable", getPackageName());
ImageView view = (ImageView) findViewById(R.id.imageView1);
view.setImageResource(resourceId);
delayedImage(index+1);
}
}, 500);
}
So every 5 second a new image is set, when i click on the button Share I want to retrieve whatever resourceId is currently in the ImageView set in the method above.
//send image to send text
public void shareImage(View v){
ImageView view = (ImageView) findViewById(R.id.imageView1);
??????
}
I checked the ImageView object while debugging and I saw someone get the int by doing this:
Field f = ImageView.class.getDeclaredField("mResource");
f.setAccessible(true);
Object out = f.get(view);
But i'm unable to get the actual R drawable name from this which I need when i'm trying to retrieve it from
String path = "android.resource://your.package.name/" +getPackageName() +"/" +???;
.. can anyone please help?