ListView中混合使用Drawable文件夹资源和网络资源

2024-04-06 22:38

本文主要是介绍ListView中混合使用Drawable文件夹资源和网络资源,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:background="#FFFFFF">
 
  <ImageView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/image"
   android:src="@drawable/icon_1"
   android:visibility="gone"></ImageView>
 
  <ListView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/listview"></ListView>
 
</LinearLayout>


2、listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
 
  <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:id="@+id/img" 
  /> 
  <TextView android:id="@+id/listtext"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content" 
       android:textColor="#000000"  
       android:textSize="18dip" ></TextView> 
</LinearLayout>

3、

package moile.drawable.main;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListViewActivity extends Activity {
 private  ImageView imageView ;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.listview);
  imageView = (ImageView)this.findViewById(R.id.image); //放置该ImageView是为了能够设置ListView中图片的大小
  
     byte[] data = null;//1、使用Bitmap的方式
  try {
   data = this.getImage("http://online.sccnn.com/icon/1202/01.png");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }//得到图片的二进制数据
      Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//生成位图   


  ListView listView = (ListView)this.findViewById(R.id.listview);
  
  List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();  
  HashMap<String, Object> map = new HashMap<String, Object>(); 
        map.put("ItemImg",R.drawable.icon); 
        map.put("ItemTitle", "第一行"); 
        list.add(map); 
          
        map = new HashMap<String, Object>(); 
        map.put("ItemImg",R.drawable.icon_1); 
        map.put("ItemTitle", "第二行"); 
        list.add(map); 
        
        map = new HashMap<String, Object>();  //1、使用Bitmap的方式
        map.put("ItemImg",bitmap); 
        map.put("ItemTitle", "我的Test"); 
        list.add(map);
        
        SimpleAdapter m = new SimpleAdapter(this,list,R.layout.listitem,
           new String[]{"ItemImg","ItemTitle"},
           new int[]{R.id.img,R.id.listtext});
        
        listView.setAdapter(m);
        
        m.setViewBinder(new SimpleAdapter.ViewBinder() {   
   @Override
   public boolean setViewValue(View view, Object data,
     String textRepresentation) {
    // TODO Auto-generated method stub
    if(view instanceof ImageView  && data instanceof Bitmap){    //1、使用Bitmap的方式
      ImageView iv = (ImageView) view;   
      Bitmap bitmap = (Bitmap) data;     
      Bitmap b = Bitmap.createScaledBitmap(bitmap, imageView.getDrawable().getIntrinsicWidth(), imageView.getDrawable().getIntrinsicHeight(), true);//创建一个可以缩放的位图对象
      iv.setImageBitmap(b);
      return true;   
    }else   
      return false;  
    }
  });
 }
  
  public byte[] getImage(String path) throws Exception{
    URL url = new URL(path);
    //get //post
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5*1000);
    InputStream inStream = conn.getInputStream();
   
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;
    while( (len = inStream.read(buffer)) !=-1 ){
     outStream.write(buffer, 0, len);
    }
    byte[] data = outStream.toByteArray();//图片的二进制数据
    outStream.close();
    inStream.close();
    return data;

  }

}

 

这篇关于ListView中混合使用Drawable文件夹资源和网络资源的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/881008

相关文章

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

Go语言使用select监听多个channel的示例详解

《Go语言使用select监听多个channel的示例详解》本文将聚焦Go并发中的一个强力工具,select,这篇文章将通过实际案例学习如何优雅地监听多个Channel,实现多任务处理、超时控制和非阻... 目录一、前言:为什么要使用select二、实战目标三、案例代码:监听两个任务结果和超时四、运行示例五

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.