在处理多个图像时如何使ImageView更干净? ImageView数组?

时间:2018-05-22 10:06:57

标签: android imageview jsoup

我使用jsoup和ImageView从URL显示20个图像。代码有效,但我想就如何使这些重复变量变得更加清晰提出第二意见。

package edu.learner.yaarpatandar.test;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    final StringBuilder albumArtIMG = new StringBuilder();
    int pageNum = 1;
    private Button nextPage;
    private Button backPage;

    private ImageView albumArt;
    private ImageView albumArt2;
    private ImageView albumArt3;
    private ImageView albumArt4;
    private ImageView albumArt5;

    private ImageView albumArt6;
    private ImageView albumArt7;
    private ImageView albumArt8;
    private ImageView albumArt9;
    private ImageView albumArt10;

    private ImageView albumArt11;
    private ImageView albumArt12;
    private ImageView albumArt13;
    private ImageView albumArt14;
    private ImageView albumArt15;

    private ImageView albumArt16;
    private ImageView albumArt17;
    private ImageView albumArt18;
    private ImageView albumArt19;
    private ImageView albumArt20;

    //String webURL = "";
    private String[] imgURL = new String[20];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        albumArt   = (ImageView) findViewById(R.id.imageView);
        albumArt2  = (ImageView) findViewById(R.id.imageView2);
        albumArt3  = (ImageView) findViewById(R.id.imageView3);
        albumArt4  = (ImageView) findViewById(R.id.imageView4);
        albumArt5  = (ImageView) findViewById(R.id.imageView5);
        albumArt6  = (ImageView) findViewById(R.id.imageView6);
        albumArt7  = (ImageView) findViewById(R.id.imageView7);
        albumArt8  = (ImageView) findViewById(R.id.imageView8);
        albumArt9  = (ImageView) findViewById(R.id.imageView9);
        albumArt10 = (ImageView) findViewById(R.id.imageView10);
        albumArt11 = (ImageView) findViewById(R.id.imageView11);
        albumArt12 = (ImageView) findViewById(R.id.imageView12);
        albumArt13 = (ImageView) findViewById(R.id.imageView13);
        albumArt14 = (ImageView) findViewById(R.id.imageView14);
        albumArt15 = (ImageView) findViewById(R.id.imageView15);
        albumArt16 = (ImageView) findViewById(R.id.imageView16);
        albumArt17 = (ImageView) findViewById(R.id.imageView17);
        albumArt18 = (ImageView) findViewById(R.id.imageView18);
        albumArt19 = (ImageView) findViewById(R.id.imageView19);
        albumArt20 = (ImageView) findViewById(R.id.imageView20);

        nextPage = (Button) findViewById(R.id.nextPage_button);
        backPage = (Button) findViewById(R.id.backPage_button);
        checkPage();
        getWebsite();

        nextPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pageNum++;
                checkPage();
                getWebsite();
            }
        });

        backPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pageNum--;
                checkPage();
                getWebsite();
            }
        });
    }

    private void getWebsite() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document doc = Jsoup.connect(SOME_URL + Integer.toString(pageNum)).get();
                    Elements imgLink = doc.select("img");
                    for(int i = 0; i < 20; i++) {
                        imgURL[i] = imgLink.get(i+1).attr("src");
                    }

                } catch (IOException e) {
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int width = 265, height = 265;

                        for(int i = 0; i < 20; i++) {
                            imgURL[i] = imgURL[i].replaceAll(" ", "%20");
                        }

                        Picasso.get().load(imgURL[0]).resize(width,height).into(albumArt);
                        Picasso.get().load(imgURL[1]).resize(width,height).into(albumArt2);
                        Picasso.get().load(imgURL[2]).resize(width,height).into(albumArt3);
                        Picasso.get().load(imgURL[3]).resize(width,height).into(albumArt4);
                        Picasso.get().load(imgURL[4]).resize(width,height).into(albumArt5);

                        Picasso.get().load(imgURL[5]).resize(width,height).into(albumArt6);
                        Picasso.get().load(imgURL[6]).resize(width,height).into(albumArt7);
                        Picasso.get().load(imgURL[7]).resize(width,height).into(albumArt8);
                        Picasso.get().load(imgURL[8]).resize(width,height).into(albumArt9);
                        Picasso.get().load(imgURL[9]).resize(width,height).into(albumArt10);

                        Picasso.get().load(imgURL[10]).resize(width,height).into(albumArt11);
                        Picasso.get().load(imgURL[11]).resize(width,height).into(albumArt12);
                        Picasso.get().load(imgURL[12]).resize(width,height).into(albumArt13);
                        Picasso.get().load(imgURL[13]).resize(width,height).into(albumArt14);
                        Picasso.get().load(imgURL[14]).resize(width,height).into(albumArt15);

                        Picasso.get().load(imgURL[15]).resize(width,height).into(albumArt16);
                        Picasso.get().load(imgURL[16]).resize(width,height).into(albumArt17);
                        Picasso.get().load(imgURL[17]).resize(width,height).into(albumArt18);
                        Picasso.get().load(imgURL[18]).resize(width,height).into(albumArt19);
                        Picasso.get().load(imgURL[19]).resize(width,height).into(albumArt20);
                    }
                });
            }
        }).start();
    }
    void checkPage(){
        if(pageNum == 1){
            backPage.setEnabled(false);
        }
        else{
            backPage.setEnabled(true);
        }
    }
}

0 个答案:

没有答案