透明覆盖在ImageView上的视图

11

我卡在那里了。我正在尝试在背景上放置一个透明的视图。我已经尝试了几种方法。

通过XML:

android:background="@color/transparent"
或者
android:color="#80000000"  
将color.xml文件作为引用放置即可。
<resources>
    <color name="transp">#80000000</color>
</resources>

我的layout.xml文件内容如下:

android:background="@color/transp"

我也尝试过使用生成的代码来完成它

myView.getBackground().setAlpha(45);
或者
myViewm.setBackgroundResource(R.color.trans);

我看到了一些相关的帖子,但是没有一个答案有效。

更奇怪的是,所有这些解决方案似乎在Eclipse的GraphicalLayout上都能正常工作。但是当我启动我的设备时,屏幕仍然不透明。我在那个视图上画了一条线来确保发生了某些事情;并且这条线确实显示出来了。

这是我的layout.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

      <ImageView
        android:id="@+id/backgroundview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/space_bg"
        android:contentDescription="@string/desc" />

      <View 
        android:id="@+id/tileview"        
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/><!-- 
        android:background="@color/transp"/>-->

</RelativeLayout>

我的代码

private ImageView bg;
    MyView tV;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        tV = new MyView(this);

setContentView(tV);

and the myView onDraw

@Override
    protected void onDraw(Canvas canvas)    
    {

        super.onDraw(canvas);
        this.setBackgroundResource(R.color.transp);
        canvas.drawLine(10,20,30,40, paint);

    }

那么,我错在哪里了呢? 谢谢!!!

4个回答

13
android:background="@color/transparent"

你可以使用Android资源提供的transparent颜色:
android:background="@android:color/transparent"

android:color="#80000000"  

<resources>
    <color name="transp">#80000000</color>
</resources>

myViewm.setBackgroundResource(R.color.trans);

这将给你一个非常深的灰色。alpha值为80时最多是半透明的。


myView.getBackground().setAlpha(45);

你可能没有正确使用它。
private ImageView bg;
MyView tV;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                 WindowManager.LayoutParams.FLAG_FULLSCREEN);

    tV = new MyView(this);

    setContentView(tV);
}

这基本上是用MyView替换了从R.layout.activity_main(包含ImageView和其他小部件)填充的View。我认为这不是你想要的。请尝试以下操作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/backgroundview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/space_bg"
        android:contentDescription="@string/desc" />

    <RelativeLayout 
        android:id="@+id/tileview"        
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/some_drawable_smaller_than_screen_size"
            android:layout_centerInParent="true" />

    </RelativeLayout>

</RelativeLayout>

如何解析该 XML:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                 WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);
}

id="@+id/tileview"将是透明的并且覆盖在id="@+id/backgroundview"之上。


2
你真是个慷慨的天才!!!这玩意儿终于能用了!我的错误是我想把一个视图放在一个imageView上?为什么不行呢?现在,如果我想通过代码画一些线条在那个视图上,我可以做到吗? - Antoine
@Antoine,很高兴它能够正常工作。如果我想在该视图上绘制一些线条,我能通过代码实现吗?你需要更具体一些。你是想在ImageView上预定义一些线条(即:它们在活动开始时出现)吗?还是当你的应用程序运行时使用触摸事件自由绘制? - Vikram
1
抱歉,我的表述可能有些含糊。我想在活动开始时绘制一些线条,实际上是带有一些事件监听器的图块。还有,能否解释一下为什么我的解决方案不起作用? - Antoine
@Antoine 请查看我在这里的答案:Technique to make a canvas drawLine() clickable?。你需要做一些更改:例如,你想让瓷砖可点击。为此,linePath将包含4组点(point1X,point1Y,point2X,point2Y,point3X,point3Y,point4X,point4Y)。而rectF将包围这些点内的区域。还要考虑发布一个新问题。我们在这个问题上的讨论与发布的问题无关。 - Vikram
@Vikram,我有一个类似的问题。我使用了你的代码,但没有帮助到我。你能否看一下这个问题?http://stackoverflow.com/questions/21414119/fadded-edge-to-imageview-in-android?noredirect=1#comment32305886_21414119 - Manikandan
@Manikandan 你好。我已经回答了你的问题,希望能对你有所帮助。链接 - Vikram

8

android:alpha="0.5"

0.0为完全透明,0.5为中等透明度,1.0为完全不透明。 这里TextView是透明的。

输入图片说明.输入图片说明

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageview_id"
        android:layout_width="match_parent"[![enter image description here][2]][2]
        android:layout_height="130dp"
        android:scaleType="centerCrop"
        android:src="@drawable/img3" />

    <TextView
        android:id="@+id/home_tvshow_textView_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:alpha="0.5"
        android:background="@color/red"
        android:gravity="center"
        android:text="Your String"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_17"
        android:layout_marginTop="@dimen/dp_110"/>


</RelativeLayout>

这是添加半透明叠加视图最简单的方法。谢谢。 - صلي علي محمد - Atef Farouk

1
  • 使用手动视图实现

如果您在布局中使用相对布局作为父标签。

您的布局应该像这样...

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
.
.
.
    <View
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#EBE9E9"
       android:id="@+id/hide_view"/>
</RelativeLayout>

在你的活动中
hide_view = findViewById(R.id.hide_view);
hide_view.setAlpha(0.5f);

0

针对您的小部件进行设置

android:color="#80000000"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接