在Android应用程序中显示URL中的图像?

时间:2016-08-05 05:40:40

标签: android performance listview bitmap

我正在尝试创建一个解析HTML并检索图像并创建新闻或帖子列表的应用。

我有显示图像的问题,因为当我下载它的大而且当我尝试使用位图解码来采样图像的大小时,ListClass对象只支持整数数组(R.id .. )。

我研究过使用InputStream但它只支持Drawable。有没有办法在网站上显示我的列表中的图像,所以它没有分配太多的内存,导致应用程序放慢速度?

主要活动的图片;

enter image description here

我的主要活动

[![import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.widget.FrameLayout;
import android.widget.Button;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;




public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener   {

    ListView list;
    String\[\] web = {
            "asd "};
    Integer\[\] imageId = {
            R.drawable.pol};

    Handler handler;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView text = (TextView)findViewById(R.id.textView);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        listClass adapter = new
                listClass(MainActivity.this, web, imageId);
        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(MainActivity.this, "You Clicked at " +web\[+ position\], Toast.LENGTH_SHORT).show();
            }
        });

    }

我试图实现的InputStream类;

import android.media.Image;
import java.net.URL;
import android.graphics.BitmapFactory;
import java.io.InputStream;
import android.graphics.drawable.Drawable;





public class imageHandler {

    Image picture;


    public static Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "earthdownload");
            return d;
        } catch (Exception e) {
            return null;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试这一点也可以根据你修复尺寸..或者看看使用通用装载程序调整大小

<ImageView
android:id="@id/ivImage"
android:layout_width="150dp"
android:layout_height="150dp"
android:adjustViewBounds="true"

android:scaleType="fitCenter" />

使用了Android Universal Image-Loader

  

声明

    private ImageLoader imageLoader1;
  

创建

 imageLoader1 = ImageLoader.getInstance();

 imageLoader1.init(ImageLoaderConfiguration.createDefault(getActivity()));
  

no_image这里是一个可在Cache中没有任何图像加载的可绘制图像

   DisplayImageOptions
            options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .showImageOnLoading(R.drawable.no_image) // resource or drawable
            .showImageForEmptyUri(R.drawable.no_image) // resource or drawable
            .showImageOnFail(R.drawable.no_image)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    imageLoader1.displayImage(yourpath.replace(" ", "%20"), ivImage, options);

您也可以使用Piccaso并同时滑行