Dan*_*del 5 android surfaceview
我试图有一个surfaceview(一个扩展surfaceview的自定义类),我在xml布局中添加了一个固定大小比例:4:3(长度到宽度).
我希望它能够尽可能多地填充它的父级,无论是长度还是宽度,但是一旦它达到全长或宽度,它将调整另一侧以具有我之前说过的固定大小比率.
我怎么能做到这样的事情?
谢谢.
在活动的onCreate方法中,您可以读取屏幕的分辨率并为其设置布局宽度和高度.
假设您正在使用RealtiveLayout的示例:
private void create43RatioSurface() {
SurfaceView surfaceView43 = (SurfaceView)findViewById(R.id.surfaceView1);
DisplayMetrics metrics = getResources().getDisplayMetrics();
int height = 0;
int width = 0;
if(metrics.widthPixels < metrics.heightPixels){
width = metrics.widthPixels;
height= (metrics.widthPixels/4) * 3 ;
} else {
height= metrics.heightPixels;
width= (metrics.heightPixels/4) * 3 ;
}
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
surfaceView43.setLayoutParams(layoutParams);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8317 次 |
| 最近记录: |