android - 在android中绘制和动画

标签 android graphics animation

首先,这更像是一个关于动画的问题,而不是一个具体的问题编码。我需要填写我一直在做的研究留下的粗线。

我正在尝试做的事情:

Activity 应该加载时只有一个切换按钮可见。触摸按钮时,png 将动画到相对于切换按钮的特定位置。此外,如果/当再次按下切换按钮时,另一个按钮将从屏幕外滑入并滑出。

我想不通的是:

我可以绘制图像,但只能通过 xml。创建 ImageView 并以编程方式设置背景什么都不做。

当我从 xml 绘制时,我无法在按我想要的切换按钮时移动图像。当我调用 getPositionOnScreen() 时,我得到一个空指针,即使编译器可以看到我指的是在 xml 和代码中描述的 ImageView 。

我调用 getPositionOnScreen 是因为通过 xml,我将图像定位在切换按钮的后面,这样在按下按钮并且图像开始移动之前它是不可见的。这个想法是,对于不同的屏幕尺寸,我直到运行时才知道 View 的确切位置。 getPostionOnScreen 允许我获取 imageview 的坐标,因此我知道它的位置。当我有一个开始位置时,可以告诉它从切换按钮后面在屏幕上“向上移动”,方法是简单地添加到 x 或 y 直到它到达我想要的位置。

这是绘制图像的代码(插入到 onCreate 方法中)。

image= (ImageView) new ImageView(this);
  image.setImageResource(R.drawable.pic); 

这是我用来制作动画的代码。单击按钮时,它会在监听器的 View 上调用此方法。

private void activate(){

  int loc[] = new int [2];
  image.getLocationOnScreen(loc);
  int fromXDelta = loc[0];
     int fromYDelta = loc[1];
  int toYDelta = fromYDelta;
     int toXDelta = fromXDelta - 30;

  TranslateAnimation translateAnimation = new TranslateAnimation
  (-fromXDelta, -toXDelta, -fromYDelta, -toYDelta);
     translateAnimation.setDuration(500);
     translateAnimation.setFillEnabled(true);

     image.startAnimation(translateAnimation);

 }

我很清楚这是非常错误的,不会起作用。我需要了解的是为什么。非常欢迎任何帮助。

最佳答案

好的第一点。如果您正在创建一个新的 ImageView以这种方式对象(通过使用 new )你不必将它转换为 'ImageView manually as it already is a fresh'n new ImageView`.

现在让你明白为什么你的 ImageView在以您最初厌倦的方式(即通过创建新对象)声明它时未显示,因为您很可能拥有 setContentView() onCreate() 中的方法参数方法设置为 R.layout.main (您的 main.xml 文件包含您的布局)。现在这根本行不通,因为 Android 不知道您要用 ImageView 做什么你创造了,因为你从未使用过它。你可以定义一个 ViewGroup (LinearLayout、AbsoluteLayout 等)在 Java 中通过代码将所有小部件(ImageView、ToggleButton 等)分配给它们,然后将其设置为 setContentView() 的参数。让它们显示出来,或者您可以采用更简单的方法,并像我一样在包含布局的 XML 文件中定义您需要的所有内容。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ToggleButton
        android:id="@+id/trigger"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Active"
        android:textOff="Stopped"
        android:checked="false"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/trigger"
        android:visibility="invisible"/>
</RelativeLayout>

现在在 Java 中,您只需正确获取对小部件的引用(或指针),这样您就可以将它们用于各种讨厌的事情,例如为它们设置动画。

这是我的 Java 代码。

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class VeryCoolAnimation extends Activity {

    ImageView image = null;
    ToggleButton trigger = null;
    Context ctx = null;

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

        ctx = getApplicationContext();

        trigger = (ToggleButton) findViewById(R.id.trigger);
        image = (ImageView) findViewById(R.id.image);

        image.setImageResource(R.drawable.icon);

        trigger.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if(isChecked) {
                    animate();
                }
            }
        });
    }

    private void animate() {
        int loc[] = new int[2];
        image.getLocationOnScreen(loc);
        // Just used to print out the images position
        Toast.makeText(ctx, "Position on screen: x = " + loc[0] + " y = " + loc[1], 5000).show();
        int fromXDelta = loc[0];
        int fromYDelta = loc[1];
            int toYDelta = fromYDelta + 50;
        int toXDelta = fromXDelta + 80;

        TranslateAnimation translateAnimation = new TranslateAnimation(
                fromXDelta, toXDelta, fromYDelta, toYDelta);
        translateAnimation.setDuration(800);
        translateAnimation.setFillEnabled(true);

        image.startAnimation(translateAnimation);
        image.setVisibility(View.INVISIBLE);
    }
}

我不完全确定这是您想要的效果,但您当然可以根据自己的需要进行调整,因为您拥有一段功能齐全的代码。

祝你好运。

关于android - 在android中绘制和动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4110592/

相关文章:

android - 无法使用 Clevertap。卡在推送测试通知处

android - Android 中的图像路径

android - Facebook 授权访问 token ?

android - 从 kotlin 中的 fun 返回 null

c++ - 如何以编程方式生成与这些相似的形状?

graphics - ($VersionNumber < 6) 在 Mathematica 中裁剪导出的图像

java - 使用 Swing 和 Threads 设计 JApplet 动画

jquery - 将鼠标悬停在隐藏元素上后不久就会重播隐藏动画

html - 图像在屏幕上滑动 - CSS3

OpenGL:将对象转换为原点