0

我正在尝试将 BoD ( https://github.com/BoD/android-switch-backport ) 的反向移植开关旋转 90°。我玩过 Switch 类,最后我设法让它看起来不错(http://img706.imageshack.us/img706/8662/oslz.jpg)。但是,它不能正常工作。如果我尝试切换按钮,它会被绘制在视图下方的某个位置,所以我只看到它的一部分。

我在 BoD 的原始 Switch 中编辑了 onMeasure() 和 onDraw() 方法:

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);


    if (mOnLayout == null) {
        mOnLayout = makeLayout(mTextOn);
    }
    if (mOffLayout == null) {
        mOffLayout = makeLayout(mTextOff);
    }

    mTrackDrawable.getPadding(mTempRect);
    final int maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth());
    final int switchWidth = Math.max(mSwitchMinWidth, maxTextWidth * 2 + mThumbTextPadding * 4 +  
    mTempRect.left+ mTempRect.right);              
    final int switchHeight = mTrackDrawable.getIntrinsicHeight();

    mThumbWidth = maxTextWidth + mThumbTextPadding * 2;

    switch (widthMode) {
        case MeasureSpec.AT_MOST:
            widthSize = Math.min(widthSize, switchWidth);
            break;

        case MeasureSpec.UNSPECIFIED:
            widthSize = switchWidth;
            break;

        case MeasureSpec.EXACTLY:
            // Just use what we were given
            break;
    }

    switch (heightMode) {
        case MeasureSpec.AT_MOST:
            heightSize = Math.min(heightSize, switchHeight);
            break;

        case MeasureSpec.UNSPECIFIED:
            heightSize = switchHeight;
            break;

        case MeasureSpec.EXACTLY:
            // Just use what we were given
            break;
    }

这是编辑后的代码 - 我替换了这个块:

    mSwitchWidth = switchWidth;
    mSwitchHeight = switchHeight

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    final int measuredHeight = getMeasuredHeight();
    if (measuredHeight < switchHeight) {
    setMeasuredDimension(getMeasuredWidth(), switchHeight);
    }
    }

有了这个:

    mSwitchWidth = switchHeight;
    mSwitchHeight = switchWidth;

    super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    final int measuredWidth = getMeasuredWidth();
    if (measuredWidth < switchWidth) {
        //setMeasuredDimension(getMeasuredWidthAndState(), switchHeight);
        setMeasuredDimension(switchHeight, switchWidth);
    }    
    }   

我将这两行添加到 onDraw() 方法中:

    canvas.translate(getWidth(), 0);
    canvas.rotate(90);

我在这里通过 BoD 上传了 Switch 的原始代码:http://pastebin.com/8CU9ybET 我也想知道,在 onLayout() 方法中使用了 mSwitchWidth 和 mSwitchHeight,所以也许我也应该以某种方式调整它......谢谢你有任何想法ppl ;)

4

2 回答 2

4

我有一个类似的问题。我解决如下;

首先,在 /res/anim/ 中放置一个名为 rotation.xml 的文件

<?xml version="1.0" encoding="utf-8"?>
<rotate
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="-90"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="0" android:fillAfter="true">
</rotate>

然后,在您的 Activity 的 onCreate 中,执行

    @Override
      public void onCreate(Bundle icicle) {
      super.onCreate(icicle);

     setContentView(R.layout.myscreen);

     Animation rotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotation);
     LayoutAnimationController animController = new LayoutAnimationController(rotateAnim, 0);
     FrameLayout layout = (FrameLayout)findViewById(R.id.MyScreen_ContentLayout);
     layout.setLayoutAnimation(animController);
 }

希望有帮助。

于 2013-07-17T14:50:59.257 回答
3

4年后,有人可能会发现这个答案很有用。视图的整个旋转可以通过在该视图的 xml 文件中添加一行来处理,例如:

<View
    android:rotation="90"
    ... /> 
于 2018-03-19T11:10:31.753 回答