简体   繁体   English

在ImageView中图像未更改

[英]Image is not changing in ImageView

I want to change image after some animation to first image. 我想将一些动画后的图像更改为第一张图像。 I am trying with this code but only the second image is getting animated. 我正在尝试使用此代码,但是只有第二张图像正在动画化。 First image is not displaying. 第一张图像未显示。

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);      
        anim(); 


    }



    @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;
    }

   public void anim() {
        Animation animfadein =AnimationUtils.loadAnimation(this,R.anim.fadein);
        Animation animfadeout =AnimationUtils.loadAnimation(this,R.anim.fadeout);
        findViewById(R.id.imageView1).startAnimation(animfadein);
        findViewById(R.id.imageView1).startAnimation(animfadeout);
        Context myActivity =getApplicationContext();
        Toast.makeText(myActivity, "Please wait", Toast.LENGTH_SHORT).show();
        button();
       }

    public void button() {
        ImageView imgView1 = (ImageView)findViewById(R.id.imageView1);          
        Drawable myDrawable = getResources().getDrawable(R.drawable.start);
        imgView1.setImageDrawable(myDrawable);
    }

}    

My xml file is like this 我的xml文件是这样的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <ImageView

        android:id="@+id/imageView1"
        android:contentDescription="@string/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:src="@drawable/ic_logo"/>



</LinearLayout>

I tried using setImageresource still same first image is not displaying. 我尝试使用setImageresource仍未显示第一张图片。 Please let me know what mistake i have done.Thanks in advance :) 请让我知道我犯了什么错误。

You will need to use animation listener for achieving this: 您将需要使用动画侦听器来实现此目的:

animFadeIn.setAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        //start animFadeOut Here
    }
});

Hope this helps. 希望这可以帮助。

You are running everything in onCreate which is done before the user is even able to see any of the UI. 您正在onCreate中运行所有操作,这是在用户甚至无法看到任何UI之前完成的。 It is until onStart that the user will see the UI. 直到onStart,用户才能看到UI。 even so, the change will be almost immediate, so you might want to wait a few secs. 即使这样,更改几乎是立即完成的,因此您可能需要等待几秒钟。 Try this: 尝试这个:

@Override
protected void onStart() {
    super.onStart();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(5000);
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        anim();
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

In general you are making a thread wait 5 secs then do the animation. 通常,您要让一个线程等待5秒钟,然后执行动画。 I am not going to go over what's the best way to do this, but you certainly need to modify this on the UI thread, so I included it in the snippet. 我将不讨论执行此操作的最佳方法,但是您肯定需要在UI线程上进行修改,因此我将其包含在代码段中。
For more information about threading go here: 有关线程的更多信息,请转到此处:
http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM