使用一些信息创建位图叠加

时间:2014-02-07 12:28:15

标签: android bitmap android-video-player

我创建了一个Bitmap [缩略图],这是从图库中选择视频后提取的。

段: -

bm= ThumbnailUtils.createVideoThumbnail(currentFileUri.getPath(), MediaStore.Video.Thumbnails.MICRO_KIND);

我将这些位图放在Gallery适配器中,这只是图像的意思,我正在创建视频缩略图并放在那里。但是

我想在Gallery Strip中显示图像和视频之间的一些区别,这可以通过将VideoThumbnail与播放选项重叠来完成。

Something like these

尝试使用小型播放图标覆盖我的位图,但它会在Bitmap.CreateScaledBitmap(..)

上抛出 NullPointerException

段: -

bm= ThumbnailUtils.createVideoThumbnail(currentFileUri.getPath(), MediaStore.Video.Thumbnails.MICRO_KIND);
Bitmap change = null;
Bitmap border = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_media_play);
int width = bm.getWidth();
int height = bm.getHeight();
change = Bitmap.createScaledBitmap(change, width, height, false);
Canvas canvas = new Canvas(change);
Bitmap scaledBorder = Bitmap.createScaledBitmap(border,width/2,height/2, false);
canvas.drawBitmap(scaledBorder, 0, 0,null);

在我的适配器上添加新Overlay Created位图。

AddIPDActivity.this.data.add(bm);

2 个答案:

答案 0 :(得分:1)

使用BitMap

创建叠加层LayerDrawable

需要进行叠加的位图。

bm = ThumbnailUtils.createVideoThumbnail(currentFileUri.getPath(),MediaStore.Video.Thumbnails.MICRO_KIND);

Bitmap上应用了LayerDrawable,其上包含自定义图片。

Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = new BitmapDrawable(bm);
layers[1] = r.getDrawable(android.R.drawable.ic_media_play);
LayerDrawable layerDrawable = new LayerDrawable(layers);

在图库上添加了位图 - 适配器。

this.data.add(drawableToBitmap(geSingleDrawable(layerDrawable))); //data is adapter for Gallery.

将LayerDrawable转换为BitMap: -

LayerDrawable - >可绘制 - > BitMap的

  public static Drawable geSingleDrawable(LayerDrawable layerDrawable){
        int resourceBitmapHeight = 136, resourceBitmapWidth = 153;
        float widthInInches = 0.9f;
        int widthInPixels = (int)(widthInInches * SmartConsultant.getApplication().getResources().getDisplayMetrics().densityDpi);
        int heightInPixels = widthInPixels * resourceBitmapHeight / resourceBitmapWidth;
        int insetLeft = 10, insetTop = 10, insetRight = 10, insetBottom = 10;
        layerDrawable.setLayerInset(1, insetLeft, insetTop, insetRight, insetBottom);     
        Bitmap bitmap = Bitmap.createBitmap(widthInPixels, heightInPixels, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        layerDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
        layerDrawable.draw(canvas);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(SmartConsultant.getApplication().getResources(), bitmap);
        bitmapDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
        return bitmapDrawable;
}
    public static Bitmap drawableToBitmap (Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable)drawable).getBitmap();
        }
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap); 
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }

答案 1 :(得分:0)

你得到NPE,因为

Bitmap change = null;
change = Bitmap.createScaledBitmap(change, width, height, false);

change is always null。我传递了null value。所以,在将change作为createScaledBitmap(parameters)参数传递之前,将bitmap分配给change之后再调用

change = Bitmap.createScaledBitmap(change, width, height, false);