从imageview回到主屏幕android studio

时间:2016-02-25 09:33:25

标签: java android-studio android-imageview

我有一个在imageView中显示随机图像并出现问题的方法。 随机图像显示正常,但当我按回手机返回主屏幕时,应用关闭!如果我在图像上向左或向右滑动,奇怪的是它会显示阵列中的其他图像!我想要的是当用户看完图像后我想退出imageView并看到主屏幕再次显示!谢谢



import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private ImageView thumbPrint;
    private TextView result;
    private AnimationDrawable thumbAnimation;
    private String[] moodResults;
    private Runnable mRunnable;
    private ImageView resultImageView;

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

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        thumbPrint = (ImageView)findViewById(R.id.thumbPrint);
        thumbPrint.setBackgroundResource(R.drawable.thumb_animation);
        thumbAnimation = (AnimationDrawable)thumbPrint.getBackground();

        result = (TextView)findViewById(R.id.resultText);
        resultImageView = (ImageView)findViewById(R.id.resultImageView);

        thumbPrint.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                thumbAnimation.start();
                showImage();
                return true;
            }
        });

    }

    public void showImage() {
        mRunnable = new Runnable() {
            final int[] imageIds= {R.drawable.one, R.drawable.two, R.drawable.three}; // This is your array with resource id of each image
            Random r = new Random();
            Handler mHandler = new Handler();
            @Override
            public void run() {
                int rand = (int)(Math.random()* moodResults.length);
                result.setText(moodResults[rand]);
                int randomInt = r.nextInt(imageIds.length);
                resultImageView.setBackgroundResource(imageIds[randomInt]); 
                
                //stop animation
                thumbAnimation.stop();
            }
        };

        //Here handler is called the first time; the code in mRunnable will execute after 5 seconds
        Handler mHandler = new Handler();
        mHandler.postDelayed(mRunnable, 5000);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
    }
}




1 个答案:

答案 0 :(得分:1)

据我所见,当你不需要时隐藏结果视图元素时,它可以工作。 换句话说:尝试重置您使用showImage();所做的所有更改 我不在家测试,sry。

@Override
public void onBackPressed(){
   if(resultImageView.getVisibility == View.VISIBLE){
      resultImageView.setVisibility(View.INVISIBLE); //or GONE 
      result.setVisibility(View.INVISIBLE);
   }else{
      finish();
   }
}

@Override
public void run() {
   [...]
   resultImageView.setVisibility(View.VISIBLE);
   result.setVisibility(View.VISIBLE);
   [...]
}