旋转ImageView和TextView的效果实现

本文介绍了一个自定义的Android视图组件IconViewGroup,该组件可以实现ImageView和TextView组合并进行整体旋转的功能。代码中详细展示了如何通过继承ViewGroup来创建这个组件,并设置其旋转角度及背景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



如图ImageView和TextView组成布局同时旋转。,求实现效果。
经过几天研究终于实现如图效果。代码如下,给需要的人吧。还涉及到部分背景图的效果。

public class IconViewGroup extends ViewGroup{
        TextView mTextView;
        ImageView mImageView;//显示对应的icon
        private Context mContext;
        /**
         * 命名区域
         */
        private final String namespace = "http://com.oppo.examples"; 
        /**
         * 保存创建旋转角度
         */
        private float mRotateDegrees; 
        /**
         * 保存创建背景图片的ID
         */
        private int mBackGroudDrawableId;
        /**
         * 利用图片ID加载图片
         */
        private Drawable mBackGroudDrawable;
        /**
         * 原始图片所需宽、高
         */
        private int mBackGroundWidth;
        private int mBackGroundHeight;
        private View view;
        
        private static final int ICON_WIDTH=46;//显示数字的TextView的长宽
        private static final int ICON_HEIGHT=46;
        
        private static final int TEXT_WIDTH=46;//显示数字的TextView的长宽
        private static final int TEXT_HEIGHT=260;
        
        public IconViewGroup(Context context) {
                super(context);
                // TODO Auto-generated constructor stub
        }
        public IconViewGroup(Context context, AttributeSet attrs) {
                super(context, attrs);
                init(context);
                mContext = context;
                mBackGroudDrawableId=attrs.getAttributeResourceValue(namespace, "background", R.drawable.item_on);
                mBackGroudDrawable = context.getResources().getDrawable(
                                mBackGroudDrawableId);                
                mRotateDegrees=attrs.getAttributeFloatValue(namespace, "rotateDegrees",30.0f);                
        }

        private void init(Context context) {
                // TODO Auto-generated method stub
                mTextView=new MyTextView(context);
                mTextView.setTextSize(28);
                mTextView.setGravity(Gravity.CENTER_VERTICAL);
//                mTextView.setBackgroundColor(0xFFFFFF00);
                mTextView.setText(R.string.app_name);
                mTextView.setTextColor(0xF0FFFFFF);
                mImageView=new ImageView(context);
                mImageView.setBackgroundResource(R.drawable.icon);
                this.addView(mTextView);
                this.addView(mImageView);
        }

        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
                // TODO Auto-generated method stub
                int ivWidth=mImageView.getMeasuredWidth();
                int ivHeight=mImageView.getMeasuredHeight();
                int tvWidth=mTextView.getMeasuredWidth();
                int tvHeight=mTextView.getMeasuredHeight();
                mTextView.measure(r - l, b - t);
                mTextView.layout(5+ivWidth+10, getHeight()-tvHeight-5,getWidth(),getHeight()-5);
                mImageView.measure(r - l, b - t);
                mImageView.layout(5, 5+getHeight()-ivHeight, ivWidth,getHeight());

        }
        
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                // TODO Auto-generated method stub
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                /**
                 * 设定View显示区域
                 */
                mBackGroundHeight=mBackGroudDrawable.getIntrinsicHeight();
                mBackGroundWidth=mBackGroudDrawable.getIntrinsicWidth();
                if (mRotateDegrees==90.0f) {
                        setMeasuredDimension(mBackGroundHeight, mBackGroundWidth);
                } else {
                        setMeasuredDimension(mBackGroundWidth, mBackGroundHeight);
                }                
        }
        
        @Override
        protected void dispatchDraw(Canvas canvas) {
                // TODO Auto-generated method stub
                canvas.rotate(mRotateDegrees);
                mBackGroudDrawable.setBounds(0, 0, mBackGroundWidth, mBackGroundHeight);
                mBackGroudDrawable.draw(canvas);
                super.dispatchDraw(canvas);
        }        
        
}


Android中ViewGroup等容器控件的使用
http://blog.youkuaiyun.com/arui319/archive/2010/09/07/5868466.aspx
  • 大小: 4.1 KB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值