XUtils 3フレームワーク——バインド画像


  • 基本用法
  • // imageView image
    x.image().bind(imageView,image, new ImageOptions.Builder().build());
    ImageOptions options=new ImageOptions.Builder()
    // 
    .setLoadingDrawableId(R.drawable.ic_launcher)
    // 
    .setFailureDrawableId(R.drawable.ic_launcher)
    // 
    .setUseMemCache(true)
    // 
    .setCircular(true)
    // gif
    .setIgnoreGif(false)    // 
    .build();
    x.image().bind(imageView, url, imageOptions);
    
    // assets file
    x.image().bind(imageView, "assets://test.gif", imageOptions);
    
    // local file
    x.image().bind(imageView, new File("/sdcard/test.gif").toURI().toString(), imageOptions);
    x.image().bind(imageView, "/sdcard/test.gif", imageOptions);
    x.image().bind(imageView, "file:///sdcard/test.gif", imageOptions);
    x.image().bind(imageView, "file:/sdcard/test.gif", imageOptions);
    
    x.image().bind(imageView, url, imageOptions, new Callback.CommonCallback() {...});
    x.image().loadDrawable(url, imageOptions, new Callback.CommonCallback() {...});
    x.image().loadFile(url, imageOptions, new Callback.CommonCallback() {...});

    2.典型的な例
    @ContentView(R.layout.fragment_image)
    public class ImageFragment extends BaseFragment {
    
        private String[] imgSites = {
                "http://image.baidu.com/",
                "http://www.22mm.cc/",
                "http://www.moko.cc/",
                "http://eladies.sina.com.cn/photo/",
                "http://www.youzi4.com/"
        };
    
        ImageOptions imageOptions;
    
        @ViewInject(R.id.lv_img)
        private ListView imageListView;
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            imageOptions = new ImageOptions.Builder()
                    .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))
                    .setRadius(DensityUtil.dip2px(5))
                    //  ImageView wrap_content,  crop.
                    .setCrop(true) //  scaleType .
                    //  ScaleType
                    //.setPlaceholderScaleType(ImageView.ScaleType.MATRIX)
                    .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                    .setLoadingDrawableId(R.mipmap.ic_launcher)
                    .setFailureDrawableId(R.mipmap.ic_launcher)
                    .build();
    
            imageListAdapter = new ImageListAdapter();
            imageListView.setAdapter(imageListAdapter);
    
            //  url listview
            //  , , , ...
            for (String url : imgSites) {
                loadImgList(url);
            }
        }
    
        @Event(value = R.id.lv_img, type = AdapterView.OnItemClickListener.class)
        private void onImageItemClick(AdapterView> parent, View view, int position, long id) {
            Intent intent = new Intent(this.getActivity(), BigImageActivity.class);
            intent.putExtra("url", imageListAdapter.getItem(position).toString());
            this.getActivity().startActivity(intent);
        }
    
        private void loadImgList(String url) {
            x.http().get(new RequestParams(url), new Callback.CommonCallback() {
                @Override
                public void onSuccess(String result) {
                    imageListAdapter.addSrc(getImgSrcList(result));
                    imageListAdapter.notifyDataSetChanged();// listview 
                }
    
                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
    
                }
    
                @Override
                public void onCancelled(CancelledException cex) {
    
                }
    
                @Override
                public void onFinished() {
    
                }
            });
        }
    
        private ImageListAdapter imageListAdapter;
    
        private class ImageListAdapter extends BaseAdapter {
    
            private final LayoutInflater mInflater;
            private ArrayList imgSrcList;
    
            public ImageListAdapter() {
                super();
                mInflater = LayoutInflater.from(getContext());
                imgSrcList = new ArrayList();
            }
    
            public void addSrc(List imgSrcList) {
                this.imgSrcList.addAll(imgSrcList);
            }
    
            public void addSrc(String imgUrl) {
                this.imgSrcList.add(imgUrl);
            }
    
            @Override
            public int getCount() {
                return imgSrcList.size();
            }
    
            @Override
            public Object getItem(int position) {
                return imgSrcList.get(position);
            }
    
            @Override
            public long getItemId(int i) {
                return i;
            }
    
            @Override
            public View getView(final int position, View view, ViewGroup parent) {
                ImageItemHolder holder = null;
                if (view == null) {
                    view = mInflater.inflate(R.layout.image_item, parent, false);
                    holder = new ImageItemHolder();
                    x.view().inject(holder, view);
                    view.setTag(holder);
                } else {
                    holder = (ImageItemHolder) view.getTag();
                }
                holder.imgPb.setProgress(0);
                x.image().bind(holder.imgItem,
                        imgSrcList.get(position),
                        imageOptions,
                        new CustomBitmapLoadCallBack(holder));
                return view;
            }
        }
    
        private class ImageItemHolder {
            @ViewInject(R.id.img_item)
            private ImageView imgItem;
    
            @ViewInject(R.id.img_pb)
            private ProgressBar imgPb;
        }
    
        public class CustomBitmapLoadCallBack implements Callback.ProgressCallback {
            private final ImageItemHolder holder;
    
            public CustomBitmapLoadCallBack(ImageItemHolder holder) {
                this.holder = holder;
            }
    
            @Override
            public void onWaiting() {
                this.holder.imgPb.setProgress(0);
            }
    
            @Override
            public void onStarted() {
    
            }
    
            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                this.holder.imgPb.setProgress((int) (current * 100 / total));
            }
    
            @Override
            public void onSuccess(Drawable result) {
                this.holder.imgPb.setProgress(100);
            }
    
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
    
            @Override
            public void onCancelled(CancelledException cex) {
    
            }
    
            @Override
            public void onFinished() {
    
            }
        }
    
        /**
         *  
         */
        public static List getImgSrcList(String htmlStr) {
            List pics = new ArrayList();
    
            String regEx_img = "; //  
            Pattern p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
            Matcher m_image = p_image.matcher(htmlStr);
            while (m_image.find()) {
                String src = m_image.group(1);
                if (src.length() < 100) {
                    pics.add("http://" + src + ".jpg");
                    //pics.add("http://f.hiphotos.baidu.com/zhidao/pic/item/2fdda3cc7cd98d104cc21595203fb80e7bec907b.jpg");
                }
            }
            return pics;
        }
    
    }