我在Gallery中使用了FrameLayout作为Gallery Item。 FrameLayout中有2个RelativeLayout,每个包含一些TextView和一个ImageView。画廊外面有一个名为“Flip”的按钮。单击“翻转”按钮会隐藏一个相对布局,并使用翻转动画显示另一个布局。在Gallery外面还有一个TextView,我可以在其中更新一些文本。当用户滚动图库以选择另一个项目时,我从Gallery的setOnItemSelected方法更新TextView。
问题是如果我不从setOnItemSelected方法更新TextView,则翻转动画可以正常工作。但是,如果我更新TextView,则动画不起作用。即使它没有在FrameLayout中切换RelativeLayout。
如果有人知道问题所在,请帮助我。
以下是主屏幕的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"
android:gravity="top|center" android:background="#0000">
<RelativeLayout android:id="@+id/RelativeLayout04" android:layout_height="fill_parent" android:layout_width="fill_parent"><RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/top_bar" android:layout_alignParentTop="true" android:id="@+id/headerRelativeLayout"><Button android:id="@+id/homeButton" android:layout_height="wrap_content" android:layout_marginTop="5dip" android:background="@drawable/home_button_open" android:layout_marginLeft="5dip" android:layout_width="wrap_content" android:layout_alignParentLeft="true"></Button>
<Button android:id="@+id/backButton" android:layout_height="wrap_content" android:layout_marginTop="5dip" android:background="@drawable/back_button" android:layout_toRightOf="@+id/homeButton" android:layout_width="wrap_content"></Button>
<TextView android:layout_height="wrap_content" android:textSize="22sp" android:layout_toLeftOf="@+id/LinearLayout02" android:gravity="center" android:layout_toRightOf="@+id/backButton" android:id="@+id/TextView02" android:layout_width="fill_parent" android:textStyle="bold" android:layout_centerInParent="true"></TextView>
<LinearLayout android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_centerVertical="true" android:layout_marginRight="10dip" android:id="@+id/LinearLayout02">
<com.vocabAhead.SATVocab.LoginButton android:layout_marginRight="5dip" android:id="@+id/facebookButton" android:layout_width="wrap_content" android:background="@drawable/facebook_button" android:layout_height="wrap_content"></com.vocabAhead.SATVocab.LoginButton>
<Button android:id="@+id/settingsButton" android:layout_width="wrap_content" android:background="@drawable/settings_button" android:layout_height="wrap_content" android:layout_gravity="center_vertical"></Button>
</LinearLayout>
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent" android:layout_height="35dip" android:background="#FF212121" android:layout_alignParentBottom="true" android:id="@+id/footerRelativeLayout">
<Button android:id="@+id/playButton" android:layout_height="wrap_content" android:background="@drawable/play_button" android:layout_marginLeft="10dip" android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true"></Button>
<Button android:layout_alignParentRight="true" android:id="@+id/showScriptButton" android:layout_height="wrap_content" android:background="@drawable/infobutton" android:layout_marginRight="10dip" android:layout_width="wrap_content" android:layout_centerVertical="true"></Button>
<TextView android:layout_height="wrap_content" android:textColor="#FFFF" android:id="@+id/itemSerialTextView" android:layout_width="wrap_content" android:layout_centerInParent="true"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5dip" android:layout_toRightOf="@+id/playButton" android:id="@+id/durationTextView" android:textColor="#FFFF"></TextView>
</RelativeLayout>
<Gallery android:id="@+id/wordsGallery" android:layout_above="@+id/footerRelativeLayout" android:layout_below="@+id/headerRelativeLayout" android:layout_height="fill_parent" android:layout_width="fill_parent" android:scrollbars="none" android:scrollbarSize="0dip" android:layout_margin="0dip" android:gravity="fill" android:padding="0dip" android:background="#0000"></Gallery>
</RelativeLayout>
</LinearLayout>
以下是Gallery项目的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_margin="0dip" android:padding="0dip">
<RelativeLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/wordDetailsRelativeLayout" android:background="#FFFF">
<TextView android:gravity="center" android:textSize="18sp" android:layout_alignParentTop="true" android:layout_width="fill_parent" android:text="Apathy" android:textStyle="bold" android:layout_height="wrap_content" android:layout_marginBottom="5dip" android:layout_marginTop="5dip" android:id="@+id/wordTextView" android:textColor="#FF1a3f6e"></TextView>
<TextView android:gravity="center" android:layout_width="fill_parent" android:text="verb" android:layout_height="wrap_content" android:id="@+id/partsOfSpeechTextView" android:layout_below="@+id/wordTextView" android:layout_marginBottom="5dip"></TextView>
<LinearLayout android:paddingBottom="5dip" android:layout_width="fill_parent" android:gravity="center" android:background="#FFFF" android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_alignParentBottom="true"><ImageView android:src="@drawable/iphone_vocabulary_logo_15" android:id="@+id/logoImageView" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
<LinearLayout android:layout_height="fill_parent" android:layout_marginBottom="5dip" android:layout_width="fill_parent" android:layout_below="@+id/partsOfSpeechTextView" android:orientation="vertical" android:id="@+id/imageAndMeaningLinearLayout" android:gravity="top|center_horizontal" android:layout_above="@+id/LinearLayout01"><ImageView android:id="@+id/wordImageView" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
<TextView android:layout_height="wrap_content" android:id="@+id/wordMeaningTextView" android:layout_width="wrap_content" android:textColor="#F000" android:textSize="15sp" android:layout_marginLeft="10dip" android:layout_marginTop="10dip" android:layout_marginRight="10dip" android:gravity="center"></TextView>
</LinearLayout>
</RelativeLayout>
<RelativeLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#FFFF" android:id="@+id/wordScriptRelativeLayout" android:visibility="gone">
<TextView android:gravity="center" android:textSize="18sp" android:layout_alignParentTop="true" android:layout_width="fill_parent" android:text="Apathy" android:textStyle="bold" android:layout_height="wrap_content" android:layout_marginBottom="5dip" android:layout_marginTop="5dip" android:textColor="#FF1a3f6e" android:id="@+id/wordTextView2"></TextView>
<TextView android:gravity="center" android:layout_width="fill_parent" android:text="verb" android:layout_height="wrap_content" android:id="@+id/partsOfSpeechTextView2" android:layout_below="@+id/wordTextView2" android:layout_marginBottom="5dip"></TextView>
<LinearLayout android:paddingBottom="5dip" android:layout_width="fill_parent" android:gravity="center" android:background="#FFFF" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:id="@+id/LinearLayout02"><ImageView android:src="@drawable/iphone_vocabulary_logo_15" android:id="@+id/logoImageView" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/wordScriptLinearLayout" android:gravity="top|center_horizontal" android:layout_below="@+id/partsOfSpeechTextView2" android:layout_above="@+id/LinearLayout02"><TextView android:layout_height="wrap_content" android:layout_margin="10dip" android:id="@+id/wordScriptTextView" android:layout_width="fill_parent" android:textColor="#F000" android:textSize="15sp"></TextView>
</LinearLayout>
</RelativeLayout>
</FrameLayout>
在主屏幕中,当用户单击showScriptButton(页脚的右键)时,framelayout的一个相对布局会切换到另一个。用翻转动画显示相对布局的切换。
现在,页脚中间有一个文本视图。如果我们从代码更改该文本视图的文本,那么即使框架布局不改变相对布局,翻转动画也不起作用。
这是Gallery的setOnItemSelectedListener方法:
wordsGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
durationTextView.setText("");
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
以下是playButton的方法:
private void playAudio() {
if(isPaused) {
myRefreshThread = new Thread(new secondCountDownRunner());
myRefreshThread.start();
audioPlayer.start();
} else {
audioPlayer = MediaPlayer.create(this, ApplicationCache.wordAudioList.get(word.wordText));
audioPlayer.setOnCompletionListener(this);
int totalDuration = audioPlayer.getDuration()/1000;
String durationText = "";
int min = totalDuration/60;
int seconds = totalDuration % 60;
if(min < 10)
durationText = "0";
durationText += min+":";
if(seconds < 10)
durationText += "0";
durationText += seconds;
System.out.println("Duration of audio:"+durationText);
durationTextView.setText(durationText);
myRefreshThread = new Thread(new secondCountDownRunner());
myRefreshThread.start();
audioPlayer.start();
}
isPaused = false;
isPlaying = true;
playButton.setBackgroundResource(R.drawable.pause_button);
}
这是secondCountDownRunner线程:
class secondCountDownRunner implements Runnable{
// @Override
public void run() {
while(!Thread.currentThread().isInterrupted()){
Message m = new Message();
m.what = 2;
audioPlayerHandler.sendMessage(m);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
这是audioPlayerHandler:
audioPlayerHandler = new Handler() {
@Override
public void handleMessage( Message msg )
{
if(msg.what == 1) {
if(isPlaying) {
if(wordsGallery.getSelectedItemPosition() < (ApplicationCache.dontKnowWords.size() - 1)) {
//currentPosition++;
//currentGalleryPosition++;
//isWordScriptShowing = false;
//updateWordDetails();
word = wordsList.get(wordsGallery.getSelectedItemPosition() + 1);
wordsGallery.setSelection(wordsGallery.getSelectedItemPosition() + 1, true);
playAudio();
}
}
} else if(msg.what == 2) {
if(isPlaying) {
int duration = audioPlayer.getDuration()/1000;
int currentPosition = audioPlayer.getCurrentPosition()/1000;
int timeLeft = duration - currentPosition;
if(timeLeft < 0)
timeLeft = 0;
String durationText = "";
int min = timeLeft/60;
int seconds = timeLeft % 60;
if(min < 10)
durationText = "0";
durationText += min+":";
if(seconds < 10)
durationText += "0";
durationText += seconds;
durationTextView.setText(durationText);
}
}
}
};
以下是单击showScriptButton时调用的方法:
public void flipView(int position) {
applyRotation(0, 90, position);
isFrontShowing[position] = !isFrontShowing[position];
}
private void applyRotation(float start, float end, int position) {
// Find the center of image
final float centerX, centerY;
if(isFrontShowing[position] == true) {
centerX = detailsLayout[position].getMeasuredWidth() / 2.0f;
centerY = detailsLayout[position].getMeasuredHeight() / 2.0f;
} else {
centerX = scriptLayout[position].getMeasuredWidth() / 2.0f;
centerY = scriptLayout[position].getMeasuredHeight() / 2.0f;
}
//System.out.println("center X:"+centerX+",Y:"+centerY);
// Create a new 3D rotation with the supplied parameter
// The animation listener is used to trigger the next animation
final Flip3dAnimation rotation =
new Flip3dAnimation(start, end, centerX, centerY);
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(isFrontShowing[position], detailsLayout[position], scriptLayout[position]));
if (isFrontShowing[position] == true)
{
//detailsLayout[position].requestFocus();
//detailsLayout[position].bringToFront();
detailsLayout[position].startAnimation(rotation);
} else {
//System.out.println("---Backward flipping started...");
//scriptLayout[position].requestFocus();
//scriptLayout[position].bringToFront();
scriptLayout[position].startAnimation(rotation);
}
}
这是Flip3dAnimation类:
import android.graphics.Camera;
import android.graphics.Matrix; import android.view.animation.Animation; import android.view.animation.Transformation;
公共类Flip3dAnimation extends Animation { 私人最终浮动mFromDegrees; 私人最终浮动mToDegrees; 私人最终浮动mCenterX; 私人最终浮动mCenterY; 私人相机mCamera;
public Flip3dAnimation(float fromDegrees, float toDegrees, float centerX,
float centerY) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees
+ ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
DisplayNextView类:
import android.view.animation.Animation;
import android.widget.RelativeLayout;
public final class DisplayNextView实现Animation.AnimationListener { private boolean mCurrentView; RelativeLayout layout1; RelativeLayout layout2;
public DisplayNextView(boolean currentView, RelativeLayout layout1,
RelativeLayout layout2) {
mCurrentView = currentView;
this.layout1 = layout1;
this.layout2 = layout2;
}
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
layout1.post(new SwapViews(mCurrentView, layout1, layout2));
}
public void onAnimationRepeat(Animation animation) {
}
}
SwapViews类:
import android.view.View;
import android.view.animation.Animation; import android.view.animation.DecelerateInterpolator; import android.view.animation.Animation.AnimationListener; import android.widget.RelativeLayout;
public final class SwapViews实现Runnable { private boolean mIsFirstView; RelativeLayout layout1; RelativeLayout layout2;
public SwapViews(boolean isFirstView, RelativeLayout layout1, RelativeLayout layout2) {
mIsFirstView = isFirstView;
this.layout1 = layout1;
this.layout2 = layout2;
}
public void run() {
final float centerX, centerY;
if(mIsFirstView) {
centerX = layout1.getWidth() / 2.0f;
centerY = layout1.getHeight() / 2.0f;
} else {
centerX = layout2.getWidth() / 2.0f;
centerY = layout2.getHeight() / 2.0f;
}
Flip3dAnimation rotation;
if (mIsFirstView == true) {
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);
layout2.requestFocus();
layout2.bringToFront();
rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
} else {
layout2.setVisibility(View.GONE);
layout1.setVisibility(View.VISIBLE);
layout1.requestFocus();
layout1.bringToFront();
rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
}
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
rotation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation arg0) {
WordDetailItemAdapter.notifyAdapter();
}
});
if (mIsFirstView == true) {
layout2.startAnimation(rotation);
} else {
layout1.startAnimation(rotation);
}
}
}
有没有人对这个问题有所了解。请帮忙。
答案 0 :(得分:1)
在您的主要布局中,为什么您将RelativeLayout04作为顶级LinearLayout的唯一子项?我认为你的布局的复杂性可能会对这个问题产生影响,所以我在这里提供一个更简单的替代方案,使用android:layout_weight
来使Gallery填充页眉或页脚未使用的空间。如果这有帮助,请告诉我。
<?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"
android:background="#0000"
>
<!-- Header -->
<RelativeLayout
android:id="@+id/headerRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
...
</RelativeLayout>
<!-- The gallery -->
<Gallery
android:id="@+id/wordsGallery"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
...
/>
<!-- Footer -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="35dip"
android:background="#FF212121"
android:layout_alignParentBottom="true"
>
...
</RelativeLayout>
</LinearLayout>