来自url的android加载图像与文件中的加载图像不同

时间:2016-02-25 10:34:59

标签: android

我按照this link从网址加载图片

这是活动

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:id="@+id/imageViewImg3"/>

</LinearLayout>

</RelativeLayout>

如果从文件

加载图像,这是Binderdata
public class trang3Binderdata extends BaseAdapter {
static final String KEY_IMG = "img";
LayoutInflater inflater;
List<HashMap<String,String>> imgHashmap;
ViewHolder holder;
public trang3Binderdata() {
    // TODO Auto-generated constructor stub
}

public trang3Binderdata(Activity act, List<HashMap<String,String>> map) {
    this.imgHashmap = map;
    inflater = (LayoutInflater) act
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    // TODO Auto-generated method stub

    return imgHashmap.size();
}

public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null){
        vi = inflater.inflate(R.layout.list_img, null);
        holder = new ViewHolder();
        holder.ivImgtrang3 =(ImageView)vi.findViewById(R.id.imageViewImg3);
        vi.setTag(holder);
    }
    else{

        holder = (ViewHolder)vi.getTag();
    }

    String uri = "drawable/"+ imgHashmap .get(position).get(KEY_IMG);
    int imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());
    Drawable image = vi.getContext().getResources().getDrawable(imageResource);
    holder.ivImgtrang3.setImageDrawable(image);

    return vi;
}

static class ViewHolder{
    ImageView ivImgtrang3;
}

}

这是结果
enter image description here

这是来自网址的Binderdata加载图片

public class trang3Binderdata extends BaseAdapter {
static final String KEY_IMG = "img";
LayoutInflater inflater;
List<HashMap<String,String>> imgHashmap;
ViewHolder holder;
public trang3Binderdata() {
    // TODO Auto-generated constructor stub
}

public trang3Binderdata(Activity act, List<HashMap<String,String>> map) {
    this.imgHashmap = map;
    inflater = (LayoutInflater) act
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    // TODO Auto-generated method stub
    return imgHashmap.size();
}

public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null){
        vi = inflater.inflate(R.layout.list_img, null);
        holder = new ViewHolder();
        holder.ivImgtrang3 =(ImageView)vi.findViewById(R.id.imageViewImg3);
        vi.setTag(holder);
    }
    else{

        holder = (ViewHolder)vi.getTag();
    }

    int loader = R.drawable.skorea;
    String uri = imgHashmap .get(position).get(KEY_IMG);
    ImageLoader imgLoader = new ImageLoader(vi.getContext().getApplicationContext());
    imgLoader.DisplayImage(uri, loader, holder.ivImgtrang3);

    return vi;
}

static class ViewHolder{
    ImageView ivImgtrang3;
}

}

这是结果
enter image description here

我想从url加载图像,如从文件加载图像。但结果不一样。为什么?
请帮我。谢谢你的阅读

1 个答案:

答案 0 :(得分:0)

只需更改布局xml以满足您的需求,我删除RelativeLayout,因为我看到它没有必要,因为您只有一个图像视图。您可能需要在图像中设置scaleType以达到最佳比例视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent" // change width to match parent 
        android:layout_height="wrap_content"  // change height to specific number or let it as its wrap_content
        android:adjustViewBounds="true"
        android:id="@+id/imageViewImg3"/>

</LinearLayout>