Spinner在合适的时间没有变得可见?

时间:2017-10-29 17:16:10

标签: android multithreading android-asynctask

最初微调器是不可见的。当按下按钮时,调用downloadImage()方法。但是spinner.setVisibility(View.VISIBLE);在执行imageView.setImageBitmap(resultImage);之前不会使微调器可见。

  

,即在下载图像并设置为图像视图后执行spinner.setVisibility(View.VISIBLE);

public class MainActivity extends AppCompatActivity {
ImageView imageView;
Bitmap resultImage;
Bitmap bitmap;
ProgressBar spinner;

public void downloadImage(View view){

    ImageDownloader imageDownloader = new ImageDownloader();
    spinner.setVisibility(View.VISIBLE);
    try {
        resultImage = imageDownloader.execute("https://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png").get();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    imageView.setImageBitmap(resultImage);
    //spinner.setVisibility(View.INVISIBLE);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView)findViewById(R.id.imageView);
    spinner = (ProgressBar) findViewById(R.id.spinner);


}
public class ImageDownloader extends AsyncTask<String,Void,Bitmap>{

    @Override
    protected Bitmap doInBackground(String... params) {
        try {
            URL url = new URL(params[0]);
            HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;

    }
}
}

1 个答案:

答案 0 :(得分:2)

您的问题与在var gulp = require("gulp"); var rename = require("gulp-rename"); var path = require("path"); var sass = require("gulp-sass"); gulp.task('modules-sass', function () { // using .scss extensions for sass files return gulp.src(`development/**/*.scss`) .pipe(sass()) .pipe(rename(function (file) { // file.dirname before any changes console.log("file.dirname 1 = " + file.dirname); // this removes the last directory var temp = path.dirname(file.dirname); console.log(" temp = " + temp); // now add 'Css' to the end of the directory path file.dirname = path.join(temp, 'Css'); console.log(" after = " + file.dirname); })) .pipe(gulp.dest('development')); }); // this is the directory structure I assumed // gulpfile.js is just above the 'development' directory // development / Admin / Sass1 / file1.scss // development / Admin / Sass1 / file2.scss // development / Admin / Sass2 / file3.scss // development / Admin / Sass2 / file4.scss // development / Admin / Css // development / Public / Sass1 / file5.scss // development / Public / Sass1 / file6.scss // development / Public / Sass2 / file7.scss // development / Public / Sass1 / file8.scss // development / Public / Css 方法中调用导致进程等待的get()有关,这正是您不想要的。

当你调用downloadImage时,它会返回一个AsyncTask,然后你打开execute("http....."),然后等待结果可用。在onClick监听器完成之前,视图不会更新,这是在下载完成之后。

See the doc for get here

将您的get()方法更改为此。

downloadImage

然后将您的视图操作移动到ImageDownloader前/后执行方法。     public class ImageDownloader扩展了AsyncTask {

public void downloadImage(View view){
    ImageDownloader imageDownloader = new ImageDownloader();
    imageDownloader.execute("https://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png");
}