Android开发中获取不同手机屏幕的宽度和高度

 我来答
春新快马打擂台C
2012-07-28 · TA获得超过551个赞
知道答主
回答量:222
采纳率:0%
帮助的人:153万
展开全部
继承SurfaceView 类写的也可以继承View类写
public class GetWHSurfaceView extends SurfaceView implements Callback,Runnable{

private int textX = 20, textY = 20;
private int screenHeight;
private int screenWidth;
private SurfaceHolder sfh;
private boolean flag;
private Thread th;
//画布
private Canvas canvas;
public GetWHSurfaceView(Context context) {
super(context);

sfh = this.getHolder();
sfh.addCallback(this);
canvas = new Canvas();
Paint paintText;

paintText = new Paint();
this.setKeepScreenOn(true);
setFocusable(true);
setFocusableInTouchMode(true);
}

/**
* 重写触屏事件函数
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
//玩家手指点击屏幕的动作
if (event.getAction() == MotionEvent.ACTION_DOWN) {
textX = x;
textY = y;
//玩家手指抬起离开屏幕的动作
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
textX = x;
textY = y;
//玩家手指在屏幕上移动的动作
} else if (event.getAction() == MotionEvent.ACTION_UP) {
textX = x;
textY = y;
}
return true;
}
public void myDraw()
{
//绘制背景色
canvas = sfh.lockCanvas();
try {
if(canvas != null)
{
//创建一个画笔的实例
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), paint);
//设置画笔的颜色
paint.setColor(Color.WHITE);
//绘制文本
canvas.drawText(String.valueOf(textX), textX, textY, paint);
canvas.drawText(String.valueOf(textY), textX+25, textY, paint);
canvas.drawText(String.valueOf(screenWidth), 280, 20, paint);
canvas.drawText(String.valueOf(screenHeight),280+20, 20, paint);
}
} catch (Exception e) {

}finally
{
sfh.unlockCanvasAndPost(canvas);
}

}
@Override
public void run() {
while(flag)
{
long start = System.currentTimeMillis();
myDraw();
long end = System.currentTimeMillis();
try {
if(end - start < 50)
{
Thread.sleep(50 -(end-start));
System.out.print("run");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
screenHeight = this.getHeight();
screenWidth = this.getWidth();
flag = true;
th = new Thread(this);
th.start();

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}

}
wantyer
2012-07-27 · 超过51用户采纳过TA的回答
知道小有建树答主
回答量:88
采纳率:0%
帮助的人:94.1万
展开全部
直接贴代码给你了兄弟:
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
//得到屏幕宽高
String showSize = "手机屏幕分辨率: \n"+ display.getWidth()+ "*" + display.getHeight();
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式