【问题标题】:How do i keep image on transparent background?如何将图像保持在透明背景上?
【发布时间】:2018-09-21 20:46:30
【问题描述】:

I want to acheive the following. Transparent background with white tick 我如何在透明背景上获得白色刻度线。 我尝试了以下方法。

viewprev = mgridview.getChildAt(i - mgridview.getFirstVisiblePosition());
                viewprev.setBackgroundColor(Color.WHITE);
                viewprev.setAlpha(0.5f);

【问题讨论】:

  • 你在说什么?
  • viewprev.setBackgroundColor(Color.TRANSPARENT);

标签: android image android-layout android-studio gridview


【解决方案1】:

试试这个,

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/ivMain"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:contentDescription="@string/app_name"
    android:src="@mipmap/ic_launcher"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<FrameLayout
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:background="#99676767"
    app:layout_constraintBottom_toBottomOf="@id/ivMain"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@id/ivMain">

    <ImageView
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:contentDescription="@string/app_name"
        android:src="@drawable/vector_favorite" />
</FrameLayout>

您的刻度应该在FrameLayout 中,它应该像在代码中一样受到限制,因此它始终位于ImageView 的中间。从图像中,我猜您正在尝试创建长按来选择物品之类的东西。如果是这样,只需在 xml 中将 FrameLayout 的可见性设置为 gone 并在 LongClick 事件中将其设置为 visible

这是上面代码的输出:

您可以随意调整颜色以达到所需的透明度水平

【讨论】:

    【解决方案2】:

    您可能需要使用 FrameLayout。Framelayout 包含您的图像和其上方的空 ImageView。当您需要绘制刻度时 - 只需将其图像设置到该 ImageView 中即可。

    【讨论】:

      【解决方案3】:

      首先,您需要将ImageView 添加到您的网格项目布局中。请记住在主网格图像下方插入 xml 标记以显示在顶部。然后使用match_parent 作为图像视图的宽度和高度,并将scaleType 分配到中心。这是您的复选标记视图。您可以添加半透明背景以在网格项目图像上方形成阴影,并且src 属性将进入复选标记。

      您还可以修改复选标记项的图像视图可见性切换隐藏和显示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-21
        • 2014-10-05
        • 2010-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-08
        相关资源
        最近更新 更多