我正在尝试编写一个应用程序,以便在两个图像之间交叉淡入淡出,然后在单击时返回。
初始方法fade
可以正常工作并淡化到新图像,但是当单击第二个图像时,没有任何效果。
我认为问题是第一个图像仍然覆盖在第二个图像上,所以我只是一遍又一遍地点击第一个图像。
但是我添加了打印图像变量的hashcode()
以检查执行情况,并且它似乎每次仅从方法fadeBack
返回相同的变量。
我在日志(// n click on image n
)中注释了每次点击时出现的条目,但是当应该调用fadeBack
时,似乎调用相同的方法fade
。
需要更多信息,请告知我们。
日志:
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);
}
}
活动主要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>
答案 0 :(得分:0)
好的想通了。
作为drawable,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>