Android:在 FrameLayout 内翻译动画后无法单击底部 TextView
我有一个 FrameLayout,其中放置了两个相同的 TextView。 我希望能够将第一个视图平移到左侧(我已经完成了并且工作得非常顺利)。但是我希望能够单击其下方的 TextView 来执行操作。
当我尝试单击底部 TextView 时,顶部 TextView 会再次被单击。我有一种感觉,这是因为动画的渲染方式和实际 x,y 位置的变化没有生效。
这是我到目前为止所拥有的。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="50dip"
android:text="@string/hello"
android:id="@+id/unbutt"
android:gravity="right|center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="50dip"
android:text="@string/hello"
android:id="@+id/butt" />
</FrameLayout>
代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
public class Main extends Activity implements AnimationListener, OnClickListener
{
/** Called when the activity is first created. */
private class BottomViewClick implements OnClickListener
{
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Second Click", 5).show();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.butt);
tv.setBackgroundColor(0xffb71700);
tv.setOnClickListener(this);
TextView tv2 = (TextView)findViewById(R.id.unbutt);
tv2.setBackgroundColor(0xffb700ff);
tv2.setOnClickListener(new BottomViewClick());
}
private boolean revealed = false;
@Override
public void onClick(View v) {
Animation a ;
if(!revealed)
a = new TranslateAnimation(0f, -200f, 0f, 0f);
else
a = new TranslateAnimation(-200f, 0f, 0f, 0f);
a.setDuration(500);
a.setFillAfter(true);
a.setAnimationListener(this);
v.startAnimation(a);
}
@Override
public void onAnimationEnd(Animation animation) {
if(revealed)
revealed = false;
else
revealed = true;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
}
I have a FrameLayout in which I have placed two identical TextViews.
I want to be able to translate the first view to the left (which I have done and is working like a charm). However I want to be able to click the TextView underneath it to perform an action.
When I try to click the bottom TextView, the top TextView gets clicked again instead. I have a feeling this is because the way animations are rendered and the change in actual x,y position doesn't take effect.
This is what I have so far.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="50dip"
android:text="@string/hello"
android:id="@+id/unbutt"
android:gravity="right|center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="50dip"
android:text="@string/hello"
android:id="@+id/butt" />
</FrameLayout>
The code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
public class Main extends Activity implements AnimationListener, OnClickListener
{
/** Called when the activity is first created. */
private class BottomViewClick implements OnClickListener
{
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Second Click", 5).show();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.butt);
tv.setBackgroundColor(0xffb71700);
tv.setOnClickListener(this);
TextView tv2 = (TextView)findViewById(R.id.unbutt);
tv2.setBackgroundColor(0xffb700ff);
tv2.setOnClickListener(new BottomViewClick());
}
private boolean revealed = false;
@Override
public void onClick(View v) {
Animation a ;
if(!revealed)
a = new TranslateAnimation(0f, -200f, 0f, 0f);
else
a = new TranslateAnimation(-200f, 0f, 0f, 0f);
a.setDuration(500);
a.setFillAfter(true);
a.setAnimationListener(this);
v.startAnimation(a);
}
@Override
public void onAnimationEnd(Animation animation) {
if(revealed)
revealed = false;
else
revealed = true;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的标签中有 ICS,所以我认为这就是您的目标。在这种情况下,您正在使用的
Animation
对象基本上已被弃用,取而代之的是 动画师类。旧的方法仅移动View
的视觉位置,而物理位置保持不变。您必须通过操纵视图的边缘来自行移动它。另一方面,使用ObjectAnimator
允许您物理移动对象及其可视组件。http://android-developers.blogspot.com/2011/05/introducing -viewpropertyanimator.html
You have ICS in the tag, so I assume that's what your target is. In that case, the
Animation
objects you're using are essentially deprecated in favor of the Animator class. The old way of doing it only moves the visual location of theView
while the physical location remains the same. You would have to move it yourself by manipulating the margins of the view. Using anObjectAnimator
on the other hand allows you to physically move the object along with it's visual component.http://android-developers.blogspot.com/2011/05/introducing-viewpropertyanimator.html