Android按钮缩放动画

时间:2016-10-26 16:58:56

标签: java android xml animation button

我只是想让我的图像按钮在应用程序启动时,在第一个屏幕中执行缩放(稍微放大和缩小)动画,直到我按下它。这是因为这是主要的'和应用程序最重要的按钮,我想引起用户的注意。我找到了一些教程,并且达到了这个目的:

MainScreen.java

package com.example.konarx.a11042016;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class MainScreen extends AppCompatActivity {
private Button btn;
final Animation scale; //ERROR - Variable 'scale' might not have been initialized//

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);
    final scale = AnimationUtils.loadAnimation(this, R.anim.gps_button_animation); //ERROR - Unknown class: 'scale'//
    btn = (Button) findViewById(R.id.ImageButton); //ERROR - Unexpected cast to `Button`: layout tag was `ImageButton`//
    btn.startAnimation(scale); //I just want to do the animation without clicking it. Is that going to work?//
}

public void InfoActivity(View view) {
    Intent intent = new Intent(this, InfoActivity.class);
    startActivity(intent);
}
}

gps_button_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/linear_interpolator">
<scale
    android:fromXScale="1.0"
    android:toXScale="3.0"
    android:fromYScale="1.0"
    android:toYScale="3.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="500"
    android:repeatCount="1"
    android:repeatMode="reverse" />
</set>

main_activity.xlm中的按钮xml

<ImageButton
    android:id="@+id/ImageButton"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/button_image"
    android:layout_marginTop="15dp"
    />

请帮助:(

ps:我是新手,这是我的第一个应用

2 个答案:

答案 0 :(得分:3)

首先,您必须初始化按钮

btn = (ImageButton) findViewById(R.id.ImageButton);

现在,您需要在clickListener上设置此按钮。

btn.setOnClickListener(this);

添加到您的班级声明

    public class MainScreen extends AppCompatActivity implements View.OnClickListener {
       private ImageButton btn;
       private Animation scale;

最后要做的是添加clickListener并启动动画

        @Override
        public void onClick(View view) {
             btn.startAnimation(scale);
        }

    public class MainScreen extends AppCompatActivity implements View.OnClickListener {
    private Button btn;
    private Animation scale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);
        scale = AnimationUtils.loadAnimation(this, R.anim.gps_button_animation);
      btn = (Button) findViewById(R.id.ImageButton); 
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        btn.startAnimation(scale); //gives me error to scale//
    }

    public void InfoActivity(View view) {
        Intent intent = new Intent(this, InfoActivity.class);
        startActivity(intent);
    }
    }

希望能解决您的问题。如果您对此有疑问,请随时询问;)

答案 1 :(得分:0)