简体   繁体   English

Android-向图像视图添加自定义边框

[英]Android - adding custom border to image view

I need to have an image view in my app that has a border something like the follows : 我需要在我的应用程序中具有边框的图像视图,如下所示:

在此处输入图片说明

I only have the image for the border, and the imageview, and somehow need to fit the imageview inside the border (the border image is transparent - like the one used in this question is downloaded from here ). 我只有边框的图像和imageview,并且某种程度上需要将imageview放在边框内(边框图像是透明的-就像从这里下载此问题中使用的图像一样)。

Any suggestions on how to achieve this ? 关于如何实现这一目标的任何建议?

There is many solutions that you can do. 您可以执行许多解决方案。

  • Extend an ImageView Class and Override OnDraw(Canvas) and Draw your Image on the Canvas of the ImageView by using paint and shader and provide the size of the rect to draw over then Draw the Frame. 扩展ImageView类并覆盖OnDraw(Canvas)并通过使用绘画和着色器在ImageView的Canvas上绘制图像,并提供要绘制的rect的大小,然后绘制Frame。

  • Set Background of ImageView as Frame and Ur Image as Src giving the a padding that will make the Src Image fit inside the Transparent Area. 将ImageView的Background设置为Frame,将Ur Image设置为Src,并指定一个填充,使Src图像适合透明区域。

Try this, save it as "frame.9.png" as 9patch, drop it to drawables folder: 尝试此操作,将其另存为“ frame.9.png”作为9patch,并将其拖放到drawables文件夹中:

在此处输入图片说明

Exampe pic in frame, it should be bigger, example: 示例框架中的图片,它应该更大,例如:

在此处输入图片说明

Example as layout: 布局示例:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/frame">

    <ImageView
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/cat"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        />
</LinearLayout>

Result: 结果:

在此处输入图片说明

Remember, 9-patch cant shrink they can stretch. 请记住,9补丁不能收缩,它们可以拉伸。 Info here: https://stackoverflow.com/a/14274623/619673 此处的信息: https : //stackoverflow.com/a/14274623/619673

Cat image was downloaded from http://www.socwall.com/wallpapers/search:cat/ Cat图片是从http://www.socwall.com/wallpapers/search:cat/下载的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM