图片的绘制以及旋转缩放的实现
在这点上Android 确实比J2ME 强大很多 手机游戏开发最痛苦的是什么?? 是游戏引擎的开发,但是工程师会把大部分时间浪费在对坐标上,如果写引擎的时候没有把自适应考虑周全后期会非常痛苦,现在手机屏幕分辨率是各式各样 内存大小也是各式各样 所以可见自适应屏幕算法有多么的重要。
复制代码
复制代码
4.播放frame动画
做游戏的话播放动画可就是必不可少的元素 帧动画帧动画 顾名思义是一帧一帧的播放 。 实际在开发中为了节省内存美术会把人物的图片切成一小块一小块然后由程序根据编辑器生成的点把图片在拼接起来这样就可以做到用更少的图片去实现更多的动画效果因为不太方便介绍图片编辑器 这个demo我只给大家简单的介绍一下播放动画的原理 后期我会深入讲解。
如图所示这个小人一直在行走 实际上是4张图片在来回切换 每张图片延迟500毫秒 后播下一张 以此类推。
在这点上Android 确实比J2ME 强大很多 手机游戏开发最痛苦的是什么?? 是游戏引擎的开发,但是工程师会把大部分时间浪费在对坐标上,如果写引擎的时候没有把自适应考虑周全后期会非常痛苦,现在手机屏幕分辨率是各式各样 内存大小也是各式各样 所以可见自适应屏幕算法有多么的重要。

-
package cn.m15.xys;
-
-
import android.app.Activity;
-
import android.content.Context;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
import android.graphics.Canvas;
-
import android.graphics.Matrix;
-
import android.graphics.Paint;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.LinearLayout;
-
-
public class Image extends Activity {
-
ImageView imageView = null;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
imageView = new ImageView(this);
-
setContentView(R.layout.image);
-
LinearLayout ll = (LinearLayout) findViewById(R.id.iamgeid);
-
ll.addView(imageView);
-
// 向左移动
-
Button botton0 = (Button) findViewById(R.id.buttonLeft);
-
botton0.setOnClickListener(new OnClickListener() {
-
@Override
-
public void onClick(View arg0) {
-
imageView.setPosLeft();
-
}
-
});
-
-
// 向右移动
-
Button botton1 = (Button) findViewById(R.id.buttonRight);
-
botton1.setOnClickListener(new OnClickListener() {
-
@Override
-
public void onClick(View arg0) {
-
imageView.setPosRight();
-
}
-
});
-
// 左旋转
-
Button botton2 = (Button) findViewById(R.id.buttonRotationLeft);
-
botton2.setOnClickListener(new OnClickListener() {
-
@Override
-
public void onClick(View arg0) {
-
imageView.setRotationLeft();
-
}
-
});
-
-
// 右旋转
-
Button botton3 = (Button) findViewById(R.id.buttonRotationRight);
-
botton3.setOnClickListener(new OnClickListener() {
-
@Override
-
public void onClick(View arg0) {
-
imageView.setRotationRight();
-
}
-
});
-
-
// 缩小
-
Button botton4 = (Button) findViewById(R.id.buttonNarrow);
-
botton4.setOnClickListener(new OnClickListener() {
-
-
@Override
-
public void onClick(View arg0) {
-
imageView.setNarrow();
-
}
-
});
-
-
// 放大
-
Button botton5 = (Button) findViewById(R.id.buttonEnlarge);
-
botton5.setOnClickListener(new OnClickListener() {
-
-
@Override
-
public void onClick(View arg0) {
-
imageView.setEnlarge();
-
}
-
});
-
-
super.onCreate(savedInstanceState);
-
-
}
-
-
class ImageView extends View {
-
Paint mPaint = null;
-
Bitmap bitMap = null;
-
Bitmap bitMapDisplay = null;
-
int m_posX = 120;
-
int m_posY = 50;
-
int m_bitMapWidth = 0;
-
int m_bitMapHeight = 0;
-
Matrix mMatrix = null;
-
float mAngle = 0.0f;
-
float mScale = 1f;//1为原图的大小
-
-
public ImageView(Context context) {
-
super(context);
-
mPaint = new Paint();
-
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
-
bitMap = BitmapFactory.decodeResource(this.getResources(),
-
R.drawable.image);
-
bitMapDisplay = bitMap;
-
mMatrix = new Matrix();
-
// 获取图片宽高
-
m_bitMapWidth = bitMap.getWidth();
-
m_bitMapHeight = bitMap.getHeight();
-
}
-
-
// 向左移动
-
public void setPosLeft() {
-
m_posX -= 10;
-
}
-
-
// 向右移动
-
public void setPosRight() {
-
m_posX += 10;
-
}
-
-
// 向左旋转
-
public void setRotationLeft() {
-
mAngle--;
-
setAngle();
-
}
-
-
// 向右旋转
-
public void setRotationRight() {
-
mAngle++;
-
setAngle();
-
}
-
-
// 缩小图片
-
public void setNarrow() {
-
if (mScale > 0.5) {
-
mScale -= 0.1;
-
setScale();
-
}
-
}
-
-
// 放大图片
-
public void setEnlarge() {
-
if (mScale < 2) {
-
mScale += 0.1;
-
setScale();
-
}
-
}
-
-
// 设置缩放比例
-
public void setAngle() {
-
mMatrix.reset();
-
mMatrix.setRotate(mAngle);
-
bitMapDisplay = Bitmap.createBitmap(bitMap, 0, 0, m_bitMapWidth,
-
m_bitMapHeight, mMatrix, true);
-
}
-
-
// 设置旋转比例
-
public void setScale() {
-
mMatrix.reset();
-
//float sx X轴缩放
-
//float sy Y轴缩放
-
mMatrix.postScale(mScale, mScale);
-
bitMapDisplay = Bitmap.createBitmap(bitMap, 0, 0, m_bitMapWidth,
-
m_bitMapHeight, mMatrix, true);
-
}
-
-
@Override
-
protected void onDraw(Canvas canvas) {
-
super.onDraw(canvas);
-
canvas.drawBitmap(bitMapDisplay, m_posX, m_posY, mPaint);
-
invalidate();
-
}
-
}
- }
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:id="@+id/iamgeid"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<Button android:id="@+id/buttonLeft"
-
android:layout_width="fill_parent" android:layout_height="wrap_content"
-
android:text="图片向左移动"
-
/>
-
<Button android:id="@+id/buttonRight"
-
android:layout_width="fill_parent" android:layout_height="wrap_content"
-
android:text="图片向右移动"
-
/>
-
<Button android:id="@+id/buttonRotationLeft"
-
android:layout_width="fill_parent" android:layout_height="wrap_content"
-
android:text="图片左旋转"
-
/>
-
<Button android:id="@+id/buttonRotationRight"
-
android:layout_width="fill_parent" android:layout_height="wrap_content"
-
android:text="图片右旋转"
-
/>
-
<Button android:id="@+id/buttonNarrow"
-
android:layout_width="fill_parent" android:layout_height="wrap_content"
-
android:text="图片缩小"
-
/>
-
<Button android:id="@+id/buttonEnlarge"
-
android:layout_width="fill_parent" android:layout_height="wrap_content"
-
android:text="图片放大"
-
/>
- </LinearLayout>
做游戏的话播放动画可就是必不可少的元素 帧动画帧动画 顾名思义是一帧一帧的播放 。 实际在开发中为了节省内存美术会把人物的图片切成一小块一小块然后由程序根据编辑器生成的点把图片在拼接起来这样就可以做到用更少的图片去实现更多的动画效果因为不太方便介绍图片编辑器 这个demo我只给大家简单的介绍一下播放动画的原理 后期我会深入讲解。
如图所示这个小人一直在行走 实际上是4张图片在来回切换 每张图片延迟500毫秒 后播下一张 以此类推。

-
package cn.m15.xys;
-
-
import android.app.Activity;
-
import android.content.Context;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
import android.graphics.Canvas;
-
import android.graphics.Color;
-
import android.graphics.Paint;
-
import android.os.Bundle;
-
import android.view.View;
-
-
public class FramAnimation extends Activity {
-
public final static int ANIM_COUNT = 4;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
setContentView(new FramView(this));
-
super.onCreate(savedInstanceState);
-
-
}
-
-
class FramView extends View {
-
Bitmap[] bitmap = new Bitmap[ANIM_COUNT];
-
Bitmap display = null;
-
Paint paint = null;
-
long startTime = 0;
-
int playID = 0;
-
-
public FramView(Context context) {
-
super(context);
-
for (int i = 0; i < ANIM_COUNT; i++) {
-
bitmap[i] = BitmapFactory.decodeResource(this.getResources(),
-
R.drawable.hero_a + i);
-
}
-
display = bitmap[0];
-
paint = new Paint();
-
startTime = System.currentTimeMillis();
-
}
-
-
@Override
-
protected void onDraw(Canvas canvas) {
-
super.onDraw(canvas);
-
paint.setColor(Color.WHITE);
-
canvas.drawText("播放动画中...", 100, 30, paint);
-
long nowTime = System.currentTimeMillis();
-
if (nowTime - startTime >= 500) {
-
startTime=nowTime;
-
playID++;
-
if (playID >= ANIM_COUNT) {
-
playID = 0;
-
}
-
canvas.drawBitmap(bitmap[playID], 100, 100, paint);
-
}
-
invalidate();
-
}
-
}
-
- }