按键响应的位置不是在Android版本的动画后更改低于3.0 [英] Button response position not changed after animation in Android version lower than 3.0

查看:102
本文介绍了按键响应的位置不是在Android版本的动画后更改低于3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法

使用补间动画视图

预期结果

有一个按钮,放在现场 A ,一旦点击,它移动向右现货 B 。然后我点击当场 B 这个按钮,它向左移动,现货 A

问题

现货首先单击 A ,按钮都移到现货 B 。然后,点击点 B 带来了无反应,相反,它会以点击在现场总是响应 A

Main.java

 包com.example.animationdemo;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.animation.Animation;
进口android.view.animation.AnimationUtils;
进口android.widget.Button;公共类主要活动扩展{
    / **当第一次创建活动调用。 * /    布尔flagToggleButton = FALSE;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        最后键b =(按钮)findViewById(R.id.button1);
        b.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(查看为arg0){
                如果(flagToggleButton == FALSE){
                    动画动画= AnimationUtils.loadAnimation(Main.this,R.anim.animation_move_right);
                    b.startAnimation(阿尼姆);
                    flagToggleButton = TRUE;
                }
                其他{
                    动画动画= AnimationUtils.loadAnimation(Main.this,R.anim.animation_move_left);
                    b.startAnimation(阿尼姆);
                    flagToggleButton = FALSE;
                }
            }
        });
    }
}

anim_move_right.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<集
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:插值=@机器人:动画/ accelerate_decelerate_interpolator
    机器人:fillEnabled =真
    机器人:fillAfter =真正的>
    <翻译
        机器人:时间=1000
        机器人:fromXDelta =0%
        机器人:toXDelta =200%
        机器人:startOffset =0/>
< /集>

anim_move_left.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<集
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:插值=@机器人:动画/ accelerate_decelerate_interpolator
    机器人:fillEnabled =真
    机器人:fillAfter =真正的>
    <翻译
        机器人:时间=1000
        机器人:fromXDelta =0%
        机器人:toXDelta = - 200%
        机器人:startOffset =0/>
< /集>


解决方案

这是如何 TranslateAnimation 工作,它不动,以新的位置,你必须设置在的LayoutParams 按钮所以它实际上没有移动到新位置时,检查的链接如何移动视图到新的位置的例子

Method

Using tweened view animation.

Expected Result

A button, placed on spot A, once on clicked, it moves rightward to spot B. Then I click that button on the spot B, it move leftward to spot A.

Problem

First clicking on spot A, the button did move to spot B. Then a click at spot B brings up no response, instead, it will respond always to clicking at spot A.

Main.java

package com.example.animationdemo;

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.AnimationUtils;
import android.widget.Button;

public class Main extends Activity {
    /** Called when the activity is first created. */

    boolean flagToggleButton = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener () {
            @Override
            public void onClick(View arg0) {
                if (flagToggleButton == false) {
                    Animation anim = AnimationUtils.loadAnimation(Main.this, R.anim.animation_move_right);
                    b.startAnimation(anim);
                    flagToggleButton = true;
                }
                else {
                    Animation anim = AnimationUtils.loadAnimation(Main.this, R.anim.animation_move_left);
                    b.startAnimation(anim);
                    flagToggleButton = false;
                }
            }
        });
    }
}

anim_move_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fillEnabled="true"
    android:fillAfter="true" >
    <translate
        android:duration="1000"
        android:fromXDelta="0%"
        android:toXDelta="200%"
        android:startOffset="0" />
</set>

anim_move_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fillEnabled="true"
    android:fillAfter="true" >
    <translate
        android:duration="1000"
        android:fromXDelta="0%"
        android:toXDelta="-200%"
        android:startOffset="0" />
</set>

解决方案

This is how TranslateAnimation works, it doesn't move the view to new position you have to set the LayoutParams for your Button so it actually does move to new position, check This link for an example of how to move the view to new position

这篇关于按键响应的位置不是在Android版本的动画后更改低于3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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