0

我想自动更改画布图像

意味着我希望画布图像应该一个接一个地连续设置。

我编写了只设置一次图像的代码。

但我不知道有关自动更改画布图像的代码,因此它会创建对象在路上运行的效果..

那么在画布中创建这种动画的方法是什么?

请给我一些想法来创建这样的 2D 动画。

提前感谢。

4

2 回答 2

2

我会这样做:

  • 创建一个包含位图的自定义视图,并在onDraw()此位图中绘制在画布上
  • 给这个自定义视图一个 setter,给它一个新的位图(可能是一个 ressouce-id 或类似的东西)
  • 创建一个新线程(带有 UI 处理程序),每秒至少更改 24 次画布资源并customView.invalidate()通过调用handler.post

这对我有用

编辑:代码

活动:

package de.test.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class AnimationTestActivity extends Activity {
Button btn;
CustomView customView;
LinearLayout layout;

int[] imageIDs;

private void init(){    //array with my ressouce-IDs
    imageIDs = new int[]{
        R.drawable.pic1,    
        R.drawable.pic2,    
        R.drawable.pic3,    
        R.drawable.pic4,    
        R.drawable.pic5,    
        R.drawable.pic6,    
        R.drawable.pic7,    
        R.drawable.pic8,    
        R.drawable.pic9,    
        R.drawable.pic10,   
        R.drawable.pic11,   
        R.drawable.pic12,   
        R.drawable.pic13,   
        R.drawable.pic14,   
        R.drawable.pic15,   
        R.drawable.pic16,   
        R.drawable.pic17,   
        R.drawable.pic18,   
        R.drawable.pic19,   
        R.drawable.pic20,   
        R.drawable.pic21,   
        R.drawable.pic22,   
        R.drawable.pic23,   
        R.drawable.pic24    
    };
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    init();

    btn = (Button) findViewById(R.id.btnStart);
    layout = (LinearLayout)findViewById(R.id.layout);

    customView = new CustomView(this);
    customView.setNewImage(imageIDs[0]);
    layout.addView(customView);

    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Thread t = new Thread(){
                private final int FPS = 24; //How many frames will be dran per second
                private final int SLEEPTIME = 1000/FPS; //Time, the thread waits, before drawing the next picture

                @Override
                public void run() {
                    super.run();
                    for(int i=0;i<imageIDs.length;i++){
                        customView.setNewImage(imageIDs[i]);    //set next picture
                        customView.repaint();   //draw the picture on the canvas
                        try {
                            sleep(SLEEPTIME);   //wait, until the next picture can be drawn
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            t.start();
        }
    });
}
}

自定义视图:

包 de.test.animation;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;


public class CustomView extends View {
private Bitmap image;   //image to be drawn on this view
private Context context;

public CustomView(Context context) {    //constructor
    super(context);
    this.context = context;
}

public void setNewImage(int r_id){  //method to set a new picture (via resouce-id)
    image = BitmapFactory.decodeResource(context.getResources(), r_id); //decode the image from the resouces
}

public void repaint(){  //method to repaint this view
    this.post(new Runnable(){   //posting via a new runnable (otherwhise you get a "calledByWrongThreadException"
        @Override
        public void run() {
            invalidate();   //Thread initiates UI-Thread to update this view
        }
    });
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(image, 0, 0, new Paint()); //draw the picture in the view
}


}

我希望这可以帮助你。那么祝你好运吧。

于 2013-02-18T11:33:31.167 回答
2

看看这个帧动画教程:

http://www.youtube.com/watch?v=iTKtT-R98EE

更多信息可以在以下链接中找到:

开始逐帧动画

以下是分步过程:

在帧动画中,您将反复交换帧,以便它在人眼看来是连续的,我们觉得它是动画的。帧是指图像。因此,要实现帧动画,需要有一组图像来描述运动。

第 1 步 - 创建一个可绘制文件夹。

在其中创建一个 animation_list.xml 文件。

它包括:具有帧图像地址的项目列表。

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">

<item android:drawable="@drawable/blank" android:duration="210" />
<item android:drawable="@drawable/logo" android:duration="210" />
<item android:drawable="@drawable/logo1" android:duration="210" />
<item android:drawable="@drawable/logo2" android:duration="210" />
<item android:drawable="@drawable/logo3" android:duration="210" />
<item android:drawable="@drawable/logo4" android:duration="210" />
<item android:drawable="@drawable/logo5" android:duration="210" />
<item android:drawable="@drawable/logo6" android:duration="210" />
<item android:drawable="@drawable/logofinal" android:duration="210" />
</animation-list>

第 2 步 - 创建一个 activity_main.xml 文件

它包括:图像视图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageAnimation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true" />

</RelativeLayout>

第 3 步 - 在 onCreate 方法之外:

声明 Image View 和 Animation Drawable

// Declaring an Image View and an Animation Drawable

ImageView view;
AnimationDrawable frameAnimation;

Step4- 在 OnCreate 方法中:

Typecast the Image view Typecast the Animation Drawable 在图像视图上设置可绘制背景

// Typecasting the Image View
view = (ImageView) findViewById(R.id.imageAnimation);

// Setting animation_list.xml as the background of the image view
view.setBackgroundResource(R.drawable.animation_list);

// Typecasting the Animation Drawable
frameAnimation = (AnimationDrawable) view.getBackground();

Step5- 在 onCreate 方法之后:

动画应该只在它处于焦点时运行,即当它对用户可见时。因此在 onCreate 方法之后定义此方法。

// Called when Activity becomes visible or invisible to the user
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
      if (hasFocus) {
    // Starting the animation when in Focus
    frameAnimation.start();
    } else {
        // Stoping the animation when not in Focus
    frameAnimation.stop();
      }
}

资源

于 2013-02-18T11:31:08.763 回答