我正在制作一个小图像下载Android应用程序作为一个项目。它获取两个图像的URL,下载它们并在ImageViews中显示它们。目前,我正在使用两种单独的ImageDownloaders
即ImageDownloader1
和ImageDownloader2
扩展AsyncTask并实例化其对象,然后下载图像。这两个类都在执行相同的工作,但使用不同的 ImageView。我希望通过只创建一个类(如ImageDownloader
),然后创建两个实例来为每个 ImageView 下载图像来完成此操作?有什么办法可以这样做,那么请建议我?
爪哇代码:
public class MainActivity extends AppCompatActivity {
Button image1Button,image2Button,resetButton;
ImageView upperImageIV,lowerImageIV;
ProgressBar upperImagePB,lowerImagePB;
ImageDownloader1 image1;
ImageDownloader2 image2;
TextView textView1,textView2;
boolean status1,status2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1Button = (Button) findViewById(R.id.button1);
image2Button = (Button) findViewById(R.id.button2);
resetButton = (Button) findViewById(R.id.button3);
upperImageIV = (ImageView) findViewById(R.id.imageView1);
lowerImageIV = (ImageView) findViewById(R.id.imageView2);
upperImagePB = (ProgressBar) findViewById(R.id.pbarUpperIMG);
lowerImagePB = (ProgressBar) findViewById(R.id.pbarLLowerIMG);
status1 = false;
status2 = false;
textView1 = (TextView) findViewById(R.id.editText1);
textView2 = (TextView) findViewById(R.id.editText2);
}
public void downloadImage1(View view){
upperImageIV.setVisibility(View.GONE);
String url;
url = textView1.getText().toString();
if(!url.isEmpty()) {
status1 = true;
image1 = new ImageDownloader1();
upperImagePB.setVisibility(View.VISIBLE);
image1.execute(url);
}
else if(url.isEmpty()){
upperImageIV.setVisibility(View.GONE);
}
}
public class ImageDownloader1 extends AsyncTask<String,Void,Bitmap>{
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
System.out.println("Content Type = "+connection.getContentType());
if(connection.getContentType().contains("image")){
InputStream inputStream = connection.getInputStream();
return BitmapFactory.decodeStream(inputStream);
}
}
catch (Exception e) {
e.printStackTrace();
}
while(image1.cancel(true) == false ){
if (isCancelled())
break;
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap){
super.onPostExecute(bitmap);
try {
upperImageIV.setImageBitmap(image1.get());
upperImagePB.setVisibility(View.GONE);
upperImageIV.setVisibility(View.VISIBLE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void downloadImage2(View view){
lowerImageIV.setVisibility(View.GONE);
String url;
url = textView2.getText().toString();
if(!url.isEmpty()) {
status2 = true;
image2 = new ImageDownloader2();
lowerImagePB.setVisibility(View.VISIBLE);
image2.execute(url);
}
else if(url.isEmpty()){
lowerImageIV.setVisibility(View.GONE);
}
}
public class ImageDownloader2 extends AsyncTask<String,Void,Bitmap>{
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
if(connection.getContentType().contains("image")){
InputStream inputStream = connection.getInputStream();
return BitmapFactory.decodeStream(inputStream);
}
}
catch (Exception e) {
e.printStackTrace();
}
while(image2.cancel(true) == false ){
if (isCancelled())
break;
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap){
super.onPostExecute(bitmap);
try {
lowerImageIV.setImageBitmap(image2.get());
lowerImagePB.setVisibility(View.GONE);
lowerImageIV.setVisibility(View.VISIBLE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void reset(View view){
System.out.println("Reset Button pressed");
upperImageIV.setVisibility(View.GONE);
upperImagePB.setVisibility(View.GONE);
lowerImageIV.setVisibility(View.GONE);
lowerImagePB.setVisibility(View.GONE);
if(status1 == true && status2 == false){
image1.cancel(true);
}
else if(status1 == false && status2 == true){
image2.cancel(true);
}
else if(status1 == true && status2 == true){
image1.cancel(true);
image2.cancel(true);
}
else {
}
}
}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context="com.example.syeddanish.downloadingimages.MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="URL here"
android:textSize="24sp"/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="URL here"
android:textSize="24sp"
android:layout_below="@id/editText1"/>
<LinearLayout
android:id="@+id/buttonsLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/editText2">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="downloadImage1"
android:text="Image 1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="downloadImage2"
android:text="Image 2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="reset"
android:text="Reset" />
</LinearLayout>
<LinearLayout
android:id="@+id/imagesLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/buttonsLinearLayout"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:id="@+id/upperIMGRL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.5">
<ProgressBar
android:id="@+id/pbarUpperIMG"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_centerInParent="true"
android:visibility="gone" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/lowerIMGRL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.5">
<ProgressBar
android:id="@+id/pbarLLowerIMG"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_centerInParent="true"
android:visibility="gone" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
使用毕加索图书馆,您可以执行以下操作
Picasso.with(context).load(url).centerCrop().into(imageView)
public void downloadImage1(View view){
String url = textView1.getText().toString();
if(!url.isEmpty()) {
ImageDownloader imageDownloader1 = new ImageDownloader(upperImageIV, upperImagePB);
imageDownloader1.execute(url);
} else if(url.isEmpty()) {
upperImageIV.setVisibility(View.GONE);
}
}
public void downloadImage2(View view){
String url = textView2.getText().toString();
if(!url.isEmpty()) {
ImageDownloader imageDownloader2 = new ImageDownloader(lowerImageIV, lowerImagePB);
imageDownloader2.execute(url);
} else if(url.isEmpty()) {
lowerImageIV.setVisibility(View.GONE);
}
}
你可以像这样使用AsyncTask。
public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
ImageView upperImageIV;
ProgressBar upperImagePB;
ImageDownloader(ImageView upperImageIV, ProgressBar upperImagePB) {
this.upperImageIV = upperImageIV;
this.upperImagePB = upperImagePB;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
upperImageIV.setVisibility(View.GONE);
upperImagePB.setVisibility(View.VISIBLE);
}
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
System.out.println("Content Type = " + connection.getContentType());
if (connection.getContentType().contains("image")) {
InputStream inputStream = connection.getInputStream();
return BitmapFactory.decodeStream(inputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
try {
upperImageIV.setImageBitmap(bitmap);
upperImagePB.setVisibility(View.GONE);
upperImageIV.setVisibility(View.VISIBLE);
} catch (Exception e) {
e.printStackTrace();
}
}
}