我使用canvas绘制位图来制作移动的屏幕和对象。但它运行缓慢。我使用某种方式作为移动屏幕图片res / drawable-xxhdpi作为建议,但它仍然很慢。
public class MainActivity extends Activity {
public static float xuser,yuser;
public static float xcomp,ycomp;
public static int vtx =100;
public static int vty =0;
public static int demsc=1;
long startTime,updatedTime;
private Handler customHandler = new Handler();
public static TextView txttime;
public static String showtime;
public static ViewGroup.LayoutParams params;
public static LinearLayout sc;
public static addUser user;
public void RunTime()
{
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
public Runnable updateTimerThread = new Runnable()
{
public void run()
{
updatedTime = SystemClock.uptimeMillis() - startTime;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
if (demsc>=24)
{
demsc=1;
}
if (milliseconds>(demsc*41.7))
{
demsc=demsc+1;
user = new addUser(MainActivity.this);
MainActivity.this.addContentView(user,params);
AddPicture aa= new AddPicture(MainActivity.this);
}
showtime="" + mins + ":" + String.format("%02d", secs) + ":" + String.format("%03d", milliseconds);
txttime.setText(showtime + " " + demsc);
customHandler.postDelayed(this, 0);
}
};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txttime = (TextView)findViewById(R.id.txttime);
sc=(LinearLayout)findViewById (R.id.screenlayout);
xuser=150; yuser=150;
xcomp=150; ycomp=150;
user = new addUser(this);
params = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
addContentView(user, params);
RunTime();
}
}
并且:
public class addUser extends View
{
public addUser(Context context)
{
super(context);
}
public void draw(Canvas canvas)
{
MainActivity.xuser=MainActivity.xuser+2;
MainActivity.xcomp=MainActivity.xcomp+2;
MainActivity.ycomp=MainActivity.ycomp+2;
int sch=MainActivity.sc.getHeight();
int scw=MainActivity.sc.getWidth();
MainActivity.vtx=MainActivity.vtx+10;
Bitmap a = BitmapFactory.decodeResource(getResources(),R.drawable.screen);
a=Bitmap.createScaledBitmap(a, scw, sch, true);
canvas.drawBitmap(a, MainActivity.vtx, 0, null);
Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.dam);
Bitmap c = BitmapFactory.decodeResource(getResources(),R.drawable.chem);
canvas.drawBitmap(b, MainActivity.xuser, MainActivity.yuser, null);
canvas.drawBitmap(c, MainActivity.xcomp, MainActivity.ycomp, null);
}
}