看看这是不是你想要的
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();
}
}