Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android:漫畫APP開發筆記之從Assets本地讀取圖片

Android:漫畫APP開發筆記之從Assets本地讀取圖片

編輯:關於Android編程

list為Assets下目錄名

 

//本地讀取
	 private ArrayList getImageFromAssetsFile(String list)  
	  {  
		 String test=list;
		 ArrayList newpiclistinfo = new ArrayList();
		 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;iPicListAdapter.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{
	
	public PicListAdapter(Context context, int textViewResourceId, ArrayList 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;
	}
}

PicListInfo.java

 

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;
	}
	
}


 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved