0

我试图用通用图像加载器做背景。但它没有这是 BaseActivity.java 源代码:https ://github.com/nostra13/Android-Universal-Image-Loader/tree/master/sample/src/com/nostra13/example/universalimageloader 我该怎么做壁纸选择的图像作为背景。我知道所选图片网址的 >> imageUrls2[pagerPosition]

BaseActivity.java

    //Copyright 2011-2013 Sergey Tarasevich
package com.nostra13.example.universalimageloader;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.nostra13.example.universalimageloader.Constants.Extra;
import com.nostra13.universalimageloader.core.ImageLoader;

public abstract class BaseActivity extends Activity {

String[] imageUrls2;
protected ImageLoader imageLoader = ImageLoader.getInstance();

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.item_clear_memory_cache:
            imageLoader.clearMemoryCache();
            return true;
        case R.id.item_clear_disc_cache:
            imageLoader.clearDiscCache();
            return true;
        case R.id.setBg: //Set Background Menu 

            Bundle bundle = getIntent().getExtras();
            imageUrls2 = bundle.getStringArray(Extra.IMAGES);

            int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

            Toast.makeText(getApplicationContext(), imageUrls2[pagerPosition], Toast.LENGTH_LONG).show();
    //For example http://www.wallpaperfx.com/view_image/abstract-glass-paint-1600x900-wallpaper-14862.jpg
            }
            return false;
}

}

ImageGalleryActivity.java

    package com.nostra13.example.universalimageloader;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.nostra13.example.universalimageloader.Constants.Extra;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

/**
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 */
@SuppressWarnings("deprecation")
public class ImageGalleryActivity extends BaseActivity {

    String[] imageUrls;

    DisplayImageOptions options;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_image_gallery);

        Bundle bundle = getIntent().getExtras();
        imageUrls = bundle.getStringArray(Extra.IMAGES);

        options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.ic_stub)
            .showImageForEmptyUri(R.drawable.ic_empty)
            .showImageOnFail(R.drawable.ic_error)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

        Gallery gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new ImageGalleryAdapter());
        gallery.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startImagePagerActivity(position);
            }
        });
    }

    private void startImagePagerActivity(int position) {
        Intent intent = new Intent(this, ImagePagerActivity.class);
        intent.putExtra(Extra.IMAGES, imageUrls);
        intent.putExtra(Extra.IMAGE_POSITION, position);
        startActivity(intent);
    }

    private class ImageGalleryAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return imageUrls.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = (ImageView) convertView;
            if (imageView == null) {
                imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_gallery_image, parent, false);
            }
            imageLoader.displayImage(imageUrls[position], imageView, options);
            return imageView;
        }
    }
}
4

1 回答 1

0

根据您的信息,我认为您可以在基本活动上使用受保护的方法(或抽象方法)并在子类活动(ImagePagerActivity)中覆盖它。它看起来像这样:

 public abstract class BaseActivity extends Activity {

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_clear_memory_cache:
                imageLoader.clearMemoryCache();
                return true;
            case R.id.item_clear_disc_cache:
                imageLoader.clearDiscCache();
                return true;
            case R.id.setBg: //Set Background Menu 

                Bundle bundle = getIntent().getExtras();
                imageUrls2 = bundle.getStringArray(Extra.IMAGES);
                int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);
                Toast.makeText(getApplicationContext(), imageUrls2[pagerPosition], Toast.LENGTH_LONG).show();

                // call protected method, subclass should override it
                displayImage(imageUrls2[pagerPosition]);    
                }
                return false;
    }

    protected void displayImage(String imageUrl){}
}

public class ImagePagerActivity extends BaseActivity{       
    @override
    public void displayImage(String imageUrl){
        imageLoader.displayImage(imageUrl, Your_Image_View);        
    }
}
于 2014-07-22T03:58:47.230 回答