Created
September 6, 2017 14:47
-
-
Save PauloCeami/d14983ab726ff2494a409de26fcb83f8 to your computer and use it in GitHub Desktop.
Carregamento de imagem com a lib Universal Image Loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/linearLayout" | |
android:layout_width="match_parent" | |
android:layout_margin="8dp" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<View | |
android:layout_width="match_parent" | |
android:layout_height="1dp" | |
android:layout_marginBottom="5dp" | |
android:layout_marginTop="5dp" | |
/> | |
<ListView | |
android:id="@+id/lvPosts" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
crie um adapter pra receber os dados do server com , é neste arquivo que as imagens serão impressas | |
package br.com.app.centralmetadevendasoficial.appvendas.Adapters; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.nostra13.universalimageloader.core.ImageLoader; | |
import com.nostra13.universalimageloader.core.assist.FailReason; | |
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener; | |
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener; | |
import java.util.List; | |
import br.com.app.centralmetadevendasoficial.appvendas.Model.ImageLoaderProdutoBean; | |
import br.com.app.centralmetadevendasoficial.appvendas.R; | |
public class ImageLoaderAdapter extends BaseAdapter { | |
private List<ImageLoaderProdutoBean> list; | |
private LayoutInflater inflater; | |
private ImageLoader mImageLoader; | |
public ImageLoaderAdapter(Context context, List<ImageLoaderProdutoBean> list, ImageLoader mImageLoader) { | |
this.list = list; | |
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
this.mImageLoader = mImageLoader; | |
} | |
@Override | |
public int getCount() { | |
return list.size(); | |
} | |
@Override | |
public Object getItem(int position) { | |
return list.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
ViewHolder holder; | |
if (convertView == null) { | |
convertView = inflater.inflate(R.layout.item, null); | |
holder = new ViewHolder(); | |
convertView.setTag(holder); | |
holder.ivPost = (ImageView) convertView.findViewById(R.id.ivPost); | |
holder.tvPost = (TextView) convertView.findViewById(R.id.tvPost); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
holder.tvPost.setText(list.get(position).getDescricao_produto()); | |
// IMAGE LOADER | |
mImageLoader.displayImage(list.get(position).getUrl_image(), | |
holder.ivPost, | |
null, | |
new ImageLoadingListener() { | |
@Override | |
public void onLoadingStarted(String s, View view) { | |
} | |
@Override | |
public void onLoadingFailed(String s, View view, FailReason failReason) { | |
} | |
@Override | |
public void onLoadingComplete(String s, View view, Bitmap bitmap) { | |
} | |
@Override | |
public void onLoadingCancelled(String s, View view) { | |
} | |
}, new ImageLoadingProgressListener() { | |
@Override | |
public void onProgressUpdate(String uri, View view, int current, int total) { | |
Log.i("Script", "onProgressUpdate(" + uri + " : " + total + " : " + current + ")"); | |
} | |
}); | |
return convertView; | |
} | |
private class ViewHolder { | |
public ImageView ivPost; | |
public TextView tvPost; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.app.centralmetadevendasoficial.appvendas.Controller; | |
import android.graphics.Bitmap; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.provider.Settings; | |
import android.support.v7.app.ActionBarActivity; | |
import android.util.Log; | |
import android.widget.ListView; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator; | |
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache; | |
import com.nostra13.universalimageloader.core.DisplayImageOptions; | |
import com.nostra13.universalimageloader.core.ImageLoader; | |
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; | |
import com.nostra13.universalimageloader.core.assist.ImageScaleType; | |
import com.nostra13.universalimageloader.core.assist.QueueProcessingType; | |
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder; | |
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer; | |
import com.nostra13.universalimageloader.core.download.BaseImageDownloader; | |
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import br.com.app.centralmetadevendasoficial.appvendas.Adapters.ImageLoaderAdapter; | |
import br.com.app.centralmetadevendasoficial.appvendas.Importacoes.VolleySingleton; | |
import br.com.app.centralmetadevendasoficial.appvendas.Model.Empresa_SqliteBean; | |
import br.com.app.centralmetadevendasoficial.appvendas.Model.Empresa_SqliteDao; | |
import br.com.app.centralmetadevendasoficial.appvendas.Model.ImageLoaderProdutoBean; | |
import br.com.app.centralmetadevendasoficial.appvendas.R; | |
import br.com.app.centralmetadevendasoficial.appvendas.Util.CustomJsonObjectRequest; | |
import br.com.app.centralmetadevendasoficial.appvendas.Util.Methods; | |
public class ImageLoaderProdutos extends ActionBarActivity { | |
private static final String TAG_IMAGENS_PRODUTOS = "imagens_produtos"; | |
private static String ID_CPF_EMPRESA; | |
private static String URL_IMAGES; | |
private static Map<String, String> params; | |
private static JSONArray imagens_produtos = null; | |
private ListView lvPosts; | |
private List<ImageLoaderProdutoBean> list; | |
private ImageLoader mImageLoader; | |
private Empresa_SqliteDao empDao; | |
private Empresa_SqliteBean empBean; | |
private RequestQueue rq; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.image_loader_produtos); | |
list = new ArrayList<>(); | |
empDao = new Empresa_SqliteDao(this); | |
empBean = new Empresa_SqliteBean(); | |
empBean = empDao.buscarEmpresa(); | |
rq = VolleySingleton.getInstance(getApplicationContext()).getRequestQueue(); | |
if (empBean != null) { | |
ID_CPF_EMPRESA = empBean.getEmp_cpf(); | |
URL_IMAGES = Methods.get(getApplicationContext()) + "/SistemaCMDV/JsonExport/imagens_produtos.php"; | |
params = new HashMap<String, String>(); | |
params.put("emp_celularkey", Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID)); | |
params.put("ID_CPF_EMPRESA", ID_CPF_EMPRESA); | |
CustomJsonObjectRequest request = new CustomJsonObjectRequest(Request.Method.POST, URL_IMAGES, params, new Response.Listener<JSONObject>() { | |
@Override | |
public void onResponse(JSONObject response) { | |
try { | |
imagens_produtos = response.getJSONArray(TAG_IMAGENS_PRODUTOS); | |
for (int i = 0; i < imagens_produtos.length(); i++) { | |
JSONObject imagem = imagens_produtos.getJSONObject(i); | |
String Img_descricao = imagem.getString("Img_descricao"); | |
String Prd_codigo = imagem.getString("Prd_codigo"); | |
String prd_descricao = imagem.getString("prd_descricao"); | |
Log.i("script", "codigo produto " + String.valueOf(Prd_codigo)); | |
Log.i("script", "descricao imagem " + Img_descricao); | |
Log.i("script", "descricao produto " + prd_descricao); | |
Log.i("script", "**************************"); | |
list.add(new ImageLoaderProdutoBean(prd_descricao, Methods.get(getApplicationContext()) + "/SistemaCMDV/view/IMAGENSPRD/img/" + Img_descricao, Integer.parseInt(Prd_codigo))); | |
} | |
if (!list.isEmpty()) { | |
DisplayImageOptions mDisplayImageOptions = new DisplayImageOptions.Builder() | |
.showImageOnLoading(R.drawable.loading) // resource or drawable | |
.showImageForEmptyUri(R.drawable.cmdvic) // resource or drawable | |
.showImageOnFail(R.drawable.fail) // resource or drawable | |
.resetViewBeforeLoading(false) // default | |
.delayBeforeLoading(1000) | |
.cacheInMemory(true) // default | |
.cacheOnDisk(false) // default | |
.considerExifParams(false) // default | |
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default | |
.bitmapConfig(Bitmap.Config.ARGB_8888) // default | |
.displayer(new SimpleBitmapDisplayer()) // default | |
.handler(new Handler()) // default | |
.build(); | |
ImageLoaderConfiguration conf = new ImageLoaderConfiguration.Builder(ImageLoaderProdutos.this) | |
.memoryCacheExtraOptions(800, 800) // default = device screen dimensions | |
.diskCacheExtraOptions(480, 800, null) | |
.threadPoolSize(3) // default | |
.threadPriority(Thread.NORM_PRIORITY - 2) // default | |
.tasksProcessingOrder(QueueProcessingType.FIFO) // default | |
.denyCacheImageMultipleSizesInMemory() | |
.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) | |
.memoryCacheSize(2 * 1024 * 1024) | |
.memoryCacheSizePercentage(13) // default | |
.diskCacheSize(50 * 1024 * 1024) | |
.diskCacheFileCount(100) | |
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default | |
.imageDownloader(new BaseImageDownloader(ImageLoaderProdutos.this)) // default | |
.imageDecoder(new BaseImageDecoder(false)) // default | |
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default | |
.writeDebugLogs() | |
.build(); | |
mImageLoader = ImageLoader.getInstance(); | |
mImageLoader.init(conf); | |
PauseOnScrollListener mPauseOnScrollListener = new PauseOnScrollListener(mImageLoader, true, true); | |
lvPosts = (ListView) findViewById(R.id.lvPosts); | |
lvPosts.setAdapter(new ImageLoaderAdapter(ImageLoaderProdutos.this, list, mImageLoader)); | |
lvPosts.setOnScrollListener(mPauseOnScrollListener); | |
} | |
} catch (JSONException e) { | |
Log.i("Script", "JSONException: " + e.getMessage()); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Log.i("Script", "VolleyError: " + error.getMessage()); | |
Log.i("Script", "VolleyError: " + error.getCause()); | |
} | |
}); | |
request.setTag("tag"); | |
rq.add(request); | |
} | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
//mImageLoader.clearMemoryCache(); | |
//mImageLoader.clearDiskCache(); | |
if (mImageLoader != null) { | |
mImageLoader.stop(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/tvPost" | |
android:layout_width="match_parent" | |
android:layout_height="25dp" | |
android:gravity="center_horizontal" | |
android:text="Teste" /> | |
<ImageView | |
android:id="@+id/ivPost" | |
android:layout_gravity="center_horizontal" | |
android:layout_width="match_parent" | |
android:layout_height="316dp" | |
android:src="@drawable/cmdvic" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment