gpt4 book ai didi

java - 通过创建不同的对象来使用相同的 AsyncTask

转载 作者:行者123 更新时间:2023-12-02 11:01:57 26 4
gpt4 key购买 nike

我正在制作一个小图像,下载 Android 应用程序作为一个项目。它获取两个图像的 URL,下载它们并将它们显示在 ImageView 中。目前,我正在使用两个单独的 ImageDownloader,即 ImageDownloader1 和 ImageDownloader2,它们都扩展 AsyncTask 并实例化它们的对象,然后下载图像。这两个类都在不同的 ImageView 上执行相同的工作。我希望通过只创建一个像 ImageDownloader 这样的类,然后创建两个实例来为每个 ImageView 下载图像来完成此操作?有什么办法可以做到这一点,请建议我吗?

Java 代码:

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 库,您可以执行以下操作

Picasso.with(context).load(url).centerCrop().into(imageView)

关于java - 通过创建不同的对象来使用相同的 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51265624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com