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

问题描述 投票:0回答:2

我正在尝试将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;)

java android rotation switch-statement
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);
 }

希望能有所帮助。


3
投票

4年后,有人可能会觉得此答案有用。 可以通过向视图的xml文件中仅添加一行来处理视图旋转的整个过程,例如:

<View
    android:rotation="90"
    ... /> 
© www.soinside.com 2019 - 2024. All rights reserved.