UIL不显示图像

时间:2014-02-10 09:05:17

标签: java android universal-image-loader

这不是我第一次使用UIL,我是否在我的应用程序中使用了UIl,我无法在活动B 上使用它?有时在我反对后调用UIL onLoadingComplete。如果我将活动B代码转移到单独的项目,图像加载

    public class ExpandableCardListView2 extends FragmentActivity {

    static CardArrayAdapter mCardArrayAdapter;
    ArrayList<Card> cards;
    public static Dialog FDialog;
    public static Card card;
    ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_expandable_card_list_view2);

        this.getActionBar().setDisplayHomeAsUpEnabled(true);
        this.getActionBar().setDisplayShowHomeEnabled(false);
        this.getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#d85700")));

              setContentView(R.layout.activity_expandable_card_list_view2);

              Fragment mFragment = new UniversalImageLoaderFragment();

              FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
              ft.replace(R.id.google_card_main, mFragment, "settings1");
              ft.addToBackStack(null);
              ft.commit();          

    }

UniversalImageLoaderFragment.java

public class UniversalImageLoaderFragment extends BaseFragment{


    DisplayImageOptions options;

    @Override
    public String getTitleResourceId() {
        return "Title";
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.demo_extras_fragment_picasso, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initUniversalImageLoaderLibrary();
        initCard();
    }

    /**
     * Android-Universal-Image-Loader config.
     *
     * DON'T COPY THIS CODE TO YOUR REAL PROJECT!     *
     * I would recommend doing it in an overloaded Application.onCreate().
     * It is just for test purpose
     *
     *
     */
    private void initUniversalImageLoaderLibrary(){

        File cacheDir = StorageUtils.getCacheDirectory(getActivity());
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity())
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                .writeDebugLogs()
                .build();

        ImageLoader.getInstance().init(config);

        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .displayer(new SimpleBitmapDisplayer())
                .showImageOnFail(R.drawable.ab_bg_black)
                .build();
    }


    /**
     * This method builds a simple card
     */
    private void initCard() {

        //Init an array of Cards
        ArrayList<Card> cards = new ArrayList<Card>();
        for (int i = 0; i < 200; i++) {
            UniversalImageLoaderCard card = new UniversalImageLoaderCard(this.getActivity(),options);
            card.setTitle("A simple card loaded with Universal-Image-Loader " + i);
            card.setSecondaryTitle("Simple text..." + i);
            card.setCount(i);
            cards.add(card);
        }

        CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(getActivity(), cards);

        CardListView listView = (CardListView) getActivity().findViewById(R.id.carddemo_extra_list_picasso);
        if (listView != null) {
            listView.setAdapter(mCardArrayAdapter);
        }

    }

}

UniversalImageLoaderCard.java

public class UniversalImageLoaderCard extends Card{

    protected String mTitle;
    protected String mSecondaryTitle;
    protected int count;

    DisplayImageOptions options;

    public UniversalImageLoaderCard(Context context,DisplayImageOptions options) {
        this(context, R.layout.dialog_notification2);
        this.options=options;
    }

    public UniversalImageLoaderCard(Context context, int innerLayout) {
        super(context, innerLayout);
        init();
    }

    private void init() {

        //Add thumbnail
        UniversalCardThumbnail cardThumbnail = new UniversalCardThumbnail(mContext);
        ///cardThumbnail.setExternalUsage(true);
        addCardThumbnail(cardThumbnail);

        //Add ClickListener
        setOnClickListener(new OnCardClickListener() {
            @Override
            public void onClick(Card card, View view) {
                Toast.makeText(getContext(), "Click Listener card=", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void setupInnerViewElements(ViewGroup parent, View view) {

        //Retrieve elements
        TextView title = (TextView) parent.findViewById(R.id.textView1);
        Log.i("##Inside setupInnerViewElements: ", "UniversalImageLoaderCard");

        if (title != null)
            title.setText(mTitle);



    }

    /**
     * CardThumbnail which uses Universal-Image-Loader  Library.
     * If you use an external library you have to provide your login inside #setupInnerViewElements.
     *
     * This method is called before built-in method.
     * If {@link it.gmariotti.cardslib.library.internal.CardThumbnail#isExternalUsage()} is false it uses the built-in method.
     */
    class UniversalCardThumbnail extends CardThumbnail {

        public UniversalCardThumbnail(Context context) {
            super(context);
        }

        @Override
        public void setupInnerViewElements(ViewGroup parent, View viewImage) {

            SoftReference<AnimateFirstDisplayListener> animateFirstListener = new SoftReference<AnimateFirstDisplayListener>(new AnimateFirstDisplayListener());
            /*
             * If your cardthumbnail uses external library you have to provide how to load the image.
             * If your cardthumbnail doesn't use an external library it will use a built-in method
             */

            //It is just an example.
            //In real case you should config better the imageLoader
            ImageLoader imageLoader = ImageLoader.getInstance();
            imageLoader.init(ImageLoaderConfiguration.createDefault(mContext .getApplicationContext()));


            //Here you have to set your image with an external library
            //Only for test, use a Resource Id and a Url
            Log.i("##Inside setupInnerViewElements: ", "UniversalCardThumbnail");
                imageLoader.displayImage("http://jinisyssoftware.com/wp-content/themes/jinisys/images/header-logo.png", (ImageView) viewImage.findViewById(R.id.img2), options, animateFirstListener.get());


            /*
            viewImage.getLayoutParams().width = 96;
            viewImage.getLayoutParams().height = 96;
            */
        }
    }


    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public String getSecondaryTitle() {
        return mSecondaryTitle;
    }

    public void setSecondaryTitle(String secondaryTitle) {
        mSecondaryTitle = secondaryTitle;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {


        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }
            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                Log.i("##Inside onLoadingFailed: ", "value");

            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                Log.i("##Inside onLoadingCancelled: ", "value");
            }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

            if (loadedImage != null) {

                WeakReference<ImageView> imageView = new WeakReference<ImageView>((ImageView) view);

                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    Log.i("##Inside onLoadingComplete: ", "value");
                    FadeInBitmapDisplayer.animate(imageView.get(), 500);
                    displayedImages.add(imageUri);
                }
            }

        }
    }




}

1 个答案:

答案 0 :(得分:0)

我在我的程序中多次使用UIL,这么多初始化。我所做的是创建了一个包含一个函数的类,该函数将返回UIL的新实例,这样我就可以在程序中访问它。