【问题标题】:Android. How to add transparent image over another image安卓。如何在另一个图像上添加透明图像
【发布时间】:2013-02-05 22:15:01
【问题描述】:

我知道如何通过surfaceView 做到这一点,我想知道我是否应该走那条路。

我只是想创建一个启动画面,它有一个全屏图像,顶部有一个不透明的图像(经过短暂的延迟)。我无法弄清楚这是如何在 XML 代码中完成的。

这就是我所拥有的......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<ImageView
android:id="@+id/lightDot"
android:src="@drawable/splashlight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
 />

<ImageView
android:id="@+id/bGround"
android:src="@drawable/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
 />

所以我的“lightDot”对象是半透明的,我想在短暂的延迟后将它覆盖在我的 bGround 资源之上。

这是我的代码:

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class SplashAct extends Activity implements OnClickListener{

Button mainButton;
Button lightDot;
ImageView background;
ImageView light;

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

    background = (ImageView)findViewById(R.id.bGround); 
    light = (ImageView)findViewById(R.id.lightDot); 

    background.setOnClickListener(this);
}


@Override
public void onClick(View arg0) {
    //finish();
    Intent toMainGame = new Intent(this, ActOptions.class);
    startActivity(toMainGame);
    }



}

谢谢。

【问题讨论】:

  • 谢谢大家的回答,这里有一些好主意,最后,虽然我决定用代码而不是 XML 来做这一切 - 它看起来容易多了!谢谢

标签: android xml splash-screen


【解决方案1】:

你想要的是FrameLayout。

子视图在堆栈中绘制,最近添加的子视图在顶部。

您可以使用layout_gravity 更了解叠加层的位置,但听起来这就是您所需要的。

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/image" >
        </ImageView>

        <ImageView
            android:id="@+id/overlay"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/overlay"/>
    </FrameLayout>

【讨论】:

    【解决方案2】:

    使用相对布局而不是线性布局。

    这允许您将视图彼此叠加,而不是在线性列表中。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
    
      <ImageView
        android:id="@+id/lightDot"
        android:src="@drawable/splashlight"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@null"
         />
    
      <ImageView
        android:id="@+id/bGround"
        android:src="@drawable/splash"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         />
    
    </RelativeLayout>
    

    要使点在时间之后出现,请在 xml 中使用 android:visiblity="INVISIBLE"。 (所以它开始不可见)

    然后在您的代码中使用light.setVisiblity(View.VISIBLE);。

    【讨论】:

    • 尝试重新排序图像视图,或使用框架布局。它可能以错误的顺序设置在另一个之上。我想还有一个上面的标签。
    【解决方案3】:

    您可以使用LayerDrawable

    定义一个简单的线性布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/splash_layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    

    在您的 Activity 类中:

    LinearLayout layout = (LinearLayout) findViewById(R.id.splash_layout);
    Drawable drawableLayers[] = new Drawable[] {getResources().getDrawable(R.drawable.splash), getResources().getDrawable(R.drawable.splashlight)};
    // ^ The order of drawables is important: The above line overlays splashlight on top of splash.
    LayerDrawable layerDrawable = new LayerDrawable(drawableLayers);
    layout.setBackgroundDrawable(layerDrawable);
    

    【讨论】:

      【解决方案4】:
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:id="@+id/ll"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      ***android:background="@drawable/splash"*** >
      

      // Try this for invisible
      iv.getBackground().setAlpha(0);
      
      //Try this for visible 
      iv.getBackground().setAlpha(255);
      
      //What great about this is that you could create a loop 
      //to slowly increment the alpha, creating a fade effect instead of 
      //invisible to visible.
      

      【讨论】:

      • 谢谢,通过一些小的调整让它工作了 - 知道如何添加延迟吗?我已经在 try/catch 中尝试过 thread.sleep 但这似乎不起作用(将其放入 for 循环并在 for 循环中使用变量来获取不透明度,但是延迟发生在绘制任何内容之前而不是减慢动画速度 - 谢谢
      猜你喜欢
      • 2018-10-11
      • 1970-01-01
      • 2011-07-20
      • 2021-06-30
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      相关资源
      最近更新 更多