简体   繁体   English

Android 给 ImageView 添加边框图片

[英]Android add border image to ImageView

Is it possible to add a bitmap as border of an ImageView in android dynamically in Java?是否可以在Java中动态添加位图作为Android中ImageView的边框?

I have already tried the following:我已经尝试了以下方法:

Image view in XML and border as shape the result is wrong I want to add bitmap in border. XML中的图像视图和边框作为形状结果错误我想在边框中添加位图。

You have got multiple options:您有多种选择:

  1. Place an other View with the border (also imageview) above your Imageview.在 Imageview 上方放置一个带有边框(也是 imageview)的其他 View。 Thay might be the simples solution.这可能是最简单的解决方案。 Just add an other view to your xml and make sure they are overlapping.只需将其他视图添加到您的 xml 并确保它们重叠。 (Use for example a Relative or FrameLayout as container) (例如使用相对或 FrameLayout 作为容器)

  2. Use a Layer List and draw a shape above a Bitmap and add that to your ImageView使用图层列表并在位图上方绘制一个形状并将其添加到您的 ImageView

  3. Write a custom ImageView and use the Canvas to draw the Border in the overwritten onDraw method.写一个自定义的ImageView,在覆盖的onDraw方法中使用Canvas绘制Border。 Eg canvas.drawRect(...) its pretty straightforward.例如 canvas.drawRect(...) 它非常简单。

The simple way:简单的方法:

A drawable for ImageView 's attr that android:foreground android:foreground ImageView属性的drawable

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">


    <stroke android:width="1px" android:color="#ff0000" />

    <solid android:color="@android:color/transparent"/>

</shape>

Then XML ,然后XML

<ImageView
    android:id="@+id/unreserved_head_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foreground="@drawable/shape_border"
    />

The simplest solution I found recently.我最近找到的最简单的解决方案。 Try to use material theme in your app.尝试在您的应用中使用材料主题。 Use "Theme.MaterialComponents.Light"使用"Theme.MaterialComponents.Light"

Then use image view inside and material card view.然后使用里面的图像视图和材料卡视图。

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/white"
            app:cardCornerRadius="8dp"
            app:cardElevation="0dp"
            app:strokeColor="@color/colorPrimary"
            app:strokeWidth="1dp">
              
              <ImageView/>//define you image view hear
       
        </com.google.android.material.card.MaterialCardView>

With property stokeColor and strokeWidth you can have border on an image view and make it dynamic.使用属性stokeColorstrokeWidth您可以在图像视图上设置边框并使其动态化。

you can use 9 patch in andorid studio to make an image a border!您可以在andorid studio中使用9个补丁来使图像成为边框!

i was finding a solution but i did not find any so i skipped that part我正在寻找解决方案,但没有找到,所以我跳过了那部分

then i go to the google images of firebase assets and i accidentaly discovered that they use 9patch然后我转到了firebase资产的谷歌图片,我偶然发现他们使用了9patch

9patch 在行动

heres the link: https://developer.android.com/studio/write/draw9patch you just need to drag where the edges are继承人的链接: https : //developer.android.com/studio/write/draw9patch你只需要拖动边缘的位置

its just like boder edge in unity它就像统一的边界边缘

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

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