Android - 在图像/缩略图上覆盖播放按钮的最佳方式

发布于 2024-12-06 02:43:00 字数 1676 浏览 1 评论 0原文

我有一个 Android 应用程序,可以播放音频/视频并显示图片。 对于视频,我想在预览图像顶部以及列表视图中叠加一个播放按钮。 现在我的做法是使用 xml 中的 ImageView,然后可绘制对象是一个图层图层列表,我以编程方式定义它,因为其中一个图像是动态的,当然播放按钮是静态的。我想将播放按钮与底部对齐 10px,并水平居中。

我的 ImageView 是这样定义的(在 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 是表单的一部分,用户可以在其中编辑标题和其他信息。 然后在我的活动中,我创建如下所示的图层列表:

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 x 384,但我发现它实际上不是尺寸。在我的手机上,它们是 480x800 或 800x480,具体取决于相机的方向曾在.. 由于我的布局宽度/高度是预先定义的,我只是想要一种方法来阻止播放按钮层完全缩放并每次都以相同的方式对齐它。

另一个明显的方法是使用相对布局(或者也许框架布局?)但我希望避免这种情况,因为我使用相同的代码来显示图像和视频(两者都有图像缩略图预览 - 但只有视频应该有播放按钮)。

知道如何将图层与图层列表对齐,或者有同样效果或更好的替代方案吗?

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.

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"/>

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2024-12-13 02:43:00

我最终使用了这样的 FrameLayout:

<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 的可见性设置为视频的预览播放​​按钮,而将其保留为照片。
对于我的列表视图,我使用了上面显示的图层列表方法,因为所有缩略图都具有相同的尺寸。希望这对其他人有帮助!

请注意,ID 为 VideoPreviewPlayButton 的 ImageView 上的layout_gravity 将其置于水平居中的底部,并且 10dp paddingBottom 将其从底部向上移动了一点。

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>

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!

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.

墨落画卷 2024-12-13 02:43:00

考虑使用RelativeLayout作为视图的容器。它让您可以自由地放置子视图。您可以将按钮绑定到图像视图的右侧或顶部边缘。

Consider using RelativeLayout as the container for your views. It gives you freedom of placing child views. You could bind your button to the right or top edge of your imageview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文