簡體   English   中英

Android ImageView 投影

[英]Android ImageView drop shadow

我正在嘗試向 ImageView 添加投影。 另一個 Stackoverflow 答案似乎使用畫布和位圖等,比它需要的復雜得多。

在 iOS 上,我會做這樣的事情:

myImageView.layer.shadowColor = [UIColor redColor].CGColor;
myImageView.layer.shadowRadius = 5;
myImageView.layer.shadowOffset = CGRectMake(0, 5);

並且它會渲染一個陰影,無論陰影是應用於視圖、圖像還是文本。

我試圖在 Android 上做同樣的事情,但它只是拒絕工作:

birdImageView = new ImageView(context);
birdImageView.setImageResource(R.drawable.yellow_bird);

Paint paint = new Paint();
paint.setAntiAlias(true);


birdImageView.setLayerType(LAYER_TYPE_SOFTWARE, null);
paint.setShadowLayer(5, 0, 5, Color.argb(255, 255, 0, 0));
birdImageView.setLayerPaint(paint);

我的鳥圖像上根本沒有看到任何預期的紅色陰影。

難道我做錯了什么?

例子

假設我想要這樣的陰影:

投影示例

更新

我是否必須求助於 Android 5.0 和新的 Elevation api ( http://developer.android.com/training/material/shadows-clipping.html )?

但是如果要使用新的 API,那么根據目前的人口統計數據( http://www.droid-life.com/2016/02/02/android-distribution-february-2016/ ),超過 50% 的用戶不會能夠使用該應用程序。

T_T

在 android studio 中有構建drawable ,您可以使用它來將陰影應用於任何View 這類似於陰影。

android:background="@drawable/abc_menu_dropdown_panel_holo_light"

使用它您不能更改視圖的背景顏色及其邊框顏色。 如果您想要自己的自定義可繪制對象,請使用layer-list

custom_drop_shadow_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--the shadow comes from here-->
    <item
        android:bottom="0dp"
        android:drawable="@android:drawable/dialog_holo_light_frame"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">

    </item>

    <item
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <!--whatever you want in the background -->
        <shape android:shape="rectangle">
            <solid android:color="@android:color/red" />

        </shape>
    </item>
</layer-list>

並適用於您的視圖,如下所示

android:background="@drawable/custom_drop_shadow_drawable"

希望它會幫助你! 感謝Android 查看陰影

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM