Android - 在图像/缩略图上覆盖播放按钮的最佳方式
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终使用了这样的 FrameLayout:
然后我只需将 View.VISIBLE 的可见性设置为视频的预览播放按钮,而将其保留为照片。
对于我的列表视图,我使用了上面显示的图层列表方法,因为所有缩略图都具有相同的尺寸。希望这对其他人有帮助!
请注意,ID 为 VideoPreviewPlayButton 的 ImageView 上的layout_gravity 将其置于水平居中的底部,并且 10dp paddingBottom 将其从底部向上移动了一点。
I ended up using a FrameLayout like this:
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.
考虑使用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.