Java 如何制作方形框架布局?
我尝试通过扩展android.widget.FrameLayout来创建自定义mysqueframe类,并通过传递自定义宽度和高度来覆盖onMeasure方法Java 如何制作方形框架布局?,java,android,android-layout,android-view,android-framelayout,Java,Android,Android Layout,Android View,Android Framelayout,我尝试通过扩展android.widget.FrameLayout来创建自定义mysqueframe类,并通过传递自定义宽度和高度来覆盖onMeasure方法 public class MySquareFrame extends FrameLayout { public MySquareFrame(Context context) { super(context); } public MySquareFrame(Context context, AttributeSet attrs)
public class MySquareFrame extends FrameLayout {
public MySquareFrame(Context context) {
super(context);
}
public MySquareFrame(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MySquareFrame(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MySquareFrame(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width > height ? height : width;
setMeasuredDimension(size, size);
}
}
在xml中使用这个
<com.example.akash.view.MySquareFrame
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background3">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/fuel_meter"
android:rotation="120"
/>
<com.triggertrap.seekarc.SeekArc
android:id="@+id/seekArc"
android:layout_width="match_parent"
android:layout_height="match_parent"
seekarc:max="120"
android:padding="70dp"
seekarc:rotation="180"
seekarc:startAngle="30"
seekarc:sweepAngle="300"
seekarc:touchInside="false"
seekarc:clockwise="false"
seekarc:thumb="@drawable/nob" />
</com.example.akash.view.MySquareFrame>
这有助于我根据屏幕尺寸获得方形框架。检查,一个Android库,它为不同的布局提供了一个包装类,在不丢失任何核心功能的情况下将它们呈现为方形
尺寸是在渲染布局之前计算的,因此在获得视图后不会重新渲染或进行任何调整
要使用该库,请将其添加到build.gradle中:
repositories {
maven {
url "https://maven.google.com"
}
}
dependencies {
compile 'com.github.kaushikthedeveloper:squarelayout:0.0.3'
}
检查一下,一个Android库,它为不同的布局提供了一个包装类,在不丢失任何核心功能的情况下使它们成为正方形
尺寸是在渲染布局之前计算的,因此在获得视图后不会重新渲染或进行任何调整
要使用该库,请将其添加到build.gradle中:
repositories {
maven {
url "https://maven.google.com"
}
}
dependencies {
compile 'com.github.kaushikthedeveloper:squarelayout:0.0.3'
}
您可以在android开发者网站上看到的实现:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSize == 0 && heightSize == 0) {
// If there are no constraints on size, let FrameLayout measure
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Now use the smallest of the measured dimensions for both dimensions
final int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
setMeasuredDimension(minSize, minSize);
return;
}
final int size;
if (widthSize == 0 || heightSize == 0) {
// If one of the dimensions has no restriction on size, set both dimensions to be the
// on that does
size = Math.max(widthSize, heightSize);
} else {
// Both dimensions have restrictions on size, set both dimensions to be the
// smallest of the two
size = Math.min(widthSize, heightSize);
}
final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
super.onMeasure(newMeasureSpec, newMeasureSpec);
}
您可以在android开发者网站上看到的实现:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSize == 0 && heightSize == 0) {
// If there are no constraints on size, let FrameLayout measure
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Now use the smallest of the measured dimensions for both dimensions
final int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
setMeasuredDimension(minSize, minSize);
return;
}
final int size;
if (widthSize == 0 || heightSize == 0) {
// If one of the dimensions has no restriction on size, set both dimensions to be the
// on that does
size = Math.max(widthSize, heightSize);
} else {
// Both dimensions have restrictions on size, set both dimensions to be the
// smallest of the two
size = Math.min(widthSize, heightSize);
}
final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
super.onMeasure(newMeasureSpec, newMeasureSpec);
}
我已经找到了一个非常“简化”的解决方案。按如下所示创建您自己的OneFrameLayout,然后将此FrameLayout添加到xml中:
public class OneOneFrameLayout extends FrameLayout {
public OneOneFrameLayout(@NonNull Context context) {
super(context);
}
public OneOneFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public OneOneFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public OneOneFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int snHeight = MeasureSpec.getSize(widthMeasureSpec);
int snHeightSpec = MeasureSpec.makeMeasureSpec(snHeight, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, snHeightSpec);
}
}
我已经找到了一个非常“简化”的解决方案。按如下所示创建您自己的OneFrameLayout,然后将此FrameLayout添加到xml中:
public class OneOneFrameLayout extends FrameLayout {
public OneOneFrameLayout(@NonNull Context context) {
super(context);
}
public OneOneFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public OneOneFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public OneOneFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int snHeight = MeasureSpec.getSize(widthMeasureSpec);
int snHeightSpec = MeasureSpec.makeMeasureSpec(snHeight, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, snHeightSpec);
}
}
为了使用
GridLayoutManager
获得方形视图,Koopa给出了一个很好的答案,但您已经有了widthmasurespec
。
宽度是最重要的,然后获得与宽度匹配的高度:
package com.my.package;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class SquareFrameLayout extends FrameLayout {
public SquareFrameLayout(@NonNull Context context) {
super(context);
}
public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec); //<-- Width + Width...
}
}
package com.my.package;
导入android.content.Context;
导入android.util.AttributeSet;
导入android.widget.FrameLayout;
导入androidx.annotation.NonNull;
导入androidx.annotation.Nullable;
公共类SquareFrameLayout扩展了FrameLayout{
公共SquareFrameLayout(@NonNull上下文){
超级(上下文);
}
public SquareFrameLayout(@NonNull上下文,@Nullable属性集属性){
超级(上下文,attrs);
}
public SquareFrameLayout(@NonNull上下文,@Nullable AttributeSet attrs,int defStyleAttr){
super(上下文、attrs、defStyleAttr);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(widthMeasureSpec,widthMeasureSpec)//
为了使用GridLayoutManager
获得方形视图,Koopa给出了一个很好的答案,但您已经有了widthmasurespec
。
宽度是最重要的,然后获得与宽度匹配的高度:
package com.my.package;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class SquareFrameLayout extends FrameLayout {
public SquareFrameLayout(@NonNull Context context) {
super(context);
}
public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec); //<-- Width + Width...
}
}
package com.my.package;
导入android.content.Context;
导入android.util.AttributeSet;
导入android.widget.FrameLayout;
导入androidx.annotation.NonNull;
导入androidx.annotation.Nullable;
公共类SquareFrameLayout扩展了FrameLayout{
公共SquareFrameLayout(@NonNull上下文){
超级(上下文);
}
public SquareFrameLayout(@NonNull上下文,@Nullable属性集属性){
超级(上下文,attrs);
}
public SquareFrameLayout(@NonNull上下文,@Nullable AttributeSet attrs,int defStyleAttr){
super(上下文、attrs、defStyleAttr);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(widthMeasureSpec,widthMeasureSpec)//
使用显示度量获取显示的宽度,并将高度设置为与代码中的宽度相同的高度,以使其显示为方形。谢谢。成功了。请更新代码“statingupdated
”其他用户使用显示度量获取显示宽度,并将高度设置为与代码中的宽度相同的高度,以使其显示为方形,这将对其他用户很有帮助。谢谢。成功了。更新您的代码“statingupdated
”。这将对其他用户很有帮助链接为404。文件现在位于GitHub上:链接为404。文件现在位于GitHub上: