I am making an application on Android which generates one of the two available images on clicking a button and after a duration of 1 second, that image is supposed to fade away, giving the user a choice to click the button again.
The problem is that the animation works smoothly on the first button press (i.e. generates an image and then fades away), however on the second button press, the image just sits there and nothing happens. I can't figure out why.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView firstimage = (ImageView)findViewById(R.id.imageView2);
final ImageView secondimage = (ImageView)findViewById(R.id.imageView1);
final Button clickMe = (Button)findViewById(R.id.button);
final TextView image_description = (TextView)findViewById(R.id.textView);
image_description.setText("");
final Animation fadeout = new AlphaAnimation(1,0);
fadeout.setStartOffset(1000);
fadeout.setDuration(1000);
secondimage.setAnimation(fadeout);
firstimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
secondimage.setVisibility(View.GONE);
firstimage.setVisibility(View.GONE);
image_description.setVisibility(View.GONE);
clickMe.setVisibility(View.VISIBLE);
fadeout.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
System.out.println("Animation block");
secondimage.setVisibility(View.GONE);
firstimage.setVisibility(View.GONE);
image_description.setVisibility(View.GONE);
clickMe.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
clickMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("Click block");
Random r = new Random();
int i = r.nextInt(2);
clickMe.setVisibility(View.GONE);
if(i == 0) {
secondimage.setVisibility(View.VISIBLE);
image_description.setText("LOOK it's a CAT");
image_description.setVisibility(View.VISIBLE);
secondimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
} else {
firstimage.setVisibility(View.VISIBLE);
image_description.setText("LOOK it's a DOG");
image_description.setVisibility(View.VISIBLE);
firstimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
}
}
});
}
The Logs look something like this:
Click Block
Animation Block
Click Block
Click Block
Click Block
Click Block
Any idea why the code is not reaching the Animation block on 2nd click onwards?
Alright. I was able to solve my own query.
I replaced
secondimage.setAnimation(fadeout);
with
secondimage.startAnimation(fadeout);
On doing so, the code was able to reach the onAnimationEnd function.
The easiest thing to do is create/inflate a new instance of the Animation type object for each view that needs animation. As you have it now, it's trying to reuse the same object.
Related
I am new and studying online . This is my first animation study at Android. The problem is that when I choose the answer my Card View animation is suppose to show immediately. But it didn't show as soon as I click . But I found out that after I click the button, I need to press Home button and resume it from the background apps to start the animation.The animation starts only after I do this process. Toast is showing without a problem. After I choose the answer I always need to do like that to show my animation. The app is about True or false.
Also the coding is exactly the same as my online tutorial. maybe my phone problem ?? I am using Samsung S 7 edge.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView QuestionTextView,QcountTextView;
private Button Tbutton,Fbutton;
private ImageButton prev,next;
private int count = 0;
private CardView cardView;
private Animation shake;
public Questions Qarray[] = new Questions[]{
new Questions(R.string.Q1, true),
new Questions(R.string.Q2, false),
new Questions(R.string.Q3, true),
new Questions(R.string.Q4, true),
new Questions(R.string.Q5, false),
new Questions(R.string.Q6, true),
new Questions(R.string.Q7, false)
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tbutton = findViewById(R.id.tb);
Fbutton = findViewById(R.id.fb);
QuestionTextView = findViewById(R.id.questionshow);
QcountTextView = findViewById(R.id.Qcount);
next = findViewById(R.id.nb);
prev = findViewById(R.id.pb);
cardView = findViewById(R.id.cardView);
shake = AnimationUtils.loadAnimation(MainActivity.this,R.anim.shakeanimation);
Tbutton.setOnClickListener(this);
Fbutton.setOnClickListener(this);
next.setOnClickListener(this);
prev.setOnClickListener(this);
QuestionTextView.setText(Qarray[count].question);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.tb:
giveanswer(true);
pagechange();
break;
case R.id.fb:
giveanswer(false);
pagechange();
break;
case R.id.pb:
if(count != 0) {
count = (count - 1);
pagechange();
}
break;
case R.id.nb:
count = (count + 1) % Qarray.length;
pagechange();
break;
}
}
private void pagechange() {
QuestionTextView.setText(Qarray[count].getQuestion());
QcountTextView.setText((count+1) + " out of " + Qarray.length);
}
private void giveanswer(boolean b) {
boolean correctanswer = Qarray[count].ans;
if(b == correctanswer){
fadeView();
Toast.makeText(this,R.string.yes,Toast.LENGTH_SHORT).show();
}
else{
ShakeAnimation();
Toast.makeText(this,R.string.no,Toast.LENGTH_SHORT).show();
}
}
private void fadeView(){
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(350);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
cardView.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new
Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
cardView.setCardBackgroundColor(Color.GREEN);
}
#Override
public void onAnimationEnd(Animation animation){
cardView.setCardBackgroundColor(Color.rgb(41,226,205));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
private void ShakeAnimation(){
cardView.setAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
cardView.setCardBackgroundColor(Color.RED);
}
#Override
public void onAnimationEnd(Animation animation) {
cardView.setCardBackgroundColor(Color.rgb(41,226,205));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
I expect the animation show as soon as I choose the answer.
you need to call alphaAnimation.start(); or shake.start(); just having animation listeners is not gonna start your animation when you want it to start.
try adding these lines exactly where you want the animations to run.
For Example:
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(350);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
cardView.setAnimation(alphaAnimation);
alphaAnimation.start();
Also setAnimation should to be used with xml Animations if I'm not mistaking
I have three Images. When I first touch, first Image is shown, when I second touch, second Image is shown, and then when I third touch, third Image is shown. After all, when I fourth touch, I want to show first Image return and so on.
Can someone point me to show how handling or touching on a ImageView of android?
The below should do what you need:
public class MainActivity extends Activity {
ImageView image;
int i=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
image = (ImageView) findViewById(R.id.imageViewName);
setButtonOnClickListeners();
}
}
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (i==1){
image.setImageResource(R.drawable.image1);
i++;
}
else if(i==2){
image.setImageResource(R.drawable.image2);
i++;
}
else if(i==3){
image.setImageResource(R.drawable.image3);
i++;
}
else if(i==4){
image.setImageResource(R.drawable.image4);
i=1;
}
}
});
I've not tested but the idea is correct. When you click the image it will change the drawable depending on the value of i. When you get image 1 i will equal 1. Onclick will increment i until i==4 which it will reset to 1 as you requested.
A while loop might be tidier but this was the quickest soultion I thought of.
how to handle click on an ImageView
You can simply set a View.OnClickListener for your ImageView:
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do stuff
}
});
I am trying to make a splash screen which has got 2 images .1st one is at center and moves up, 2nd one comes in when 1st goes up completely.
I have done the animation for the 1st image but the second one is not working in the desired manner. Actually I am not quite sure do i use imageswitcher or any other function. Please let me know what shall i do.
I would share the codes here.
public class Splash_screen extends Activity { // SPLASH ACTIVITY
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
ImageView kfcpic = (ImageView) findViewById(R.id.kfclogo);
ImageView sogood= (ImageView)findViewById(R.id.sogood);
TranslateAnimation animation = new TranslateAnimation(0,0,0,-500); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(3000); // animation duration
//animation.setRepeatCount(0); // animation repeat count
animation.setRepeatMode(1);
animation.setFillAfter(true);
// repeat animation (left to right, right to left )
//animation.setFillAfter(true);
kfcpic.startAnimation(animation); // start animation
Thread logoTimer = new Thread() {
public void run() {
try {
sleep(5000);
runOnUiThread(new Runnable() {
public void run() {
Intent mainIntent = new Intent(Splash_screen.this, MainActivity.class);
startActivity(mainIntent);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
finish();
}
}
};
logoTimer.start();
}
}
Maybe you didn't set the visibility of sogood ImageView correctly. It would be better if you could provide the layout of this SplashActivity.
I have a class where I include another layout on a button click. This included layout has some buttons and a code which executes on clicking these buttons. I have used a counter which indicates the number of times the button is clicked. First time clicking on the button includes the layout and the second time clicking removes the views and so on. Here's the code
public class Home extends Fragment implements OnClickListener {
int c = 0;
Button bmain, bnew, bolder;
RelativeLayout r1;
View rootView;
Animation slidedown, slideup;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home, container, false);
bmain = (Button) rootView.findViewById(R.id.btn2);
bmain.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View arg0) {
ViewGroup con = null;
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout flContainer = (FrameLayout)rootView.findViewById(R.id.flContainer);
//Loading animation
slidedown = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down);
slideup = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);
//The counter indicates the number of clicks.
//Needs to be replaced for a better solution.
//If it's even add view
if(c%2==0)
{
//Adding layout here
flContainer.addView(layoutInflater.inflate(R.layout.test1,con,false ));
//Starting Animation
flContainer.startAnimation(slidedown);
//After adding layout we can find the Id of the included layout and proceed from there
bnew = (Button) rootView.findViewById(R.id.btntest);
bnew.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity(), "You Clicked New", Toast.LENGTH_LONG).show();
}
});
bolder = (Button) rootView.findViewById(R.id.btntest1);
bolder.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
Intent form = new Intent(getActivity(),FeedbackForm.class);
startActivity(form);
}
});
c++;
} //If ends here
//If it's odd remove view
else
{
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
//flContainer.removeView(flContainer);
//flContainer.removeView(layoutInflater.inflate(R.layout.test1, con, false));
c++;
}
}
}
The code at the end
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
removes the view but fails to process the animation. I have tried using removeView but in that case the buttonclicks in the if statement fail to execute the second time. What am I missing here? How can I achieve it?
The answer is pretty simple. You have to remove the view after the animation is finished. This can be achieved pretty simple, first you have to set an animation listener for your animation and in the onAnimationEnd callback - which is called when the animation is finished - you remove the views.
EDIT:
Replace this:
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
With this:
slideup.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
flContainer.removeAllViews();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
flContainer.startAnimation(slideup);
If there are any further problems let me know.
I creating a "Add" button by using sdk, so i wan it to move it to left when i press that "Add" button, and come back to original place when i press again. The problem i have is the button only can go to the left when press, and cannot come back to the original place after i trying to press it again. Does it i having some problem on the if...else function there? any help would appreciate.
public class TestImage2 extends Activity {
/** Called when the activity is first created. */
ImageView image_plus;
ImageView image_subtract;
ImageView image_multiply;
ImageView image_div;
String op;
boolean pergi = false;
final Animation animation = new TranslateAnimation(0,100,0,0);
final Animation animation2 = new TranslateAnimation(100,0,0,0);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image_plus = (ImageView)findViewById(R.id.test_image);
image_subtract = (ImageView)findViewById(R.id.test_image2);
image_multiply = (ImageView)findViewById(R.id.test_image3);
image_div = (ImageView)findViewById(R.id.test_image4);
image_plus.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
if(pergi == false)
{
animation.setDuration(1000);
image_plus.startAnimation(animation);
pergi= true;
animation.setFillAfter(true);
}
else
{
animation2.setDuration(1000);
image_plus.startAnimation(animation2);
pergi = false;
animation2.setFillAfter(true);
}
}});
i think that there is something wrong in
final Animation animation2 = new TranslateAnimation(100,0,0,0);
it should be
final Animation animation2 = new TranslateAnimation(0,-100,0,0);
:)