绘画和android中动画 [英] drawing and animating in android

查看:60
本文介绍了绘画和android中动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一关,这更是一个比关于特定问题的编码动画问题。我需要填写的研究,我一直在做左广招。

first off, this is more of a question about animating than a specific problem coding. i need to fill in the broad strokes left by the research i've been doing.

我想要做的:

活动应该只与一个切换按钮可见加载。当按钮被触动,一个​​PNG将动画相对于切换按钮一定的地位。同时,另一个按钮将关闭屏幕滑入和滑出如果/当切换按钮再次pssed $ P $。

the activity should load with only the a togglebutton visible. when the button is touched, a png will be animated to a certain position relative to the togglebutton. also, another button will slide in from off screen and slide off if/when the togglebutton is pressed again.

我想不通:

我可以绘制图像,但只有通过XML。创建一个ImageView的编程和设置背景什么都不做。

i can draw an image, but only via xml. creating an imageview and setting the background programmatically does nothing.

当我从XML得出,当切换按钮是pssed像我想$ P $我不能移动图像。当我打电话getPositionOnScreen()我得到一个空指针,即使编译器可以看到我指的是在XML和在code中描述的ImageView的。

when i draw from xml, i can't move the image when the togglebutton is pressed like i want to. when i call getPositionOnScreen() i get a null pointer, even though the complier can see i'm referring to the imageview described in both xml and in code.

我,因为我通过XML调用getPositionOnScreen,我有定位切换按钮后面的图像,使其直到按钮已被pressed不可见,图像开始移动。这个想法是,用不同的屏幕尺寸,我不会确切地知道该视图,直到运行时。 getPostionOnScreen可以让我得到的ImageView的坐标,所以我知道它已经被定位。当我有一个起始位置,可以让它在屏幕上从后面切换按钮通过简单地添加到X或Y直到它就是我想要的动起来。

i'm calling getPositionOnScreen because via xml, i have the image positioned behind the togglebutton so that it's not visible until the button has been pressed and the image starts moving. the idea is that with different screen sizes i won't know exactly where the view is until runtime. getPostionOnScreen allows me to get the coordinates of the imageview so i know where it has been positioned. when i have a start position, can tell it to "move up" on the screen from behind the togglebutton by simply adding to x or y until it's where i want.

这是code绘制图像(插在onCreate方法)。

this is the code to draw the image (inserted in the onCreate method).

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

这是code我使用动画。单击该按钮时,呼吁从听者认为这种方法。

this is the code i'm using to animate. when the button is clicked, it calls this method on the view from the listener.

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

 }

我很清楚,这是可怕的错误,将无法正常工作。我需要明白的是为什么。任何帮助将是非常欢迎的。

i'm well aware that this is horribly wrong and won't work. what i need to understand is why. any help would be very welcome.

推荐答案

好吧第一点。如果要创建一个新的的ImageView 反对这种方式(通过使用),你不必将它转换为ImageView的手动因为它已经是一个fresh'n新 ImageView`。

Alright the first point. If you are creating a new ImageView object that way (by using new) you don't have to cast it to 'ImageViewmanually as it already is a fresh'n newImageView`.

现在让你明白为什么你的的ImageView 声明它,当你厌倦了最初的方式(通过创建一个新的对象IE),因为你最有可能有不显示的setContentView()中设置为 R.layout.main你的的onCreate()方法参数的方法(包含您的布局你的main.xml中的文件)。现在,根本不会因为Android的工作,不知道你正在尝试用的ImageView 您已经创建了,你从来没有用它做。哟,既可以用code定义的ViewGroup (LinearLayout中,AbsoluteLayout等),Java和所有的小部件(ImageView的,切换按钮等)分配给他们,然后将其设置为的setContentView的参数()来让他们显示或者你可以去更简单的方法,并确定你在包含布局像我一样,XML文件所需要的。

Now to make you understand why your ImageView wasn't displayed when declaring it the way you initially tired (i.e. by creating a new object) because you most likely had the setContentView() methods parameter in your onCreate() method set to R.layout.main (your main.xml file containing your layout). Now that simply won't work as Android won't know what you are trying to do with the ImageView you've created as you never use it. Yo could either define a ViewGroup (LinearLayout, AbsoluteLayout, etc.) in Java by code and assign all your widgets (ImageView, ToggleButton, etc.) to them and then set it as the parameter of setContentView() to get them displayed or you can go the easier way and define all you need in your XML file containing your layout like I did.

<?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中,你只需要得到引用(或指针),以小部件的权利,所以你可以像动画他们使用他们的各种讨厌的东西。

Now in Java you just have to get the references (or pointers) to your widgets right so you can use them for all kinds of nasty stuff like animating them.

下面是我的Java code。

Here is my Java code.

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

我不完全肯定这是你瞄准的效果,但当然也可以进行调整以满足您的需求为您code的全功能件。

I'm not entirely sure that this is the effect you were aiming for but you can of course adjust it to suit your needs as you have a fully functional piece of code.

祝你好运。

这篇关于绘画和android中动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆