android自定义控件:可旋转View:可作为ImageView、ImageButton

 依然是android用户体验问题,IPhone的多方向旋转支持要优于android,android的框架原本支持两个方向Portrait、Landscape。

要实现三种方向Portrait、Landscape、Landscape-reverse需要三种布局,但是三种布局用三套资源是很浪费的,尤其是图形化程序。

 

为此自定义一个可指定三种方向控件,是很有必要的。

 

自定义的View:

  1. /** 
  2.  *  
  3. import android.content.Context; 
  4. import android.graphics.Canvas; 
  5. import android.graphics.drawable.Drawable; 
  6. import android.util.AttributeSet; 
  7. import android.util.Log; 
  8. import android.view.View; 
  9. /** 
  10.  * @author Bill 
  11.  * @rotateDegrees 仅支持90.0 180.0 0.0(默认) 
  12.  * @background 图片资源文件 
  13.  */  
  14. public class RotateableView extends View{  
  15.     /** 
  16.      * 命名区域 
  17.      */  
  18.     private final String namespace = "http://com.bill.cn";   
  19.     /** 
  20.      * 保存创建旋转角度 
  21.      */  
  22.     private float mRotateDegrees;   
  23.     /** 
  24.      * 保存创建背景图片的ID 
  25.      */  
  26.     private int mBackGroudDrawableId;  
  27.     /** 
  28.      * 利用图片ID加载图片 
  29.      */  
  30.     private Drawable mBackGroudDrawable;  
  31.     /** 
  32.      * 原始图片所需宽、高 
  33.      */  
  34.     private int mBackGroundWidth;  
  35.     private int mBackGroundHeight;  
  36.     public RotateableView(Context context, AttributeSet attrs) {  
  37.         super(context, attrs);  
  38.         // TODO Auto-generated constructor stub  
  39.         mBackGroudDrawableId=attrs.getAttributeResourceValue(namespace, "background", R.drawable.protractor_landscape_background);  
  40.         mBackGroudDrawable = context.getResources().getDrawable(  
  41.                 mBackGroudDrawableId);        
  42.         mRotateDegrees=attrs.getAttributeFloatValue(namespace, "rotateDegrees",0.0f);         
  43.     }  
  44.     @Override  
  45.     protected void onDraw(Canvas canvas) {  
  46.         // TODO Auto-generated method stub  
  47.         super.onDraw(canvas);  
  48.         /** 
  49.          * 旋转画布 
  50.          */  
  51.         if (mRotateDegrees==90.0f) {  
  52.             canvas.rotate(mRotateDegrees, 00);  
  53.             canvas.translate(0, -mBackGroundHeight);  
  54.         } else {  
  55.             canvas.rotate(mRotateDegrees, mBackGroundWidth/2, mBackGroundHeight/2);  
  56.         }  
  57.         /** 
  58.          * 执行draw 
  59.          */  
  60.         mBackGroudDrawable.setBounds(00, mBackGroundWidth, mBackGroundHeight);  
  61.         mBackGroudDrawable.draw(canvas);  
  62.     }  
  63.     @Override  
  64.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  65.         // TODO Auto-generated method stub  
  66.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  67.         /** 
  68.          * 设定View显示区域 
  69.          */  
  70.         mBackGroundHeight=mBackGroudDrawable.getMinimumHeight();  
  71.         mBackGroundWidth=mBackGroudDrawable.getMinimumWidth();  
  72.         if (mRotateDegrees==90.0f) {  
  73.             setMeasuredDimension(mBackGroundHeight, mBackGroundWidth);  
  74.         } else {  
  75.             setMeasuredDimension(mBackGroundWidth, mBackGroundHeight);  
  76.         }         
  77.     }     
  78. }  

 

XML使用控件:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"        
  3.    xmlns:bill="http://com.bill.cn"   
  4.    android:id="@+id/background"   
  5.    android:layout_width="wrap_content"   
  6.    android:layout_height="wrap_content">   
  7. <com.***.RotateableView   
  8.    android:layout_width="wrap_conten   
  9.    android:layout_height="wrap_content"   
  10.    bill:rotateDegrees="180.0"  
  11.    bill:background="@drawable/protractor_landscape_background" />   
  12. </RelativeLayout>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值