背景图片移动时,有时会出现一闪一闪的黑,不知道为什么呢?求救啊public class CloudView extends SurfaceView implements SurfaceHolder.Callback {
final static int NUMS = 40;
SurfaceHolder sHolder;
BackGroundMoveThread bMoveThread;
RepaintThread rThread;
Paint paint; int backgroundX;
Bitmap background;
Bitmap[] backgrounds = new Bitmap[NUMS]; int screenWidth = 480;
int screenHeight = 320;
int imgWidth;
int imgHeight;
int k; public CloudView(Context context) {
super(context);
sHolder = getHolder();
sHolder.addCallback(this);// TODO Auto-generated constructor stub
// Screen size
DisplayMetrics dm = getResources().getDisplayMetrics();
screenHeight = dm.heightPixels;
screenWidth = dm.widthPixels;
// init Thread
rThread = new RepaintThread(this, sHolder);
bMoveThread = new BackGroundMoveThread(this);
// init Bitmap
background = BitmapFactory
.decodeResource(getResources(), R.drawable.battlebackground);// 大背景图片
//background = Bitmap.createScaledBitmap(background, screenWidth, screenHeight, false);
imgHeight = background.getHeight();
imgWidth = background.getWidth() / NUMS;
Toast.makeText(context, ""+imgWidth,Toast.LENGTH_SHORT).show();
for (int i = 0; i < backgrounds.length; i++) {// 切成小图片
backgrounds[i] = Bitmap.createBitmap(background, imgWidth * i, 0,
imgWidth, imgHeight);
}
if (background != null && background.isRecycled()) {
background.recycle();
background = null;
System.gc();
} } @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub } @Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
rThread.setFlag(true);
rThread.start();
bMoveThread.setFlag(true);
bMoveThread.start();
} @Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
rThread.setFlag(false);
bMoveThread.setFlag(false);
boolean flag = false;
while (flag) {
try {
bMoveThread.join();
rThread.join();
flag = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} @Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
super.draw(canvas);
paint = new Paint();
paint.setStyle(Style.FILL);
// 解决i左侧的问题
if (backgroundX > 0) {
int n = (backgroundX / imgWidth)
+ ((backgroundX % imgWidth == 0) ? 0 : 1);// 计算i左面有几幅图
for (int j = 1; j <= n; j++) {
canvas.drawBitmap(backgrounds[(k - j + backgrounds.length)
% backgrounds.length], backgroundX - imgWidth * j, 5,
paint);
}
} // 解决i自己
canvas.drawBitmap(backgrounds[k], backgroundX, 5, paint); // 解决i右侧的问题
if (backgroundX < screenWidth - imgWidth) {
int m = screenWidth - (backgroundX + imgWidth);
int n = (m / imgWidth) + ((m % imgWidth == 0) ? 0 : 1);// 计算i右面有几幅图
for (int j = 1; j <= n; j++) {
canvas.drawBitmap(backgrounds[(k + j) % backgrounds.length], backgroundX
+ imgWidth * j, 5, paint);
}
}
} class RepaintThread extends Thread {
private boolean flag = false;// 循环标志位
private int sleep = 1;
CloudView cloudView;
SurfaceHolder surfaceHolder; public RepaintThread(CloudView cloudView, SurfaceHolder surfaceHolder) {
this.cloudView = cloudView;
this.surfaceHolder = surfaceHolder;
} public void setFlag(boolean flag) {
this.flag = flag;
} public void run() {
Canvas canvas;
while (flag) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas(null);
synchronized (this.surfaceHolder) {
this.cloudView.draw(canvas);
}
} catch (Exception e) {
e.printStackTrace();// TODO: handle exception
} finally {
if (canvas != null) {
this.surfaceHolder.unlockCanvasAndPost(canvas);
}
}
try {
Thread.sleep(sleep);
} catch (Exception e) {
// TODO: handle exception
}
}// end while
}
}
}
public class BackGroundMoveThread extends Thread {
private boolean flag = false;// 循环标志位
private int step = 2;// 背景图片移动的步长
private int sleep =20;// 休眠时长
CloudView cloudView; public void setFlag(boolean flag) {
this.flag = flag;
} public BackGroundMoveThread(CloudView cloudView) {
this.cloudView = cloudView;
} @Override
public void run() {
while (this.flag) {
cloudView.backgroundX -= step;
if(cloudView.backgroundX <-cloudView.imgWidth){
cloudView.k = (cloudView.k+1)%CloudView.NUMS;
cloudView.backgroundX+=cloudView.imgWidth;
}
try {
Thread.sleep(sleep);
}catch (Exception e) {
e.printStackTrace();// TODO: handle exception
}
}// end while
}
}
final static int NUMS = 40;
SurfaceHolder sHolder;
BackGroundMoveThread bMoveThread;
RepaintThread rThread;
Paint paint; int backgroundX;
Bitmap background;
Bitmap[] backgrounds = new Bitmap[NUMS]; int screenWidth = 480;
int screenHeight = 320;
int imgWidth;
int imgHeight;
int k; public CloudView(Context context) {
super(context);
sHolder = getHolder();
sHolder.addCallback(this);// TODO Auto-generated constructor stub
// Screen size
DisplayMetrics dm = getResources().getDisplayMetrics();
screenHeight = dm.heightPixels;
screenWidth = dm.widthPixels;
// init Thread
rThread = new RepaintThread(this, sHolder);
bMoveThread = new BackGroundMoveThread(this);
// init Bitmap
background = BitmapFactory
.decodeResource(getResources(), R.drawable.battlebackground);// 大背景图片
//background = Bitmap.createScaledBitmap(background, screenWidth, screenHeight, false);
imgHeight = background.getHeight();
imgWidth = background.getWidth() / NUMS;
Toast.makeText(context, ""+imgWidth,Toast.LENGTH_SHORT).show();
for (int i = 0; i < backgrounds.length; i++) {// 切成小图片
backgrounds[i] = Bitmap.createBitmap(background, imgWidth * i, 0,
imgWidth, imgHeight);
}
if (background != null && background.isRecycled()) {
background.recycle();
background = null;
System.gc();
} } @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub } @Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
rThread.setFlag(true);
rThread.start();
bMoveThread.setFlag(true);
bMoveThread.start();
} @Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
rThread.setFlag(false);
bMoveThread.setFlag(false);
boolean flag = false;
while (flag) {
try {
bMoveThread.join();
rThread.join();
flag = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} @Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
super.draw(canvas);
paint = new Paint();
paint.setStyle(Style.FILL);
// 解决i左侧的问题
if (backgroundX > 0) {
int n = (backgroundX / imgWidth)
+ ((backgroundX % imgWidth == 0) ? 0 : 1);// 计算i左面有几幅图
for (int j = 1; j <= n; j++) {
canvas.drawBitmap(backgrounds[(k - j + backgrounds.length)
% backgrounds.length], backgroundX - imgWidth * j, 5,
paint);
}
} // 解决i自己
canvas.drawBitmap(backgrounds[k], backgroundX, 5, paint); // 解决i右侧的问题
if (backgroundX < screenWidth - imgWidth) {
int m = screenWidth - (backgroundX + imgWidth);
int n = (m / imgWidth) + ((m % imgWidth == 0) ? 0 : 1);// 计算i右面有几幅图
for (int j = 1; j <= n; j++) {
canvas.drawBitmap(backgrounds[(k + j) % backgrounds.length], backgroundX
+ imgWidth * j, 5, paint);
}
}
} class RepaintThread extends Thread {
private boolean flag = false;// 循环标志位
private int sleep = 1;
CloudView cloudView;
SurfaceHolder surfaceHolder; public RepaintThread(CloudView cloudView, SurfaceHolder surfaceHolder) {
this.cloudView = cloudView;
this.surfaceHolder = surfaceHolder;
} public void setFlag(boolean flag) {
this.flag = flag;
} public void run() {
Canvas canvas;
while (flag) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas(null);
synchronized (this.surfaceHolder) {
this.cloudView.draw(canvas);
}
} catch (Exception e) {
e.printStackTrace();// TODO: handle exception
} finally {
if (canvas != null) {
this.surfaceHolder.unlockCanvasAndPost(canvas);
}
}
try {
Thread.sleep(sleep);
} catch (Exception e) {
// TODO: handle exception
}
}// end while
}
}
}
public class BackGroundMoveThread extends Thread {
private boolean flag = false;// 循环标志位
private int step = 2;// 背景图片移动的步长
private int sleep =20;// 休眠时长
CloudView cloudView; public void setFlag(boolean flag) {
this.flag = flag;
} public BackGroundMoveThread(CloudView cloudView) {
this.cloudView = cloudView;
} @Override
public void run() {
while (this.flag) {
cloudView.backgroundX -= step;
if(cloudView.backgroundX <-cloudView.imgWidth){
cloudView.k = (cloudView.k+1)%CloudView.NUMS;
cloudView.backgroundX+=cloudView.imgWidth;
}
try {
Thread.sleep(sleep);
}catch (Exception e) {
e.printStackTrace();// TODO: handle exception
}
}// end while
}
}
莫调用这个,这个是CloudView的draw。建议把draw函数里的内容放在一个新函数里!
可能有用
package com.example.zk;import android.os.Bundle;
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.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.NavUtils;public class MainActivity extends Activity { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
} class MyView extends View implements Runnable {
int x = 100;
float y = 0;
int step = 1; public MyView(Context context) {
super(context); new Thread(this).start();
// TODO Auto-generated constructor stub
} @Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
R.drawable.bg);
Bitmap rain = BitmapFactory.decodeResource(this.getResources(),
R.drawable.rain);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
Log.e("draw.....", y + "");
// canvas.drawLine(x, y, x, y+5, paint);
canvas.drawBitmap(rain, x, y, paint);
} public void run() {
while (true) {
y += 0.1;
postInvalidate();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}这段是模拟下雨的android代码,为什么下的过程会一卡一卡的,完全不顺畅啊!