Android 将ImageView放置在中心,宽度和高度为父视图宽度的一半?
我想将ImageView放置在一个视图组(最好是相对视图)中,如下所示。视图组被拉伸,以获取活动的全宽和全高。我想确保无论设备的宽度是多少,ImageView的高度和宽度都应该是它的一半Android 将ImageView放置在中心,宽度和高度为父视图宽度的一半?,android,android-imageview,Android,Android Imageview,我想将ImageView放置在一个视图组(最好是相对视图)中,如下所示。视图组被拉伸,以获取活动的全宽和全高。我想确保无论设备的宽度是多少,ImageView的高度和宽度都应该是它的一半 ╔═══════════════════════╗ ║ ║ ║ View Group ║ ║ ║ ║ ╔═════════════╗ ║ ║ ║
╔═══════════════════════╗
║ ║
║ View Group ║
║ ║
║ ╔═════════════╗ ║
║ ║ ║ ║
║ ║ ImageView ║ ║
║ ║ ║ ║
║ ║ width = X/2 ║ ║
║ ║ height= X/2 ║ ║
║ ╚═════════════╝ ║
║ ║
║ ║
║ ║
╚═══════════════════════╝
<----------------------->
Xpx width of Device/Viewgroup
╔═══════════════════════╗
║ ║
║ 视图组║
║ ║
║ ╔═════════════╗ ║
║ ║ ║ ║
║ ║ 图像视图║ ║
║ ║ ║ ║
║ ║ 宽度=X/2║ ║
║ ║ 高度=X/2║ ║
║ ╚═════════════╝ ║
║ ║
║ ║
║ ║
╚═══════════════════════╝
Xpx设备/视图组的宽度
有人能帮助我如何实现这一点吗?让ImageView square实现这样的SquareImageView
public class SquareImageView extends ImageView {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = getMeasuredHeight();
setMeasuredDimension(height, height);
}
然后将其与verticl以及两个虚拟不可见视图(前后)一起添加到线性布局中,所有视图均具有
布局高度=0
,并设置为方形图像视图a布局高度=0.5
,对于其他两个布局高度=0.25
可以尝试以下方法:
public class CenteredImageView extends ImageView {
public CenteredImageView(Context context) {
super(context);
}
public CenteredImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CenteredImageView(Context context, AttributeSet attrs, int id) {
super(context, attrs, id);
}
@Override
public void onMeasure(int measuredWidth, int measuredHeight) {
super.onMeasure(measuredWidth, measuredHeight);
ViewGroup parent = (ViewGroup) getParent();
if(parent != null){
int width = parent.getMeasuredWidth() / 2;
setMeasuredDimension(width, width);
}
}
可能没有必要对父对象执行空检查,但是应该相应地调整图像的大小看看这个(我已经添加了注释)
activity.main.xml
将此代码放入MainActivity
类的onCreate
方法中
最后,这里是最后一个效果:
希望对你有所帮助看看这篇文章。你应该能够使用这个解决方案,他说以父母的一半宽度和一半高度为中心从他的描述中我看到它的平方宽度=X/2高度=X/2真的,“设备/视图组的Xpx宽度”没有看到那部分谢谢你的回答很有帮助:)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.piotr.myapplication.MainActivity"
android:id="@+id/content"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/centered"/>
</RelativeLayout>
//Set layout Gravity as centered
RelativeLayout content = (RelativeLayout) findViewById(R.id.content);
content.setGravity(Gravity.CENTER);
//This is your image which would be in the middle
ImageView centeredImage = (ImageView) findViewById(R.id.centered);
//get Screen Dimensions
DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
//NOTE: If you want to square, just use one of these value.
//set as half of dimens
centeredImage.getLayoutParams().height = height/2;
centeredImage.getLayoutParams().width = width/2;
//Add an color
centeredImage.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
//Also possible
//centeredImage.setBackgroundColor(Color.BLUE);
//Add image from resources
//centeredImage.setImageDrawable(getResources().getDrawable(R.mipmap.ic_launcher));