gpt4 book ai didi

java - Android 上的交叉淡入淡出 ImageView 未重置 - 单击同一图像

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:17 26 4
gpt4 key购买 nike

我正在尝试编写一个应用程序来在两个图像之间交叉淡入淡出,然后在单击时返回。初始方法 fade 工作正常并淡入新图像,但是当单击第二个图像时,没有任何效果。我认为问题在于第一张图像仍然覆盖在第二张图像上,因此我只需一遍又一遍地单击第一张图像。不过,我添加了打印图像变量的 hashcode() 来检查执行情况,并且似乎每次都仅从方法 fadeBack 返回相同的变量。我已经在日志中注释了每次点击时出现的条目(//n click on image n),但当应该调用 fade 时,似乎正在调用相同的方法,fadeBack

需要更多信息,请告诉我。

日志:

12-29 19:02:22.273 15746-15746/? I/art: Not late-enabling -Xcheck:jni (already on)
12-29 19:02:22.566 15746-15746/com.example.richardcurteis.layoutdemo W/System: ClassLoader referenced unknown path: /data/app/com.example.richardcurteis.layoutdemo-2/lib/x86
12-29 19:02:22.842 15746-15757/com.example.richardcurteis.layoutdemo I/art: Background sticky concurrent mark sweep GC freed 22155(972KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 16MB/16MB, paused 14.211ms total 68.404ms
12-29 19:02:22.871 15746-15757/com.example.richardcurteis.layoutdemo W/art: Suspending all threads took: 29.427ms
12-29 19:02:22.905 15746-15757/com.example.richardcurteis.layoutdemo W/art: Suspending all threads took: 13.161ms
12-29 19:02:22.980 15746-15757/com.example.richardcurteis.layoutdemo I/art: Background partial concurrent mark sweep GC freed 130(10KB) AllocSpace objects, 0(0B) LOS objects, 19% free, 16MB/20MB, paused 16.186ms total 106.877ms
12-29 19:02:23.014 15746-15757/com.example.richardcurteis.layoutdemo W/art: Suspending all threads took: 33.481ms
12-29 19:02:23.018 15746-15767/com.example.richardcurteis.layoutdemo D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-29 19:02:23.138 15746-15767/com.example.richardcurteis.layoutdemo I/OpenGLRenderer: Initialized EGL, version 1.4
12-29 19:02:23.192 15746-15767/com.example.richardcurteis.layoutdemo W/EGL_emulation: eglSurfaceAttrib not implemented
12-29 19:02:23.192 15746-15767/com.example.richardcurteis.layoutdemo W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7a0a80, error=EGL_SUCCESS
12-29 19:02:27.146 15746-15746/com.example.richardcurteis.layoutdemo I/System.out: Hashcode in fadeBack - will: 176777760 //First click on image 1
12-29 19:02:27.146 15746-15746/com.example.richardcurteis.layoutdemo I/System.out: Hashcode in fadeBack - obama: 261627865 //First click on image 1
12-29 19:02:30.197 15746-15746/com.example.richardcurteis.layoutdemo I/System.out: Hashcode in fadeBack - will: 176777760 //Second click on image 2
12-29 19:02:30.197 15746-15746/com.example.richardcurteis.layoutdemo I/System.out: Hashcode in fadeBack - obama: 261627865 // //Second click on image 2

代码:

package com.example.richardcurteis.layoutdemo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

ImageView obama;
ImageView will;

public void fade(View view) {
System.out.println("Hashcode in fade - will: " + will.hashCode());
System.out.println("Hashcode in fade - obama: " + obama.hashCode());
obama.animate().alpha(0f).setDuration(2000);
will.animate().alpha(1f).setDuration(2000);

}

public void fadeBack(View view) {
System.out.println("Hashcode in fadeBack - will: " + will.hashCode());
System.out.println("Hashcode in fadeBack - obama: " + obama.hashCode());
will.animate().alpha(1f).setDuration(2000);
obama.animate().alpha(0f).setDuration(2000);

}

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

obama = (ImageView) findViewById(R.id.obama);
will = (ImageView) findViewById(R.id.will);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

Activity 主 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.richardcurteis.layoutdemo.MainActivity"
tools:showIn="@layout/activity_main"
android:clickable="false">

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

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/will"
android:layout_alignTop="@+id/obama"
android:layout_alignParentStart="true"
android:src="@drawable/will"
android:alpha="0"
android:onClick="fadeBack" />
</RelativeLayout>

最佳答案

好的,明白了。由于在设计中,will 位于 obama 之上,因此始终会调用 fadeBack 方法。更改了代码,以便交叉淡入淡出和返回包含在一个方法中,并用一个计数器来控制它的走向。

MainActivity.java

package com.example.richardcurteis.layoutdemo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

ImageView obama;
ImageView will;
int count;

public void fade(View view) {

if (count == 0) {
obama.animate().alpha(0f).setDuration(2000);
will.animate().alpha(1f).setDuration(2000);
count++;
} else {
will.animate().alpha(0f).setDuration(2000);
obama.animate().alpha(1f).setDuration(2000);
count = 0;
}

}

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

obama = (ImageView) findViewById(R.id.obama);
will = (ImageView) findViewById(R.id.will);
count = 0;

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.richardcurteis.layoutdemo.MainActivity"
tools:showIn="@layout/activity_main"
android:clickable="false">

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

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/will"
android:layout_alignTop="@+id/obama"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:src="@drawable/will"
android:onClick="fade"
android:alpha="0" />
</RelativeLayout>

关于java - Android 上的交叉淡入淡出 ImageView 未重置 - 单击同一图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34517452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com