最近开发Android的一些总结

遇到的问题如下:

1.在xml里自定义view报异常。

public GameView(Context context, AttributeSet attrs) {//注:在xml里使用这一个类必须得添加该构造函数
		super(context, attrs);
		paint = new Paint();
		//在view里获取分辨率
		DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;
		//per_Row = screenHeight/ ROW;
		//per_Col = (screenWidth - 20)/ COL;
		//init();
		//canvas = new Canvas();
		bmp = new Bitmap[id.length];
		setFocusable(true);
		setFocusableInTouchMode(true);
	}
2.xml自定义view大小如何确定
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		width = w / ROW;
		height = h / COL;
		System.out.println("每列的宽度为:" + width);
		super.onSizeChanged(w, h, oldw, oldh);
	}
3.代码修改图片合适方法;
	Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height,
							Bitmap.Config.ARGB_8888);
					Drawable drw;
					Canvas canvas1 = new Canvas(bitmap);
					drw = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), grid[i][j]));
					drw.setBounds(0, 0, (int)width, (int)height);
					drw.draw(canvas1);
					if(grid[i][j] != 0)
					canvas.drawBitmap(bitmap, i * width, j * height, null);


 
  
4.在view中获取屏幕分辨率
 
  

 
  
DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;

5.统计字符串宽度:
 
  
Paint paint= new  Paint();
			paint.setColor(Color.RED);
			String str = "当前分数为" + score + " 剩余时间: " + time + "  级别:" + lv;
			canvas.drawText(str, (screenWidth - new Paint().measureText(str))/2, 20, paint);

好了,就这么多了,以后遇到再继续补充
 
  

 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(Android)