可以自动更改Canvas Image Android吗?

时间:2013-02-18 11:25:54

标签: android animation android-canvas android-animation

我想自动更改画布图像

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

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

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

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

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

提前完成。

2 个答案:

答案 0 :(得分:2)

查看本框架动画教程:

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

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

Starting Frame-By-Frame Animation

以下是一步一步的程序:

在帧动画中,您将重复交换帧,使其看起来与人眼连续,我们觉得它是动画的。帧被称为图像。因此,为了实现帧动画,需要具有描述运动的图像集。

  

步骤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方法之外:

声明图像视图和动画可绘制

// Declaring an Image View and an Animation Drawable

ImageView view;
AnimationDrawable frameAnimation;
  

Step4- OnCreate方法内部:

Typecast图像视图 Typecast动画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();
      }
}

Source

答案 1 :(得分:1)

我会按以下方式这样做:

  • 创建一个包含位图的自定义视图,并在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();
        }
    });
}
}

<强> CustomView:

package 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
}


}

我希望这会对你有所帮助。 祝你好运。