简体   繁体   中英

How to make the focus move with button?

I made a simple button moving animation. But after I finish the button translate animation, the button focus did not move with the button. The focus still stays in the original position.

How to make the focus move with button? My sdk api level is 15.

The code is like this:

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button mButton;

    private Animation mButtonAnimation;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.button1);
        mButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mButtonAnimation.setFillAfter(true);
                mButton.startAnimation(mButtonAnimation);
            }
        });

        mButtonAnimation = AnimationUtils.loadAnimation(this, R.anim.button_anim);
    }
}

layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

button_anim.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromYDelta="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toYDelta="100" />
</set>

尝试fillAfter = true ,请参见sdk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM