依然是android用户体验问题,IPhone的多方向旋转支持要优于android,android的框架原本支持两个方向Portrait、Landscape。
要实现三种方向Portrait、Landscape、Landscape-reverse需要三种布局,但是三种布局用三套资源是很浪费的,尤其是图形化程序。
为此自定义一个可指定三种方向控件,是很有必要的。
自定义的View:
- /**
- *
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.drawable.Drawable;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.View;
- /**
- * @author Bill
- * @rotateDegrees 仅支持90.0 180.0 0.0(默认)
- * @background 图片资源文件
- */
- public class RotateableView extends View{
- /**
- * 命名区域
- */
- private final String namespace = "http://com.bill.cn";
- /**
- * 保存创建旋转角度
- */
- private float mRotateDegrees;
- /**
- * 保存创建背景图片的ID
- */
- private int mBackGroudDrawableId;
- /**
- * 利用图片ID加载图片
- */
- private Drawable mBackGroudDrawable;
- /**
- * 原始图片所需宽、高
- */
- private int mBackGroundWidth;
- private int mBackGroundHeight;
- public RotateableView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- mBackGroudDrawableId=attrs.getAttributeResourceValue(namespace, "background", R.drawable.protractor_landscape_background);
- mBackGroudDrawable = context.getResources().getDrawable(
- mBackGroudDrawableId);
- mRotateDegrees=attrs.getAttributeFloatValue(namespace, "rotateDegrees",0.0f);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- /**
- * 旋转画布
- */
- if (mRotateDegrees==90.0f) {
- canvas.rotate(mRotateDegrees, 0, 0);
- canvas.translate(0, -mBackGroundHeight);
- } else {
- canvas.rotate(mRotateDegrees, mBackGroundWidth/2, mBackGroundHeight/2);
- }
- /**
- * 执行draw
- */
- mBackGroudDrawable.setBounds(0, 0, mBackGroundWidth, mBackGroundHeight);
- mBackGroudDrawable.draw(canvas);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- // TODO Auto-generated method stub
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- /**
- * 设定View显示区域
- */
- mBackGroundHeight=mBackGroudDrawable.getMinimumHeight();
- mBackGroundWidth=mBackGroudDrawable.getMinimumWidth();
- if (mRotateDegrees==90.0f) {
- setMeasuredDimension(mBackGroundHeight, mBackGroundWidth);
- } else {
- setMeasuredDimension(mBackGroundWidth, mBackGroundHeight);
- }
- }
- }
XML使用控件:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:bill="http://com.bill.cn"
- android:id="@+id/background"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <com.***.RotateableView
- android:layout_width="wrap_conten
- android:layout_height="wrap_content"
- bill:rotateDegrees="180.0"
- bill:background="@drawable/protractor_landscape_background" />
- </RelativeLayout>