【问题标题】:Android Button Scale AnimationAndroid 按钮缩放动画
【发布时间】:2017-03-09 03:56:50
【问题描述】:

我只想让我的图像按钮在应用程序启动时在第一个屏幕上执行缩放(反复小放大和缩小)动画,直到我按下它。那是因为这是应用程序的“主要”和最重要的按钮,我想引起用户的注意。我找到了一些教程,我已经走到了这一步:

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>

ma​​in_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:我是新手,这是我的第一个应用程序

【问题讨论】:

    标签: java android xml animation button


    【解决方案1】:

    首先,你必须初始化你的按钮

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

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

    【讨论】:

    • 查看我对 MainScreen.java 所做的更改。它给了我错误 (Button) findViewById(R.id.ImageButton);btn.startAnimation(scale);
    • 你必须导入按钮
    • (按钮) findViewById(R.id.ImageButton);应该进入 onCreate...
    • 首先感谢您的宝贵时间。你是唯一一个回答的人。请查看我所做的最新更改并阅读我为您留下的行中的 cmets。再次感谢您!
    • 看看我的代码!去掉动画比例前面的final。我很抱歉按钮的问题。你应该写(ImageButton)。和私有 ImageButton btn;
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多