Android的 - 最好的方式来覆盖在图像/缩略图播放按钮最好的、缩略图、图像、方式

2023-09-05 04:10:02 作者:我们会发光

我有一个Android应用程序,它可以播放音频/视频和显示图片。 对于视频我要覆盖一个播放按钮在preVIEW图像的顶部,并在列表视图.. 现在我怎么做,就与XM​​L的ImageView的,然后被拉伸是一层一层名单,我定义编程,因为图像中的一个是动态的,播放按钮是静态的,当然。我要对齐从底部的播放按钮10px的,并且水平居中。

I have an android application which can playback audio/video and show pictures. For videos I want to overlay a play button on top of the preview image and also in list views.. Right now how I'm doing it is with an ImageView in xml, and then the drawable is a layer layer-list which I define programmatically because one of the images is dynamic, the play button is static of course. I want to align the play button 10px from the bottom, and centered horizontally.

我的ImageView的定义如下(以XML)

My ImageView is defined like this (in xml)

<ImageView 
          android:id="@+id/EvidencePreview"      
          android:layout_width="267dp"
          android:layout_height="201dp"
          android:scaleType="centerCrop"
          android:layout_margin="3dp"
          android:layout_gravity="center_horizontal|center_vertical"/>

ImageView的是,用户可以编辑标题等信息的表格的一部分。 然后在我的活动我现在创建图层列表如下:

The ImageView is part of a form where the user can edit title and other information. Then in my activity now I create the layer list like this:

Resources resources = mContext.getResources();
Drawable playOverlayDrawable = resources.getDrawable(R.drawable.play_overlay_large);                        
Drawable[] layers = new Drawable[2];
layers[0] = Drawable.createFromPath(tb.filePath);
layers[1] = playOverlayDrawable;
LayerDrawable layerDrawable = new LayerDrawable(layers);
ViewGroup.LayoutParams lp = iv.getLayoutParams();

int imageHeight = lp.height;
int imageWidth = lp.width;
int overlayHeight = layers[1].getIntrinsicHeight();
int overlayWidth = layers[1].getIntrinsicWidth();
int lR = (imageWidth - overlayWidth) / 2;
int top = imageHeight - (overlayHeight + 10);
int bottom = 10;
layerDrawable.setLayerInset(1, lR, top, lR, bottom);
iv.setImageDrawable(layerDrawable);

此只能当图像的方向是水平的。 请记住,图像/缩略图是MINI_KIND这意味着它应该是512×384,但我看到的,它实际上不是大小..我的手机上,他们要么是480×800 800×480或取决于方向的摄像头在.. 由于我的布局宽度/高度pre定义我只是想办法让播放按钮层则可以完全缩放和对齐方式相同,每次..

This only works when the orientation of the image is horizontal. Keep in mind that the image/thumbnail is the MINI_KIND which means its supposed to be 512 x 384 but I'm seeing that it actually isn't the size.. On my phone they are either 480x800 or 800x480 depending on the orientation the camera was in.. Since my layout width/height is pre defined I just want a way to keep the play button layer from scaling at all and align it the same way everytime..

另外明显的方式做到这一点是使用一个相对布局(或者一个框架布局?),但我希望避免这种情况,因为我使用的是相同的code,用于显示图像和视频(这两者有一个图像的缩略图preVIEW - 但只有影片应该对他们的播放按钮)

The other obvious way to do this would be to use a relative layout (or perhaps a frame layout?) but I was hoping to avoid that since I'm using the same code for displaying both images and videos (both of which have an image thumbnail preview -- but only videos should have the play button on them).

不知道如何用图层列表对准层,或替代方案,将工作一样好或更好?

Any idea how to align the layers with a layer list, or alternatives that would work just as well or better?

推荐答案

我结束了使用的FrameLayout是这样的:

I ended up using a FrameLayout like this:

<FrameLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="3dp"
    android:layout_gravity="center_horizontal|center_vertical" >

<ImageView 
    android:id="@+id/MediaPreview"      
    android:layout_width="267dp"
    android:layout_height="201dp"
    android:scaleType="centerCrop"
    android:src="@drawable/loading" />

<ImageView
    android:id="@+id/VideoPreviewPlayButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:layout_gravity="center_horizontal|bottom"
    android:visibility="gone"
    android:src="@drawable/play_overlay_large" />

</FrameLayout>

然后,我刚才设置的知名度View.VISIBLE到preVIEW播放按钮的视频,并留下了它的照片。 对于我的列表视图我上面使用,因为所有的缩略图是相同的尺寸显示的图层列表的方法。希望这可以帮助别人!

Then I just set the visibility to View.VISIBLE to the preview play button for videos and left it gone for photos. For my list view I used the layer list method shown above because all of the thumbnails were the same dimension. Hope this helps someone else!

请注意,在id为视频previewPlayButton ImageView的的layout_gravity把它在底部居中水平,而10dp paddingBottom会移了位从底部。

notice that the layout_gravity on the ImageView with id VideoPreviewPlayButton puts it at the bottom centered horizontally, and the 10dp paddingBottom moves it up a bit from the bottom.

 
精彩推荐