【问题标题】:About loading animations in Android关于在 Android 中加载动画
【发布时间】:2015-10-28 17:26:07
【问题描述】:

我正在使用图像选择意图从用户的图库中获取图像,并在压缩后将其放入 ImageView 中。

在我的设备上大约需要 2 秒,但在其他设备上很可能需要更多或更少的时间。我想知道如何设置一个加载动画,该动画将持续在 ImageView 中加载图像所需的时间(所以不是预设的持续时间)?

这是我处理图像的代码。一切正常,我只是不知道如何为此使用加载动画:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        File file = new File(picturePath);
        picturePath = file.getAbsolutePath();
        Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
        Bitmap bitmapRotated = rotateBitmap(bitmap, picturePath);
        if(bitmapRotated != null)
            bitmap = bitmapRotated;
        imageToSave = Bitmap.createScaledBitmap(bitmap, image.getWidth(), image.getHeight(), false);
        image.setImageBitmap(imageToSave);
        thumbnailToSave = image.getThumbnail();
        space.setVisibility(View.GONE);
        edit.setVisibility(View.VISIBLE);
        save.setVisibility(View.GONE);
        image.setEditing(false);
        imageChanged = true;
        thumbnailChanged = true;
        image.invalidate();
    }
}

【问题讨论】:

  • 在这种情况下使用progressDialog document
  • @behrooz 工作正常,谢谢!不知道这个。

标签: android animation load


【解决方案1】:

您应该使用 Android 进度条。 您可以创建一个条形图来表示操作要走多远,或者只是一个旋转的轮子。 您需要为您的任务创建一个线程,并为更新进度条创建另一个线程。

public class MyAndroidAppActivity extends Activity {

Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();

private long fileSize = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

}

public void addListenerOnButton() {

    btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
    btnStartProgress.setOnClickListener(
             new OnClickListener() {

       @Override
       public void onClick(View v) {

        // prepare for a progress bar dialog
        progressBar = new ProgressDialog(v.getContext());
        progressBar.setCancelable(true);
        progressBar.setMessage("File downloading ...");
        progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        progressBar.show();

        //reset progress bar status
        progressBarStatus = 0;

        //reset filesize
        fileSize = 0;

        new Thread(new Runnable() {
          public void run() {
            while (progressBarStatus < 100) {

              // process some tasks
              progressBarStatus = doSomeTasks();

              // your computer is too fast, sleep 1 second
              try {
                Thread.sleep(1000);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }

              // Update the progress bar
              progressBarHandler.post(new Runnable() {
                public void run() {
                  progressBar.setProgress(progressBarStatus);
                }
              });
            }

            // ok, file is downloaded,
            if (progressBarStatus >= 100) {

                // sleep 2 seconds, so that you can see the 100%
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // close the progress bar dialog
                progressBar.dismiss();
            }
          }
           }).start();

           }

            });

    }

// file download simulator... a really simple
public int doSomeTasks() {

    while (fileSize <= 1000000) {

        fileSize++;

        if (fileSize == 100000) {
            return 10;
        } else if (fileSize == 200000) {
            return 20;
        } else if (fileSize == 300000) {
            return 30;
        }
        // ...add your own

    }

    return 100;

   }
  }

【讨论】:

  • 对于我需要的东西来说似乎有点不必要的复杂,但它确实有效。然而,我用一个旋转的圆圈给自己做了一个解决方案。
【解决方案2】:

这是我使用相同代码的 ProgressDialog 解决方案。

@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        final ProgressDialog progress =  new ProgressDialog(this);
        progress.setMessage(getResources().getString(R.string.loading));
        progress.show();
        Thread mThread = new Thread() {
            @Override
            public void run() {
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                File file = new File(picturePath);
                picturePath = file.getAbsolutePath();
                Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
                Bitmap bitmapRotated = rotateBitmap(bitmap, picturePath);
                if(bitmapRotated != null)
                    bitmap = bitmapRotated;
                imageToSave = Bitmap.createScaledBitmap(bitmap, image.getWidth(), image.getHeight(), false);
                thumbnailToSave = image.getThumbnail();
                imageChanged = true;
                thumbnailChanged = true;
                PictureActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        image.setEditing(false);
                        space.setVisibility(View.GONE);
                        edit.setVisibility(View.VISIBLE);
                        save.setVisibility(View.GONE);
                        image.setImageBitmap(imageToSave);
                        image.invalidate();
                    }
                });

                progress.dismiss();
            }
        };
        mThread.start();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 2012-02-19
    • 2011-02-18
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多