I'm trying to crossfade two ImageViews by invoking a method when the first image is clicked, then we fade into the second image(alpha set to 0 initially), then I would like to fade back into the first image after clicking on the second image.
It works when crossfading from one image to the other using only one method, but if when I add the other method to crossfade back to the previous image, nothing happens when I click on the image.
public class MainActivity extends AppCompatActivity {
public void narutoFade(View view){
ImageView naruto =(ImageView) findViewById(R.id.naruto);
ImageView narutosage =(ImageView) findViewById(R.id.narutosage);
naruto.animate().alpha(0f).setDuration(2000);
narutosage.animate().alpha(1f).setDuration(2000);
}
public void narutoSageFade(View view) {
ImageView naruto2 = (ImageView) findViewById(R.id.naruto);
ImageView narutosage2 = (ImageView) findViewById(R.id.narutosage);
narutosage2.animate().alpha(0f).setDuration(2000);
naruto2.animate().alpha(1f).setDuration(2000);
}
}
Maybe you should think about declaring both as Fields, declare them only once in onCreate()/onResume(), and write one crossfade method that serves both :
public class MainActivity extends AppCompatActivity {
private ImageView naruto, narutosage;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
naruto =(ImageView) findViewById(R.id.naruto);
narutosage =(ImageView) findViewById(R.id.narutosage);
}
public void crossfade(View fadeIn, View fadeOut) {
fadeIn.animate().alpha(1).setDuration(2000);
fadeOut.animate().alpha(0).setDuration(2000);
}
}
and in your Button onClicks you just call :
crossfade(naruto, narutosage);
or
crossfade(narutosage, naruto);
Related
I'm new at androidstudio and want to compare a imageView by the following:
I have 2 imageView, both are using a drawable i named "blank" at the start of the app, using if/else I want to chance those images to another drawable i have, i tried the following:
private ImageView equipament1;
private ImageView equipament2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
public void sentImg() {
if (equipament1.equals(R.drawable.blank)){
equipament1.setImageResource(R.drawable.reactor);
}
else if (equipament2.equals(R.drawable.blank)){
equipament2.setImageResource(R.drawable.reactor);
} else {finish();}
but it doesn't work, the app just replaces the first image and if i click on the button again, nothing happens (this if/else is inside a button).
I want to check if the first image is blank, if it is, the app should replace the blank image with the image "reactor" or, if is not blank, the app should move to the second blank image, and replace it and this go on for more 2 blank spaces.
I'm doing this because I'm building an program similar to LucidChart where you put your equipments in the app.
The problem is that the second time you have already changed the value of the comparator.
If the objective is just to change the images you don't need the if/else.
private ImageView equipament1;
private ImageView equipament2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
public void sentImg() {
equipament1.setImageResource(R.drawable.reactor);
equipament2.setImageResource(R.drawable.reactor);
}
When the user clicks your button, you want to do 2 things. You want to show some images, or you want to call finish().
I would suggest using a boolean as a flag the the state and compare that instead of comparing the ImageView itself. This'll be easier, and make your code easier to read.
I created a flag called firstClick that is set to true by default. When the user clicks your button (button1 in this example), we check against that and show the images. Then we set it to false,
so the next click will call finish().
private ImageView equipament1;
private ImageView equipament2;
// The current state of the Activity
private boolean firstClick = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
// Setting your OnClickListener
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if( firstClick ) {
firstClick = false;
sentImg();
} else {
finish();
}
}
});
}
public void sentImg() {
equipament1.setImageResource(R.drawable.reactor);
equipament2.setImageResource(R.drawable.reactor);
}
public class MainActivity extends AppCompatActivity {
public void fade(View view) {
ImageView homerImageView = (ImageView) findViewById(R.id.homerImageView);
ImageView bartImageView = (ImageView) findViewById(R.id.bartImageView) ;
bartImageView.animate().alpha(1).setDuration(5000);
homerImageView.animate().alpha(0).setDuration(5000);
for ()
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
In my app have totally 2 images upon click one fade out to 0 alpha and second comes back from 0 alpha to 1 alpha when I run the app and click on the image change to other but the only thing I want that it keeps doing that upon each click on the image like if I click on image 1 it changes to image 2 but upon clicking on image to it do not change to image 1 any solution very simple will be helpfull thanks.
Try like this. Just a hint may not suits what exactly you want
In your image onClick method:
switch (v.getId()){
case R.id.id_one:
imgOne.animate().alpha(0).setDuration(5000);
callHandler(imgTwo);
break;
case R.id.id_two:
imgTwo.animate().alpha(0).setDuration(5000);
callHandler(imgOne);
break;
}
Handler function is like,
private void callHandler(final ImageView imgView) {
new Handler(Looper.myLooper()).postDelayed(new Runnable() {
#Override
public void run() {
imgOne.setVisibility(View.GONE);
imgTwo.setVisibility(View.GONE);
imgView.setVisibility(View.VISIBLE);
imgView.animate().alpha(1).setDuration(5000);
}
},5000);
}
What the title says. At first, I created a method to swap from one image to the other one, and it works. When I add the 2nd method though, to reverse the action and swap back to the first one, nothing changes. It does not even revert from the first image to the second one. Below is the code:
public void fade(View view) {
ImageView laxus = (ImageView) findViewById(R.id.laxus_shirt);
ImageView laxos2 = (ImageView) findViewById(R.id.laxus2);
laxus.animate().alpha(0f).setDuration(1000);
laxos2.animate().alpha(1f).setDuration(1000);
}
public void fadetoblack(View view) {
ImageView laxusius = (ImageView) findViewById(R.id.laxus_shirt);
ImageView laxosios2 = (ImageView) findViewById(R.id.laxus2);
laxosios2.animate().alpha(0f).setDuration(1000);
laxusius.animate().alpha(1f).setDuration(1000);
}
Thank you in advance.
You need to call start() to trigger the animation.
laxus.animate().alpha(0f).setDuration(1000).start();
laxos2.animate().alpha(1f).setDuration(1000).start();
I would suggest you to use Util method to perform crossfading. Something like below
public static void crossFade(View incomingView, View outGoingView, int outGoingViewVisibility) {
outGoingView.setAlpha(1);
ViewCompat.animate(outGoingView).alpha(0).setListener(new ViewPropertyAnimatorListener() {
#Override
public void onAnimationStart(View view) {
}
#Override
public void onAnimationEnd(View view) {
view.setVisibility(outGoingViewVisibility);
}
#Override
public void onAnimationCancel(View view) {
}
}).start();
incomingView.setVisibility(View.VISIBLE);
incomingView.setAlpha(0);
ViewCompat.animate(incomingView).setListener(null).alpha(1).start();
}
Maybe it will be an option to use ViewSwitcher.
Add a viewSwitcher with 2 ImageViews.
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/laxus"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="#+id/laxus2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ViewSwitcher>`
On Activity onCreate add code
ViewSwitcher viewSwitcher=(ViewSwitcher)findViewById(R.id. switcher); // initiate a ViewSwitcher
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); // load in animation
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); // load out animation
viewSwitcher.setInAnimation(in); // set in Animation for ViewSwitcher
viewSwitcher.setOutAnimation(out); // set out Animation for ViewSwitcher
When you want to switch between ImageViews call method viewSwitcher.showNext(); or viewSwitcher.showPrevious();
To verify which view is displayed use viewSwitcher.getCurrentView();
Here's the thing:
I have two classes: Main and control.java
the Main class is an Activity class where I build my app and the control class I just use for variables controls that I must access from other classes.
The problem is: In class Main I got 2 methods, and I have an ImageView in each of them, I need to set the image view resource of the second method on a click listener of the first one. Like this:
public void first() {
final ImageView first = (ImageView) findViewById(R.id.myview);
first.setBackgroundResource(R.drawable.myimage);
}
public void second() {
final ImageView second = (ImageView) findViewById(R.id.myview2);
//And then, I want something like this: first.setBackgroundResource(first);
}
Thanks guys!
Why don't you just do something like:
public void second() {
final ImageView second = (ImageView) findViewById(R.id.myview2);
final ImageView first = (ImageView) findViewById(R.id.myview);
first.setBackgroundResource(R.drawable.myimage);
}
Otherwise, you'd have to use a private/public class variable and define it outside the method.
I admit to being a bit confused by your question but I think below is at least the start of what you're looking for
#Override
protected void onCreate(Bundle savedInstanceState) {
control.setImageView((ImageView) findViewById(R.id.myview));
second = (ImageView) findViewById(R.id.myview2);
}
public void first() {
control.getImageView().setBackgroundResource(R.drawable.myimage);
}
public void second() {
second.setBackgroundDrawable(control.getImageView().getDrawable());
}
ImageView second;
In looking at the ApiDemos example in the Android (1.5) SDK, there is a fine example of using an ImageSwitcher, with a Gallery object to provide the "change image" actions.
The application I'm looking to write, starting into Android development, has three images that I want to be able to pane/scroll through, so ImageSwitcher looks like a fine solution. However, I don't [necesarily] want to have thumbnails in a gallery. I want either a swipe action, and/or a button, to cause a scroll to the previous/next image in the set.
The example ImageSwitcher in ApiDemos uses a Gallery, and without that Gallery, doesn't do anything.
If someone has a suggestion of a way to bind some sort of button controller, or a U/I swipe object, I would appreciate the pointer.
Sorry to ask such a newbie level question.
Thanks.
You can use it like this:
public class GalleryActivity extends Activity implements ViewFactory{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
iSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
iSwitcher.setFactory(this);
iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
iSwitcher.setImageDrawable(fetchImage(mImageURLS[0]));
iSwitcher.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// CLICK HANDLER
// Change image like:
// iSwitcher.setImageDrawable(fetchImage(mImageURLS[1]));
}
});
}
#Override
public View makeView() {
ImageView iView = new ImageView(this);
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
iView.setLayoutParams(new
ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
iView.setBackgroundColor(0xFF000000);
return iView;
}
}
fetchImage(mImageURLS[0]) returns Drawable object