Android add border image to ImageView

為{幸葍}努か 提交于 2019-12-11 12:06:07

问题


Is it possible to add a bitmap as border of an ImageView in android dynamically in Java?

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.


回答1:


You have got multiple options:

  1. Place an other View with the border (also imageview) above your Imageview. Thay might be the simples solution. Just add an other view to your xml and make sure they are overlapping. (Use for example a Relative or FrameLayout as container)

  2. Use a Layer List and draw a shape above a Bitmap and add that to your ImageView

  3. Write a custom ImageView and use the Canvas to draw the Border in the overwritten onDraw method. E.g. canvas.drawRect(...) its pretty straightforward.




回答2:


The simple way:

A drawable for ImageView's attr that android:foreground

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

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


来源:https://stackoverflow.com/questions/34268535/android-add-border-image-to-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!