安卓:无法单击底部的TextView一个的FrameLayout内翻译后动画 [英] Android: unable to click the bottom TextView after translate animation within a FrameLayout

查看:311
本文介绍了安卓:无法单击底部的TextView一个的FrameLayout内翻译后动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我所放置两个相同TextViews一个的FrameLayout。
我希望能够翻译的第一个视图向左(我已经做了,并正在努力像一个魅力)。不过,我希望能够点击下面的TextView它来执行操作。

当我尝试单击底部的TextView,顶部的TextView被再次点击来代替。我有一种感觉,这是因为动画的方式呈现,并在实际的X的变化,Y位置不生效。

这是我迄今。

 <?XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =50dip
        机器人:文字=@字符串/你好
        机器人:ID =@ + ID / unbutt
        机器人:重力=右| center_vertical
        />
    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =50dip
        机器人:文字=@字符串/你好
        机器人:ID =@ + ID /对接/>< /&的FrameLayout GT;

在code:

 进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.animation.Animation;
进口android.view.animation.Animation.AnimationListener;
进口android.view.animation.TranslateAnimation;
进口android.widget.TextView;公共类主要活动扩展实现AnimationListener,OnClickListener
{
    / **当第一次创建活动调用。 * /    私有类BottomViewClick实现OnClickListener
{    @覆盖
    公共无效的onClick(视图v){
        Toast.makeText(v.getContext(),二按,5).​​show();
    }}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    TextView的电视=(的TextView)findViewById(R.id.butt);
    tv.setBackgroundColor(0xffb71700);
    tv.setOnClickListener(本);    TextView中TV2 =(的TextView)findViewById(R.id.unbutt);
    tv2.setBackgroundColor(0xffb700ff);
    tv2.setOnClickListener(新BottomViewClick());}    私人布尔透露= FALSE;    @覆盖
    公共无效的onClick(视图v){
        动画;
        如果(!透露)
            一个=新TranslateAnimation(0F,-200f,0F,0F);
        其他
            一个=新TranslateAnimation(-200f,0F,0F,0F);
        a.setDuration(500);
        a.setFillAfter(真);
        a.setAnimationListener(本);
        v.startAnimation(一);
    }    @覆盖
    公共无效onAnimationEnd(动画动画){
        如果(揭秘)
            透露= FALSE;
        其他
            透露= TRUE;
    }    @覆盖
    公共无效onAnimationRepeat(动画动画){
    }    @覆盖
    公共无效onAnimationStart(动画动画){
    }
}


解决方案

您必须在标签ICS,所以我想这就是你的目标是什么。在这种情况下,动画你使用是赞成的动画类。做旧的方式仅在物理位置保持不变移动查看的视觉位置。你将不得不自己动手通过操纵视图的边缘移动。使用 ObjectAnimator ,另一方面可以让你身体随着它的可视化组件移动的对象。

http://android-developers.blogspot.com/2011/05/introducing-viewpropertyanimator.html

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) {
    }
}

解决方案

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 the View while the physical location remains the same. You would have to move it yourself by manipulating the margins of the view. Using an ObjectAnimator 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

这篇关于安卓:无法单击底部的TextView一个的FrameLayout内翻译后动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆