0

我用 Canvas.rotate() 旋转了一个 RelativeLayout。

public class MyLayout
extends RelativeLayout
{
    @Override
    protected void dispatchDraw(Canvas canvas){
        canvas.save();
        canvas.rotate(
            ANGLE,
            getWidth()/2,
            getHeight()/2);
        super.dispatchDraw(canvas);
        canvas.restore();
    }
}

如果我在这个布局中放置了一个复选框,它在被检查后没有正确绘制,因为没有从布局的 dispatchDraw() 调用 CheckBox 的 draw()。

有人知道如何解决这个问题吗?

4

2 回答 2

0

工作解决方案:

public class MyLayout extends RelativeLayout
{
private drawn = false;
@Override
protected void dispatchDraw (Canvas canvas )
{
    canvas .save();
    canvas .rotate(
        ANGLE,
        getWidth ()/ 2,
        getHeight ()/ 2
    );
    super.dispatchDraw (canvas); 
    canvas .restore();

    if(!drawn){
        drawn = true;
        invalidate();
        drawn = false;
    }
}
}
于 2013-07-17T07:46:38.590 回答
0

看看这是不是你想要的

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Angle to rotate"/>

    <EditText 
        android:id="@+id/angle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="30"/>

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set Rotation"
        android:onClick="onClick"
        />

    <your.package.name.MyLayout
        android:id="@+id/myLayout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:visibility="visible"
        android:gravity="center" >

        <CheckBox 
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CheckMe"/>

    </your.package.name.MyLayout>

</LinearLayout>  

活动.java

public class MainActivity extends Activity {

    private MyLayout myLayout;
    private EditText edAngle;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myLayout = (MyLayout) findViewById(R.id.myLayout);
        edAngle = (EditText) findViewById(R.id.angle);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onClick(View v){

        float angleToRotate = Float.parseFloat(edAngle.getText().toString());
        myLayout.setRotationAngle(angleToRotate);
    }
}

我的布局.java

public class MyLayout
extends RelativeLayout
{
    public float myAngle = 0;
    public MyLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }    

    public MyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void dispatchDraw(Canvas canvas){
        canvas.save();
        canvas.rotate(
            myAngle,
            getWidth()/2,
            getHeight()/2);
        super.dispatchDraw(canvas);
        canvas.restore();
    }

    public void setRotationAngle(float angle){

        this.myAngle = angle;
        this.invalidate();
    }

}
于 2013-07-13T14:59:40.287 回答