我要自动更改画布图像。是否可以自动更改Canvas图像Android?
意思是我希望那个画布图像应该被一个接一个地连续设置。
我已经写了代码,它只设置图像一次。
但我不知道自动更改画布图像的代码,以便它会建立像对象的影响在道路上运行..
那么什么是创建这类动画的方式在画布上?
请给我一些想法来创建这样的2D动画。
Thanx提前。
我要自动更改画布图像。是否可以自动更改Canvas图像Android?
意思是我希望那个画布图像应该被一个接一个地连续设置。
我已经写了代码,它只设置图像一次。
但我不知道自动更改画布图像的代码,以便它会建立像对象的影响在道路上运行..
那么什么是创建这类动画的方式在画布上?
请给我一些想法来创建这样的2D动画。
Thanx提前。
我会做到这一点的方式如下:
onDraw()
此位图绘制在画布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:
包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
}
}
我希望这可以帮助你。 然后祝你好运。
退房本教程帧的动画:
http://www.youtube.com/watch?v=iTKtT-R98EE
更多信息,可以在下面的链接中找到:
Starting Frame-By-Frame Animation
以下是一步一步的过程:
在“帧动画”中,您将重复交换帧,以使其显示为连续的t人眼,我们觉得它是动画。帧被称为图像。因此,为了实现帧动画,需要有一组图像来描述运动。
第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方法:
类型转换图像视图 类型转换动画绘制对象 设置可绘制backgroung上的图像视图
// 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();
}
}
你可以请求你的完整代码与setter和线程? – zanky 2013-02-18 11:39:10
OnDraw将如何被调用? – zanky 2013-02-18 11:54:43
我编辑了我的答案并添加了代码。祝你好运 – 2013-02-18 22:02:39