WEB开发网
开发学院手机开发Android 开发 android自定义控件:可旋转View:可作为ImageView... 阅读

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

 2010-05-14 16:58:00 来源:WEB开发网   
核心提示:"http://com.bill.cn"; /** * 保存创建旋转角度 */ private float mRotateDegrees; /** * 保存创建背景图片的ID */ private int mBackGroudDrawableId; /** * 利用图片ID加载图片 */ priva
"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使用控件:

view plaincopy to clipboardprint?

1. < ?xml version="1.0" encoding="utf-8"? >

2. < RelativeLayout

3. xmlns:android="http://schemas.android.com/apk/res/android"

4. xmlns:bill="http://com.bill.cn"

5. android:id="@+id/background"

6. android:layout_width="wrap_content"

上一页  1 2 3 4  下一页

Tags:android 定义 控件

编辑录入:coldstar [复制链接] [打 印]
赞助商链接