android - 创建带有阴影的矢量可绘制对象以覆盖图像

标签 android android-vectordrawable

我希望实现一个看起来像“所需”图像的布局,作为初始应用程序状态的一部分,通知用户一些基本操作。现在我得到了“实际”图像。 enter image description here

我需要任何可绘制/图像的背景图像,该图像具有从左下角到右上角倾斜的覆盖层并且具有任何颜色。它还必须能够在任意数量的设备、手机或平板电脑上以相同的形状进行扩展,并支持回 SDK 16。

到目前为止,我一直在放弃使用矢量图像来创建倾斜可绘制图像,然后使用 FrameLayout 将其覆盖在背景图像之上以实现分层效果的想法。我不确定这是否是实现此目的的最佳方法,但我希望覆盖可绘制对象是矢量可绘制对象或 9-patch ,这样它就不会在更宽的布局上像素化。

layout/view_layout.xml

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/background_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/bg_image"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:layout_gravity="bottom"
        android:background="@drawable/overlay"/>

</FrameLayout>

drawable/overlay.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="48dp" android:height="36dp"
    android:viewportWidth="48" android:viewportHeight="36">

    <path
        android:fillColor="#fff"
        android:pathData="M48,0 L48,36 L0,36 L0,32 Z"/>

</vector>

如果有人知道如何向矢量可绘制对象添加阴影(我一直无法找到方法),或者如果有更好的方法,任何帮助或建议将不胜感激。谢谢!

最佳答案

我最终解决这个问题的方法是扩展ImageView类并添加自定义绘制逻辑来覆盖图像。它没有像我希望的那样使用 Vector Drawable,但它确实呈现了我所希望的确切效果。这是我的类(class)的基本大纲:

public class CoveredImageView extends ImageView {

    static float DENSITY = 1f;
    static final float SHADOW_DISTANCE = 10f;
    static final int SHADOW_COLOR = 0xAA000000;

    Path path;
    Paint paint;

    Point p1, p2, p3;

    public CoveredImageView(Context context) {
        this(context, null);
    }

    public CoveredImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CoveredImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    void init(@NonNull Context context) {
        DENSITY = context.getResources().getDisplayMetrics().density;

        path = new Path();
        paint = new Paint();
        p1 = new Point();
        p2 = new Point();
        p3 = new Point();

        // Required to make the ShadowLayer work properly
        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

    void updateDrawVariables() {
        int shadowSize = (int)(SHADOW_DISTANCE * DENSITY);

        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setShadowLayer(shadowSize, 0, -1, SHADOW_COLOR);

        // Offset the actual position by the shadow size so
        int left = 0 - shadowSize;
        int right = getMeasuredWidth() + shadowSize;
        int bottom = getMeasuredHeight();

        p1.set(left, bottom);
        p2.set(right, bottom - (int)(52 * DENSITY));
        p3.set(right, bottom);

        path.setFillType(Path.FillType.EVEN_ODD);
        path.moveTo(p1.x, p1.y);
        path.lineTo(p2.x, p2.y);
        path.lineTo(p3.x, p3.y);
        // Move the path shape down so that the shadow doesn't "fade" at the left and right edges
        path.lineTo(p3.x, p3.y + shadowSize);
        path.lineTo(p1.x, p1.y + shadowSize);
        path.close();
    }

    @Override
    public void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        // Update all the drawing variables if the layout values have changed
        if(changed) {
            updateDrawVariables();
        }
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // Paint the current path values that were set after onLayout()
        canvas.drawPath(path, paint);
    }
}

关于android - 创建带有阴影的矢量可绘制对象以覆盖图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42165130/

相关文章:

来自psd的Android Vector drawable预览为空

android - API 21+ : AppCompat v23. 2.0 上的模糊图像使用带有 srcCompat 的 VectorDrawables

android - Material Design 排版和 Snackbars

android - 如何取消阻止使用 adb shell pm block 命令阻止的应用程序?

java - 即使在调用中断方法后线程也不会中断

android - 试图了解需要将哪些 SHA-1(开发和发布)添加到 firebase android 项目

安卓工作室 1.5.1 : Could not find property 'vectorDrawables'

android - AnimatedVectorDrawable 作为窗口背景。可能吗?

android - 如何从 VectorDrawable 获取位图

android - 更改 list 中的 minSdkVersion 后按钮变形