代码之家  ›  专栏  ›  技术社区  ›  Zankhna

是否可以在Android上自动更改画布图像?

  •  0
  • Zankhna  · 技术社区  · 12 年前

    我想 自动更改画布图像 .

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

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

    但我不知道自动更改画布图像的代码,所以它会产生这样的效果 物体在路上行驶 ..

    所以 在画布上创作这种动画的方法是什么?

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

    提前Thanx。

    2 回复  |  直到 12 年前
        1
  •  2
  •   Tom Mekken    12 年前

    我会按照以下方式来做:

    • 创建一个自定义视图,该视图包含位图和 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
    }
    
    
    }
    

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

        2
  •  2
  •   Community Mohan Dere    8 年前

    查看此帧动画教程:

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

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

    Starting Frame-By-Frame Animation

    以下是循序渐进的程序:

    在“帧动画”(Frame Animation)中,您将重复交换帧,使其在人眼看来是连续的,并且我们感觉它已设置动画。帧是指图像。因此,要实现帧动画,需要有一组描述运动的图像。

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

    在其中创建一个animation\ulist.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;
    

    步骤4-在OnCreate方法内部:

    键入图像视图 可绘制动画的打字 在图像视图上设置可绘制背景

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

    步骤5-在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