最近开发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);

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









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值