Android. How to add transparent image over another image

点点圈 提交于 2019-12-11 15:24:23

问题


I know how to do this via a surfaceView and I'm wondering if I should go down that rout.

I'm simply trying to create a splashscreen that has a fullscreen image with an opaque image laid over the top (after a short delay). I can't work out how this is done in XML code.

This is what I have......

<?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"
 />

So my 'lightDot' object is semi-transparent and I want to overlay this on top of my bGround resource after a short delay.

This is my code:

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);
    }



}

Thank you.


回答1:


What you want is a FrameLayout.

Child views are drawn in a stack, with the most recently added child on top.

You can get more fancy with where the overlays go using layout_gravity, but it sounds like this is all you need.

    <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:


Use Relative layout not linear layout.

This allows you to place views ontop of one another, as opposed to in a linear list.

<?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>

To make the Dot appear after time, use android:visiblity="INVISIBLE" in the xml. (so it starts invisible)

then use light.setVisiblity(View.VISIBLE); in your code.




回答3:


You could use a LayerDrawable

Define a simple LinearLayout

<?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"
>

In your Activity class:

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.


来源:https://stackoverflow.com/questions/14718028/android-how-to-add-transparent-image-over-another-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!