Java 图像以横向模式拉伸
(图片附在问题底部) 我有这样的布局:Java 图像以横向模式拉伸,java,android,imageview,android-linearlayout,landscape,Java,Android,Imageview,Android Linearlayout,Landscape,(图片附在问题底部) 我有这样的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_paren
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainLayout">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:paddingTop="10dp"
android:layout_height="match_parent"
android:id="@+id/viewsLayout"
android:layout_weight="1.0">
</LinearLayout>
</LinearLayout>
我想在内部线性布局“viewsLayout”中添加3x3个球的图像视图,因此在其相应的类中,我将这样做:
LinearLayout main = (LinearLayout) findViewById(R.id.viewsLayout);
main.setWeightSum(3.0f);
for (int i = 0; i < 3; ++i) {
LinearLayout currentRow = new LinearLayout(this);
currentRow.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
params.weight = 1.0f;
currentRow.setLayoutParams(params);
for (int j = 0; j < 3; ++j) {
ImageView viewOne = new ImageView(this);
viewOne.setBackgroundResource(R.drawable.ball);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
viewOne.setLayoutParams(params);
viewOne.setScaleType(ImageView.ScaleType.FIT_CENTER);
viewOne.setAdjustViewBounds(true);
currentRow.addView(viewOne);
}
main.addView(currentRow);
}
LinearLayout main=(LinearLayout)findViewById(R.id.viewsLayout);
main.setWeightSum(3.0f);
对于(int i=0;i<3;++i){
LinearLayout currentRow=新的LinearLayout(此);
currentRow.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父项,0);
参数重量=1.0f;
currentRow.setLayoutParams(参数);
对于(int j=0;j<3;++j){
ImageView viewOne=新的ImageView(此);
视图一.挫折背景资源(R.可牵引球);
params=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父级,LinearLayout.LayoutParams.MATCH_父级,1.0f);
viewOne.setLayoutParams(参数);
viewOne.setScaleType(ImageView.ScaleType.FIT_CENTER);
viewOne.setAdjustViewBounds(真);
currentRow.addView(viewOne);
}
main.addView(当前行);
}
问题是,在横向模式下,球会变成椭圆形(拉伸过大),正如您在所附图片中看到的那样
我看到这个问题已经被问过了,不建议避免使用横向模式。(我正在添加一张图片,以便于搜索者查找。)我看到解决方案是为横向模式创建一个新布局,但我有很多布局,因此为横向模式的每个布局创建另一个布局可能会很糟糕。有更好的解决办法吗?如果不是,我是否必须按dp设置linearLayout\ImageView的宽度,而不匹配父项
如果解决方案简短且易于编写,请告诉我。您使用的是R.drawable.ball。是png文件吗?也许您应该尝试使用9补丁文件,并避免在编辑器中为9补丁文件格式拉伸文件
如果适合您的情况,您可以通过drawable绘制球。首先,您的图像大小可能不够宽,无法适应横向模式。为此,您需要一个单独的图像以适应横向模式 其次,您可以尝试创建一个在横向模式下触发的横向布局。创建一个布局文件夹并为其创建一个单独的xml 参考这些链接,
您正在设置ImageView的背景,而不是资源,因此缩放/调整ViewBounds不会影响它。尝试
viewOne.setImageResource(R.drawable.ball)事实上,这还不是你想要的。如果你想让3x3像那样适合中心,你必须使用不同的横向膨胀代码,以保持它们的适当权重设置图像资源后看起来很棒!但为什么你认为这不是我想要的?也许我的解释不好,或者可能有我现在不期望的行为?现在看起来更完美了!:)如何将您的评论设置为我问题的答案?是的,它是一个png文件。但是,尽管我使用的是9面片图像,但图像仍处于拉伸状态。可能是因为match_parent width..尝试在9面片图像文件中指定应如何拉伸,正如此处所述,类似问题在此处所述[link]