依然是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>

更多相关文章

  1. Android(安卓)3D 游戏开发教程– Part I-VI
  2. Android(安卓)button原理 转载
  3. Android图片异步加载之Android-Universal-Image-Loader
  4. Android图片异步加载之Android-Universal-Image-Loader
  5. [Android] 使用Matrix矩阵类对图像进行缩放、旋转、对比度、亮度
  6. Android适配(屏幕适配、国际化适配)
  7. android 学习八 android selector的使用
  8. [Android] Android开发规范
  9. Android之Animation

随机推荐

  1. Android官方DataBinding(十):双向绑定之基于
  2. Android引路蜂地图开发示例:概述
  3. Chris:怎样成为一名Android应用开发
  4. android sqlite用管理工具查看
  5. Android中SharedPrerence的apply和commit
  6. Android声音管理AudioManager使用
  7. Android第三方登陆之新浪微博Weibo篇(原生
  8. Android实现录屏直播+远程控制之MediaCod
  9. Android 数据库批量查询数据的操作
  10. Android多线程--Handler