所以我有一个非常具体的问题,我觉得我应该能够弄明白自己,但经过3个星期的努力弄清楚什么是错的,我已经准备好放弃...... < / p>
基本上我正在创建一个应用程序,当你触摸屏幕时,它会创建一个新的形状(由位图图像制成),然后再增长。
再次触摸屏幕时,会创建另一个也会增长的新形状。
我遇到的问题是,每次形成新形状时,都会出现一个“鬼”图像,它不会在屏幕上居中并在淡出时闪烁一下。下面是代码,我认为问题出现在“MotionEvent.ACTION_DOWN:”区域,但我也认为它可能在onDraw部分。如果有人有任何想法,任何事情都会有所帮助。
代码:
package com.attempt.shape2;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
public class Test extends Activity implements OnTouchListener {
MainView v1;
Bitmap shape1;
Bitmap shape2;
Bitmap shape3;
Bitmap shape4;
Bitmap shape5;
Bitmap bigger1,bigger2,bigger3,bigger4;
float xOld1, yOld1, xNew1, yNew1;
float centerX1, centerY1, centerX2, centerY2,centerX3, centerY3, centerX4, centerY4;
Random generator1 = new Random();
int randomIndex1 = generator1.nextInt( 6);
//TEST STUFF
int shape1Height, bigger1Height, bigger2Height, bigger3Height, bigger4Height;
int shape1Width, bigger1Width, bigger2Width, bigger3Width, bigger4Width;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
shape1 = BitmapFactory.decodeResource(getResources(), R.drawable.blank);
bigger1 = BitmapFactory.decodeResource(getResources(), R.drawable.blank);
shape2 = BitmapFactory.decodeResource(getResources(), R.drawable.blank);
bigger2 = BitmapFactory.decodeResource(getResources(), R.drawable.blank);
shape3 = BitmapFactory.decodeResource(getResources(), R.drawable.blank);
bigger3 = BitmapFactory.decodeResource(getResources(), R.drawable.blank);
shape4 = BitmapFactory.decodeResource(getResources(), R.drawable.blank);
bigger4 = BitmapFactory.decodeResource(getResources(), R.drawable.blank);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
v1 = new MainView(this);
v1.setOnTouchListener(this);
setContentView(v1);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v1.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v1.resume();
}
public class MainView extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean isItOk = false;
public MainView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
@Override
public void run() {
// TODO Auto-generated method stub
while (isItOk == true){
//perform canvas drawing
if (!holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
onDraw(c);
holder.unlockCanvasAndPost(c);
}
}
protected void onDraw(Canvas canvas){
centerX1 = (canvas.getWidth()-bigger1.getWidth())/2;
centerY1 = (canvas.getHeight()-bigger1.getHeight())/2;
centerX2 = (canvas.getWidth()-bigger2.getWidth())/2;
centerY2 = (canvas.getHeight()-bigger2.getHeight())/2;
centerX3 = (canvas.getWidth()-bigger3.getWidth())/2;
centerY3 = (canvas.getHeight()-bigger3.getHeight())/2;
centerX4 = (canvas.getWidth()-bigger4.getWidth())/2;
centerY4 = (canvas.getHeight()-bigger4.getHeight())/2;
//bigger1Height = shape1.getHeight();
//bigger1Width = shape1.getWidth();
bigger1 = Bitmap.createScaledBitmap(shape1, bigger1Width, bigger1Height, false);
bigger2 = Bitmap.createScaledBitmap(shape2, bigger2Width, bigger2Height, false);
bigger3 = Bitmap.createScaledBitmap(shape3, bigger3Width, bigger3Height, false);
bigger4 = Bitmap.createScaledBitmap(shape4, bigger4Width, bigger4Height, false);
canvas.drawARGB(100, 0, 0, 0);
canvas.drawBitmap(bigger4, centerX4, centerY4, null);
canvas.drawBitmap(bigger3, centerX3, centerY3, null);
canvas.drawBitmap(bigger2, centerX2, centerY2, null);
canvas.drawBitmap(bigger1, centerX1, centerY1, null);
bigger1Height += 30;
bigger1Width += 30;
bigger2Height += 30;
bigger2Width += 30;
bigger3Height += 30;
bigger3Width += 30;
bigger4Height += 30;
bigger4Width += 30;
}
public void pause(){
isItOk = false;
while(true){
try{
t.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume(){
isItOk = true;
t = new Thread (this);
t.start();
}
}
@Override
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
shape4 = shape3;
shape3 = shape2;
shape2 = shape1;
bigger4Height = bigger3.getHeight();
bigger4Width = bigger3.getWidth();
bigger3Height = bigger2.getHeight();
bigger3Width = bigger2.getWidth();
bigger2Height = bigger1.getHeight();
bigger2Width = bigger1.getWidth();
shape1 = BitmapFactory.decodeResource(getResources(), R.drawable.blank);
bigger1Height = 10;
bigger1Width = 10;
xOld1 =xNew1;
yOld1 = yNew1;
xNew1 = me.getX();
yNew1 = me.getY();
randomIndex1 = generator1.nextInt( 6 );
int shapeChoose = randomIndex1;
switch(shapeChoose){
case 0:
shape1 = BitmapFactory.decodeResource(getResources(), R.drawable.dia1);
break;
case 1:
shape1 = BitmapFactory.decodeResource(getResources(), R.drawable.skinny1);
break;
case 2:
shape1 = BitmapFactory.decodeResource(getResources(), R.drawable.tri1);
break;
case 3:
shape1 = BitmapFactory.decodeResource(getResources(), R.drawable.square1);
break;
case 4:
shape1 = BitmapFactory.decodeResource(getResources(), R.drawable.hex1);
break;
case 5:
shape1 = BitmapFactory.decodeResource(getResources(), R.drawable.trap1);
break;
}
break;
case MotionEvent.ACTION_UP:
xOld1 =xNew1;
yOld1 = yNew1;
xNew1 = me.getX();
yNew1 = me.getY();
break;
case MotionEvent.ACTION_MOVE:
xOld1 =xNew1;
yOld1 = yNew1;
xNew1 = me.getX();
yNew1 = me.getY();
break;
}
return true;
}
}