在我的应用程序中,我希望能够从网站(ddragon)加载大量图像 问题是,每次我想要加载图像时,它需要很多时间和设备的电源,保存这些照片将占用很多地方,这也是一个问题。
我想知道处理大尺寸图像(500+)的最佳方法是什么(高度和宽度为200~300 dp),谢谢!
(目前我正在使用通用图像加载器)
imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(q.getImageURL(), questionImage, null, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
loadingView.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
finish();
Toast.makeText(getApplicationContext(), "Failed loading the image...\nID: " + q.getID(), Toast.LENGTH_LONG).show();
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
loadingView.setVisibility(View.INVISIBLE);
myCountDownTimer.start();
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
答案 0 :(得分:1)
我建议使用第三方库,而不是重新发明轮子。 Square有一个名为Picasso的图书馆很受欢迎。
您可以执行以下操作:
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)
Glide也很受欢迎。
答案 1 :(得分:0)
<强> MainActivity.java 强>
import java.io.InputStream;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
// Set your Image URL into a string
String URL = "Image URL for example http://www.google.com";
ImageView image;
Button button;
ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from image.xml
setContentView(R.layout.activity_main);
// Locate the ImageView in activity_main.xml
image = (ImageView) findViewById(R.id.image);
// Locate the Button in activity_main.xml
button = (Button) findViewById(R.id.button);
// Capture button click
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Execute DownloadImage AsyncTask
new DownloadImage().execute(URL);
}
});
}
// DownloadImage AsyncTask
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Download Image Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Bitmap doInBackground(String... URL) {
String imageURL = URL[0];
Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
// Set the bitmap into ImageView
image.setImageBitmap(result);
// Close progressdialog
mProgressDialog.dismiss();
}
}
}
<强> activity_main.xml中 强>
<RelativeLayout 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" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ImageView>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/button" />
</RelativeLayout>
和清单中的互联网权限
<uses-permission android:name="android.permission.INTERNET" >