title: Android, How to add a bitmap layer over another bitmap layer, dynamically from list?
body: <p>In my application, I have a list adapter. each row includes an imageView in left and two textViews in right. For getting images I'm using <a href="https://github.com/nostra13/Android-Universal-Image-Loader" rel="nofollow noreferrer">Universal-Image-Loader</a> library.</p>
  
  <p>my getView() method is this:</p>
  
  <pre><code>@Override
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;        
  
          if (convertView == null) {
              convertView     = myInflater.inflate(R.layout.list_news_adapter, null);
              holder          = new ViewHolder();
              holder.ivIcon   = (ImageView) convertView.findViewById(R.id.list_news_icon);
              holder.tvTitle  = (TextView)  convertView.findViewById(R.id.list_news_title);
              holder.tvDate   = (TextView)  convertView.findViewById(R.id.list_news_date);
  
              convertView.setTag(holder);
          } else {
              holder = (ViewHolder) convertView.getTag();
          }
  
  
          holder.tvTitle.setText(video.getTitle().get(position));
          holder.tvDate.setText(localTime.get(position));
  
          // Load and display image
          String imageUrl = (video.getDefaultImage128x96().get(position));
          imageUrl = imageUrl.trim().replaceAll(" ", "%20");
          imageLoader.displayImage(imageUrl, holder.ivIcon, options);
  
  
  
          holder.ivIcon.buildDrawingCache();
          Bitmap image = holder.ivIcon.getDrawingCache();
          if(image != null) {
              image = putOverlay(image, holder.ivIcon.getWidth(), holder.ivIcon.getHeight());
              holder.ivIcon.setImageBitmap(image);
              holder.ivIcon.destroyDrawingCache();
          }
  
  
          return convertView;
      }
  </code></pre>
  
  <p>in following code, I'm giving list of URLs to that libray to be downloaded in different threads.</p>
  
  <pre><code>// Load and display image
  String imageUrl = (video.getDefaultImage128x96().get(position));
  imageUrl = imageUrl.trim().replaceAll(" ", "%20");
  imageLoader.displayImage(imageUrl, holder.ivIcon, options);
  </code></pre>
  
  <p>after that in following code, i'm getting current image of imageView (which is downloaded) and ask other method to add a layer on top of this image.</p>
  
  <pre><code>holder.ivIcon.buildDrawingCache();
  Bitmap image = holder.ivIcon.getDrawingCache();
  
  if(image != null) {
          image = putOverlay(image, holder.ivIcon.getWidth(), holder.ivIcon.getHeight());
          holder.ivIcon.setImageBitmap(image);
          holder.ivIcon.destroyDrawingCache();
  }
  </code></pre>
  
  <p>putOverlay() method is like this:</p>
  
  <pre><code>public Bitmap putOverlay(Bitmap bmp, int width, int height) {
          Bitmap overLay = BitmapFactory.decodeResource(context.getResources(), R.drawable.video_overlay);
  
          Bitmap bmBackground = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
          Canvas canvas = new Canvas(bmBackground);
          Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
          canvas.drawBitmap(bmp, 0, 0, paint); 
          canvas.drawBitmap(overLay, width*3/8, height*3/8, paint);
  
          return bmBackground;
      }
  </code></pre>
  
  <p>It was functional part. What about result!
  Universal Image Loader library uses predefined image as long as main image is under downloading. my code can merge this two layers.
  <img src="https://i.stack.imgur.com/xl6sZ.png" alt="enter image description here"></p>
  
  <p>However, when main image is downloaded it overrides previous image while i expect my overlay image sits on top of this image.
  <img src="https://i.stack.imgur.com/gRKwa.png" alt="enter image description here"></p>
  
  <p>But, after scroll of screen, i can see my added image (most of images has and some of them doesn't have my added image).
  <img src="https://i.stack.imgur.com/lbkfF.png" alt="enter image description here"></p>
  
  <p>any suggestion/comments would be appreciated.  </p>