【问题标题】:Clipping of rotated switch view旋转开关视图的剪辑
【发布时间】:2018-06-01 15:59:34
【问题描述】:

我正在尝试在 Android 应用中旋转 Switch。我知道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;
    }
}

这使用了一种修复建议的大小的方法in this article by Lorenzo Quiroli

这是结果(第一次切换),接着是一个普通的Switch,带有一个-90android:rotation 参数,然后是一系列没有旋转的普通Switch 视图(视图边界已打开) :

您可以从绘图视图边界中看到,带有旋转的普通Switch 通常在视觉上被剪裁,因为可绘制对象延伸到边界之外,这保留了水平开关的原始尺寸。然而,自定义VerticalSwitch 具有正确的高度(这允许第二个开关显示完整的可绘制对象),但是可绘制对象偏移到视图的下半部分,并且可绘制对象仍被剪辑在底部的下方视图处于水平配置。

在调试器中检查调整大小的参数表明新的旋转尺寸正在正确应用,但裁剪仍在发生。什么原因导致偏移和削波,如何纠正?

【问题讨论】:

    标签: java android rotation android-view clipping


    【解决方案1】:

    无需创建垂直自定义Switch,您可以将android:rotation="90" 用于垂直Switch

    您只需为您的Switch 提供静态高度 试试这个

    <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>
    

    输出

    【讨论】:

    • 我得试试静态高度。我曾假设需要在正常切换视图中调整填充。感谢您的洞察力! (在自定义视图中修复剪辑的问题仍然悬而未决)
    • @RedBassett 不需要填充
    • @RedBasset 删除填充而不是尝试
    • 确认,设置高度将剪裁固定在默认的Switch 视图上。
    猜你喜欢
    • 2011-09-20
    • 2018-12-03
    • 1970-01-01
    • 2013-02-25
    • 2011-08-04
    • 1970-01-01
    • 2011-07-31
    • 2012-11-07
    • 2012-07-15
    相关资源
    最近更新 更多