将 ImageView 置于中心,宽度和高度为父宽度的一半?
Placing an ImageView in centre with width and height to be half of parent width?
我想在 ViewGroup(最好是 RelativeLayout)中放置一个 ImageView,如下所示。 View 组被拉伸以获取 activity 的完整宽度和高度。我想确保无论设备宽度是什么,ImageView 的高度和宽度都应该是它的一半..
╔═══════════════════════╗
║ ║
║ View Group ║
║ ║
║ ╔═════════════╗ ║
║ ║ ║ ║
║ ║ ImageView ║ ║
║ ║ ║ ║
║ ║ width = X/2 ║ ║
║ ║ height= X/2 ║ ║
║ ╚═════════════╝ ║
║ ║
║ ║
║ ║
╚═══════════════════════╝
<----------------------->
Xpx width of Device/Viewgroup
有人可以帮助我如何实现这一目标吗?
要使 ImageView 方形实现像这样的 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 的 LinearLayout 以及两个虚拟不可见视图(之前和之后),全部使用 layout_height=0
并为 SquareImageView
设置 layout_weigth=0.5
和另外两个每个 layout_weigth=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);
}
}
可能没有必要对父级进行 null 检查,但应该相应地调整图像大小
看看这个(我已经添加了评论)
activity.main.xml
<?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>
MainActivity.java
//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));
将此代码放入 MainActivity
class 的 onCreate
方法中。
最后来个最终效果:
希望对您有所帮助
我想在 ViewGroup(最好是 RelativeLayout)中放置一个 ImageView,如下所示。 View 组被拉伸以获取 activity 的完整宽度和高度。我想确保无论设备宽度是什么,ImageView 的高度和宽度都应该是它的一半..
╔═══════════════════════╗
║ ║
║ View Group ║
║ ║
║ ╔═════════════╗ ║
║ ║ ║ ║
║ ║ ImageView ║ ║
║ ║ ║ ║
║ ║ width = X/2 ║ ║
║ ║ height= X/2 ║ ║
║ ╚═════════════╝ ║
║ ║
║ ║
║ ║
╚═══════════════════════╝
<----------------------->
Xpx width of Device/Viewgroup
有人可以帮助我如何实现这一目标吗?
要使 ImageView 方形实现像这样的 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 的 LinearLayout 以及两个虚拟不可见视图(之前和之后),全部使用 layout_height=0
并为 SquareImageView
设置 layout_weigth=0.5
和另外两个每个 layout_weigth=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);
}
}
可能没有必要对父级进行 null 检查,但应该相应地调整图像大小
看看这个(我已经添加了评论)
activity.main.xml
<?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>
MainActivity.java
//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));
将此代码放入 MainActivity
class 的 onCreate
方法中。
最后来个最终效果:
希望对您有所帮助