Java 旋转开关视图的裁剪

Java 旋转开关视图的裁剪,java,android,rotation,android-view,clipping,Java,Android,Rotation,Android View,Clipping,我正在尝试在Android应用程序中旋转开关。我知道android:rotation参数,但由于这是应用程序的一个常见部分,所以我正在构建一个扩展开关的自定义视图。默认情况下,对视图应用旋转将保留未旋转视图的原始尺寸,因此此实现应切换宽度和高度参数以适应新方向: public class VerticalSwitch extends Switch { // Init method called from all constructors private void init(Contex

我正在尝试在Android应用程序中旋转开关。我知道android:rotation参数,但由于这是应用程序的一个常见部分,所以我正在构建一个扩展开关的自定义视图。默认情况下,对视图应用旋转将保留未旋转视图的原始尺寸,因此此实现应切换宽度和高度参数以适应新方向:

public class VerticalSwitch extends Switch {

// Init method called from all constructors
    private void init(Context context, …) {
        // Rotate the view
        setRotation(switchOrientation.ordinal()*90);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();

        int desiredWidth = height + getPaddingLeft() + getPaddingRight();
        int desiredHeight = width + getPaddingTop() + getPaddingBottom();

        //noinspection SuspiciousNameCombination
        setMeasuredDimension(measureDimension(desiredWidth, widthMeasureSpec),
                measureDimension(desiredHeight, heightMeasureSpec));
    }

    private int measureDimension(int desiredSize, int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = desiredSize;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }

        if (result < desiredSize){
            Log.e(TAG, "The view is too small, the content might get cut");
        }
        return result;
    }
}
这使用了一种固定建议尺寸的方法

这是第一个开关的结果,后面是一个正常开关,android:rotation参数为-90,后面是一系列正常开关视图,没有打开旋转视图边界:

从绘制视图边界可以看到,随着可绘制对象延伸到边界之外,带旋转的法线开关通常会在视觉上被剪裁,而边界保留了水平开关的原始尺寸。但是,自定义垂直开关具有正确的高度,这允许第二个开关显示完整的可绘制内容,但是可绘制内容偏移到视图的下半部分,并且可绘制内容仍剪裁在视图底部处于水平配置的位置下方


在调试器中检查大小调整的参数表明,新的旋转尺寸已正确应用,但剪裁仍在发生。是什么导致了偏移和剪切,以及如何纠正它?

无需创建垂直自定义开关您可以使用android:rotation=90进行垂直开关

你需要给你的开关一个静态的高度 试试这个

输出


我得试试静态高度。我假设需要在正常开关视图中调整填充。谢谢你的洞察力!在自定义中修复剪辑的问题仍然存在view@RedBassett填充不needed@RedBasset删除填充,然后尝试确认,设置高度将固定默认开关视图上的剪辑。
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp">

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:rotation="90" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:rotation="90" />


    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>