赞
踩
list为Assets下目录名
- //本地读取
- private ArrayList<PicListInfo> getImageFromAssetsFile(String list)
- {
- String test=list;
- ArrayList<PicListInfo> newpiclistinfo = new ArrayList<PicListInfo>();
- String[] list_image = null;
- try {
- //得到assets/list/目录下的所有文件的文件名,以便后面打开操作时使用
- list_image = this.getAssets().list(list);
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- for(int i=0;i<list_image.length;++i)
- {
- InputStream open = null;
- try {
- String temp = list+"/"+list_image[i];
- open = this.getAssets().open(temp);
- Bitmap bitmap = BitmapFactory.decodeStream(open);
- PicListInfo pi = new PicListInfo();
- pi.setPicUrl(bitmap);
- newpiclistinfo.add(0,pi);
- // Assign the bitmap to an ImageView in this layout
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (open != null) {
- try {
- open.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- Collections.reverse(newpiclistinfo);
- piclistinfo.addAll(newpiclistinfo);
- sla.notifyDataSetChanged();
- return piclistinfo;
- }
PicListAdapter.java
- package com.cartoon.adapters;
-
- import java.util.ArrayList;
-
- import com.cartoon.R;
-
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.MeasureSpec;
- import android.view.ViewGroup;
- import android.view.ViewGroup.MarginLayoutParams;
- import android.view.WindowManager;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
-
- public class PicListAdapter extends ArrayAdapter<PicListInfo>{
-
- public PicListAdapter(Context context, int textViewResourceId, ArrayList<PicListInfo> objects)
- {
- super(context, textViewResourceId, objects);
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder =null;
- View view;
- WindowManager wm = (WindowManager) getContext()
- .getSystemService(Context.WINDOW_SERVICE);
-
- int width = wm.getDefaultDisplay().getWidth();
- int height = wm.getDefaultDisplay().getHeight();
- if (convertView == null) {
- view = LayoutInflater.from(getContext()).inflate(R.layout.piclist_item, null);
-
- holder = new ViewHolder();
- holder.piclist_item = (ImageView)view.findViewById(R.id.piclist_item);
- //设置图片的位置
-
- ViewGroup.LayoutParams margin = new ViewGroup.LayoutParams(
- holder.piclist_item.getLayoutParams());
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
- layoutParams.height = (int) (width*0.5677);//设置图片的高度
- layoutParams.width = width; //设置图片的宽度
- layoutParams.setMargins(15, 0, 15, 0);
- holder.piclist_item.setLayoutParams(layoutParams);
- holder.piclist_item.setScaleType(ImageView.ScaleType.FIT_START);
- view.setTag(holder);
- } else {
- view = convertView;
- holder = (ViewHolder)convertView.getTag();
- }
-
- final PicListInfo singleoder = getItem(position);
- if (singleoder != null) {
- holder.piclist_item.setImageBitmap(singleoder.getPicUrl());
- }
- return view;
- }
-
- public class ViewHolder{
- public ImageView piclist_item;
- }
- }
- package com.cartoon.adapters;
-
- import android.graphics.Bitmap;
-
- public class PicListInfo {
- private Bitmap PicUrl; //图片地址
- //下面是上面属性的get和set方法
-
- public Bitmap getPicUrl() {
- return PicUrl;
- }
-
- public void setPicUrl(Bitmap picUrl) {
- PicUrl = picUrl;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。