【问题标题】:I want to create an animated image ( like i am making a plus sign by hand)我想创建一个动画图像(就像我正在手工制作一个加号)
【发布时间】:2013-09-12 17:20:18
【问题描述】:

我想创建一个动画图像(就像我正在手工制作一个加号),然后在我点击它后放在我的 android 图像按钮上。如何创建这样的动画图像?并将其放在android中的按钮上?

【问题讨论】:

  • 你能发布一些代码来展示你已经尝试过的东西或链接到一些展示你的研究的网站吗?

标签: android image animation button


【解决方案1】:
  1. 为按钮状态准备3张图片,并将其放入“resource/drawable”文件夹中。

    button_normal_green.png – 默认图像按钮。

    button_focused_orange.png – 当按钮 focused 时显示,例如,当手机的键盘在此按钮上移动(聚焦)时。

    button_pressed_yellow.png - 当按钮按下时显示。

  2. 为不同的按钮状态添加选择器

    现在,在“res/drawable/” 文件夹中创建一个新的 XML 文件,使用您想要的任何名称,在这种情况下,我们只需将名称命名为 “new_button.xml“。该文件定义了哪个按钮状态属于哪个图像。

    现在,您可以通过此 ID 引用此按钮:@drawable/new_button

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed_yellow"
      android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused_orange"
      android:state_focused="true" />
    <item android:drawable="@drawable/button_normal_green" />
    </selector>
    
  3. 添加按钮

    打开“res/layout/main.xml”文件,添加普通按钮,通过“android:background="@drawable/new_button”将背景图片附加到“new_button”上面

文件:res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/imageButtonSelector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/new_button" />
</LinearLayout>

根据您需要的信息,您可以在带有Frameanimation 的按钮中设置一堆带有+号的图像和空白图像,您可以在 ImageButtonclickEvent 上调用它们

  imgButton = (ImageButton) findViewById(R.id.imageButtonid);
    imgButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            animate();
        }
    });

private void animate() {
    imageButton = (ImageButton) findViewById(R.id.imageButtonid);
    imageButton.setVisibility(ImageView.VISIBLE);
    imageButton.setBackgroundResource(R.anim.simple_animation);
    AnimationDrawable frameAnimation = (AnimationDrawable) imgView
            .getBackground();
    frameAnimation.start();
    frameAnimation.setOneShot(true);
}

添加的文件:res\anim\simple_animation

  <?xml version="1.0" encoding="utf-8"?>
  <animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">
 <item android:drawable="@drawable/monkey_1" android:duration="50" />
 <item android:drawable="@drawable/monkey_2" android:duration="50" />
 <item android:drawable="@drawable/monkey_3" android:duration="50" />
 <item android:drawable="@drawable/monkey_4" android:duration="50" />
 <item android:drawable="@drawable/monkey_5" android:duration="50" />
 <item android:drawable="@drawable/monkey_6" android:duration="50" />
  </animation-list>

【讨论】:

  • 感谢此信息很有帮助,但我的基本查询是我想要一个 gif 类型的图像/绘图,它将显示为正在创建的加号。如何使用位图或 OpenGL 或 Photoshop 创建这样的图像/绘图?哪种文件格式?
  • @user2773642 :查看修改后的代码。尝试使用FrameAnimation 为按钮设置动画。
猜你喜欢
  • 1970-01-01
  • 2011-12-15
  • 2020-09-08
  • 2013-10-02
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 2017-07-28
  • 2013-08-03
相关资源
最近更新 更多