1

我正在为我的班级做这个项目。
您可以单击 Bart Simpson 的图像,它会在 Homer Simpson 淡入时淡出。

我需要帮助的是可以点击 Homer 并返回 Bart。

我的代码是

活动_xml

   <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.eric.layoutdemo2.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/bart"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/bart"
        android:onClick="fade"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/homer"
        android:layout_alignTop="@+id/bart"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:alpha="0"
        android:src="@drawable/homer"
       />
     </RelativeLayout>

主要活动

    import android.support.v7.app.ActionBarActivity;
        import android.os.Bundle;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.ImageView;


   public class MainActivity extends ActionBarActivity {

       public void fade(View view) {

        ImageView bart = (ImageView) findViewById(R.id.bart);

        ImageView homer = (ImageView) findViewById(R.id.homer);


        bart.animate().alpha(0f).setDuration(2000);

        homer.animate().alpha(1f).setDuration(2000);
        bart.bringToFront();
    }

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




    }
4

4 回答 4

4

很简单,您必须为 homer 对象定义一个 OnClickListener,并在它触发时调用淡入淡出函数。

public class MainActivity extends ActionBarActivity implements View.OnClickListener{
    ImageView homer, bart;

    private void fade(ImageView v1, ImageView v2) {
        v1.animate().alpha(0f).setDuration(2000);
        v2.animate().alpha(1f).setDuration(2000);
        v2.bringToFront();
    }

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

        bart = (ImageView) findViewById(R.id.bart);
        homer = (ImageView) findViewById(R.id.homer);

        bart.setOnClickListener(this);
        homer.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.bart:
                fade(bart, homer);
                     break;

            case R.id.homer:
                fade(homer, bart);
                     break;
        }
    }
}
于 2016-08-12T04:13:13.847 回答
2

以下是执行交叉淡入淡出并再次单击返回的简单方法:

XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="top" />

    <ImageView
        android:id="@+id/bart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:onClick="fade"
        app:srcCompat="@drawable/bart"
        tools:layout_editor_absoluteX="105dp"
        tools:layout_editor_absoluteY="190dp" />
    <ImageView
        android:id="@+id/homer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0"
        app:srcCompat="@drawable/homer"
        tools:layout_editor_absoluteX="105dp"
        tools:layout_editor_absoluteY="190dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

主要的 :

public class MainActivity extends AppCompatActivity {

    public void fade(View view){
        ImageView bart = (ImageView) findViewById(R.id.bart);
        ImageView homer = (ImageView) findViewById(R.id.homer);
        if (bart.getAlpha()==1f) {
            bart.animate().alpha(0f).setDuration(4000);
            homer.animate().alpha(1f).setDuration(4000);
        }
        else{
            bart.animate().alpha(1f).setDuration(4000);
            homer.animate().alpha(0f).setDuration(4000);
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
于 2020-05-11T17:09:03.420 回答
1

首先在xml文件中添加bart和homer的ImageView,并分别设置它们的id为bart和homer。

然后在 XML 文件的同一个设计菜单中将 bart 的 alpha 值设置为 1, homer 的 alpha 设置为 0。

之后将此代码添加到活动的 java 文件中。

public void fade(View view)
{
    ImageView bart = (ImageView) findViewById(R.id.bart);
    ImageView homer = (ImageView) findViewById(R.id.homer);

    bart.animate().alpha(0f).setDuration(2000);
    homer.animate().alpha(1f).setDuration(2000);
}
于 2018-05-16T11:19:06.300 回答
1

这是一个更简单的方法。只需在本垒打上添加一个 onclick 函数并用于.bringToFront();将他带到 bart 的前面,这是因为尽管 homer 是可见的并且 bart 不是 bart 在他的前面,并且第二个函数不会被类似地执行。.bringToFront();在巴特上使用。我用过其他图片。这是xml。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.pembatamang.myapplication.MainActivity"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp">

    <ImageView
        android:id="@+id/eevee"
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:alpha="0"
        android:onClick="fade1"
        android:src="@drawable/evee"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/squirtle"
        android:layout_width="wrap_content"
        android:layout_height="495dp"
        android:onClick="fade"
        android:src="@drawable/squirtle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

这是java代码。

package com.example.pembatamang.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    public void fade(View view) {
        ImageView squirtle1 = (ImageView) findViewById(R.id.squirtle);
        ImageView eevee1 = (ImageView) findViewById(R.id.eevee);
        squirtle1.animate().alpha(0f).setDuration(2000);
        eevee1.animate().alpha(1f).setDuration(2000);
        eevee1.bringToFront();
    }

    public void fade1(View view) {
        ImageView squirtle1 = (ImageView) findViewById(R.id.squirtle);
        ImageView eevee1 = (ImageView) findViewById(R.id.eevee);
        eevee1.animate().alpha(0f).setDuration(2000);
        squirtle1.animate().alpha(1f).setDuration(2000);
        squirtle1.bringToFront();
    }


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


    }
}
于 2017-08-28T15:42:12.333 回答