如何在旋转布局中绘制CheckBox?

时间:2013-07-12 18:05:54

标签: android layout rotation draw

我使用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()。

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

2 个答案:

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

Activity.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);
    }
}

MyLayout.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();
    }

}

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