Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. public class DecodeTask extends AsyncTask<String, Void, Bitmap> {
  2.  
  3. private static int MaxTextureSize = 2048; /* True for most devices. */
  4.  
  5. public ImageView v;
  6.  
  7. public DecodeTask(ImageView iv) {
  8. v = iv;
  9. }
  10.  
  11. protected Bitmap doInBackground(String... params) {
  12. BitmapFactory.Options opt = new BitmapFactory.Options();
  13. opt.inPurgeable = true;
  14. opt.inPreferQualityOverSpeed = false;
  15. opt.inSampleSize = 1;
  16.  
  17. Bitmap bitmap = null;
  18. if(isCancelled()) {
  19. return bitmap;
  20. }
  21.  
  22. opt.inJustDecodeBounds = true;
  23. BitmapFactory.decodeFile(params[0], opt);
  24. while(opt.outHeight > MaxTextureSize || opt.outWidth > MaxTextureSize) {
  25. opt.inSampleSize++;
  26. BitmapFactory.decodeFile(params[0], opt);
  27. }
  28. opt.inJustDecodeBounds = false;
  29.  
  30. bitmap = BitmapFactory.decodeFile(params[0], opt);
  31. return bitmap;
  32. }
  33.  
  34. @Override
  35. protected void onPostExecute(Bitmap result) {
  36. if(v != null) {
  37. v.setImageBitmap(result);
  38. }
  39. }
  40.  
  41. }
  42.  
  43. @Override
  44. public View getView(int position, View convertView, ViewGroup parent) {
  45. ImageView iv = null;
  46. if(convertView == null) {
  47. convertView = getLayoutInflater().inflate(R.id.your_view, null); /* Inflate your view here */
  48. iv = convertView.findViewById(R.id.your_image_view);
  49. } else {
  50. iv = convertView.findViewById(R.id.your_image_view);
  51. DecodeTask task = (DecodeTask)iv.getTag(R.id.your_image_view);
  52. if(task != null) {
  53. task.cancel(true);
  54. }
  55. }
  56. iv.setImageBitmap(null);
  57. DecodeTask task = new DecodeTask(iv);
  58. task.execute(getItem(position) /* File path to image */);
  59. iv.setTag(R.id.your_image_view, task);
  60.  
  61. return convertView;
  62. }
  63.  
  64. listView.setOnScrollListener(new OnScrollListener() {
  65. @Override
  66. public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
  67. int totalItemCount) {
  68. // Don't care.
  69. }
  70.  
  71. @Override
  72. public void onScrollStateChanged(final AbsListView view, int scrollState) {
  73. // Load images here. ListView can tell you what rows are visible, you load images for these rows and update corresponding View-s.
  74. }
  75. })
  76.  
  77. private class CallService extends AsyncTask<String, Integer, String>
  78. {
  79. protected String doInBackground(String... u)
  80. {
  81. fetchReasons();
  82. return null;
  83. }
  84. protected void onPreExecute()
  85. {
  86. //Define the loader here.
  87.  
  88. }
  89. public void onProgressUpdate(Integer... args)
  90. {
  91. }
  92. protected void onPostExecute(String result)
  93. {
  94. //remove loader
  95. //Add data to your view.
  96. }
  97. }
  98.  
  99. public void fetchReasons()
  100. {
  101. //Call Your Web Service and save the records in the arrayList
  102. }
  103.  
  104. @Override
  105. public long getItemViewType(int position) {
  106. return position;
  107. }
  108. @Override
  109. public long getItemViewCount(int position) {
  110. return getCount();
  111. }
  112. @Override
  113. public View getView(int position, View convertView, ViewGroup arg2) {
  114. if (convertView == null) {
  115. //inflate your convertView and set everything
  116. }
  117. //do not do anything just return the convertView
  118. return convertView;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement