Android, How to add a bitmap layer over another bitmap layer, dynamically from list?

社会主义新天地 提交于 2019-12-23 02:57:16

问题


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 Universal-Image-Loader library.

my getView() method is this:

@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;
    }

in following code, I'm giving list of URLs to that libray to be downloaded in different threads.

// Load and display image
String imageUrl = (video.getDefaultImage128x96().get(position));
imageUrl = imageUrl.trim().replaceAll(" ", "%20");
imageLoader.displayImage(imageUrl, holder.ivIcon, options);

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.

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();
}

putOverlay() method is like this:

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;
    }

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.

However, when main image is downloaded it overrides previous image while i expect my overlay image sits on top of this image.

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).

any suggestion/comments would be appreciated.


回答1:


Solution was easier than the thing that i was thinking.

First I removed those

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();
}

and putOverlay() method from my list adapter and then in XML file of row (list_news_adapter), I added another imageView and set its source attribute with my image.

Wow, it works fine.



来源:https://stackoverflow.com/questions/11030014/android-how-to-add-a-bitmap-layer-over-another-bitmap-layer-dynamically-from-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!