如何将android开关小部件旋转90°

时间:2013-07-17 14:42:15

标签: java android rotation switch-statement

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

我在原始的Switch by BoD中编辑了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);

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

2 个答案:

答案 0 :(得分: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);
 }

希望有所帮助。

答案 1 :(得分:3)

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

<View
    android:rotation="90"
    ... />