我使用图像拾取意图从用户的图库中获取图像,并在压缩后将其放入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();
}
}
答案 0 :(得分:0)
您应该使用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;
}
}
答案 1 :(得分:0)
以下是我使用相同代码的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();
}
}