当前位置:   article > 正文

Android:漫画APP开发笔记之从Assets本地读取图片_漫画中asset是什么文件

漫画中asset是什么文件

list为Assets下目录名

  1. //本地读取
  2. private ArrayList<PicListInfo> getImageFromAssetsFile(String list)
  3. {
  4. String test=list;
  5. ArrayList<PicListInfo> newpiclistinfo = new ArrayList<PicListInfo>();
  6. String[] list_image = null;
  7. try {
  8. //得到assets/list/目录下的所有文件的文件名,以便后面打开操作时使用
  9. list_image = this.getAssets().list(list);
  10. } catch (IOException e1) {
  11. // TODO Auto-generated catch block
  12. e1.printStackTrace();
  13. }
  14. for(int i=0;i<list_image.length;++i)
  15. {
  16. InputStream open = null;
  17. try {
  18. String temp = list+"/"+list_image[i];
  19. open = this.getAssets().open(temp);
  20. Bitmap bitmap = BitmapFactory.decodeStream(open);
  21. PicListInfo pi = new PicListInfo();
  22. pi.setPicUrl(bitmap);
  23. newpiclistinfo.add(0,pi);
  24. // Assign the bitmap to an ImageView in this layout
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. } finally {
  28. if (open != null) {
  29. try {
  30. open.close();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36. }
  37. Collections.reverse(newpiclistinfo);
  38. piclistinfo.addAll(newpiclistinfo);
  39. sla.notifyDataSetChanged();
  40. return piclistinfo;
  41. }
PicListAdapter.java

  1. package com.cartoon.adapters;
  2. import java.util.ArrayList;
  3. import com.cartoon.R;
  4. import android.content.Context;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.MeasureSpec;
  8. import android.view.ViewGroup;
  9. import android.view.ViewGroup.MarginLayoutParams;
  10. import android.view.WindowManager;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.ImageView;
  13. import android.widget.ListAdapter;
  14. import android.widget.ListView;
  15. import android.widget.RelativeLayout;
  16. import android.widget.TextView;
  17. public class PicListAdapter extends ArrayAdapter<PicListInfo>{
  18. public PicListAdapter(Context context, int textViewResourceId, ArrayList<PicListInfo> objects)
  19. {
  20. super(context, textViewResourceId, objects);
  21. }
  22. @Override
  23. public View getView(int position, View convertView, ViewGroup parent) {
  24. ViewHolder holder =null;
  25. View view;
  26. WindowManager wm = (WindowManager) getContext()
  27. .getSystemService(Context.WINDOW_SERVICE);
  28. int width = wm.getDefaultDisplay().getWidth();
  29. int height = wm.getDefaultDisplay().getHeight();
  30. if (convertView == null) {
  31. view = LayoutInflater.from(getContext()).inflate(R.layout.piclist_item, null);
  32. holder = new ViewHolder();
  33. holder.piclist_item = (ImageView)view.findViewById(R.id.piclist_item);
  34. //设置图片的位置
  35. ViewGroup.LayoutParams margin = new ViewGroup.LayoutParams(
  36. holder.piclist_item.getLayoutParams());
  37. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
  38. layoutParams.height = (int) (width*0.5677);//设置图片的高度
  39. layoutParams.width = width; //设置图片的宽度
  40. layoutParams.setMargins(15, 0, 15, 0);
  41. holder.piclist_item.setLayoutParams(layoutParams);
  42. holder.piclist_item.setScaleType(ImageView.ScaleType.FIT_START);
  43. view.setTag(holder);
  44. } else {
  45. view = convertView;
  46. holder = (ViewHolder)convertView.getTag();
  47. }
  48. final PicListInfo singleoder = getItem(position);
  49. if (singleoder != null) {
  50. holder.piclist_item.setImageBitmap(singleoder.getPicUrl());
  51. }
  52. return view;
  53. }
  54. public class ViewHolder{
  55. public ImageView piclist_item;
  56. }
  57. }

PicListInfo.java

  1. package com.cartoon.adapters;
  2. import android.graphics.Bitmap;
  3. public class PicListInfo {
  4. private Bitmap PicUrl; //图片地址
  5. //下面是上面属性的get和set方法
  6. public Bitmap getPicUrl() {
  7. return PicUrl;
  8. }
  9. public void setPicUrl(Bitmap picUrl) {
  10. PicUrl = picUrl;
  11. }
  12. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/720108
推荐阅读
相关标签
  

闽ICP备14008679号