• 自定义View(二)增加View的属性


         增加View的属性有两种方法    1、在View类中添加    2、在xml资源文件中添加

    一、在View类中添加    例:实现一个带文字的图片

      

    public class MyView extends View {
        
        private String mtext;
        private int msrc;
    
        public MyView(Context context) {
            super(context);
        }
    
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
            int resourceId = 0;
            int textId = attrs.getAttributeResourceValue(null, "Text",0);
            int srcId = attrs.getAttributeResourceValue(null, "Src", 0);
            mtext = context.getResources().getText(textId).toString();
            msrc = srcId;
        }
        
        @Override
        protected void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            InputStream is = getResources().openRawResource(msrc); 
                    Bitmap mBitmap = BitmapFactory.decodeStream(is);
                    int bh = mBitmap.getHeight();
                    int bw = mBitmap.getWidth();
                canvas.drawBitmap(mBitmap, 0,0, paint);
            //canvas.drawCircle(40, 90, 15, paint);
            canvas.drawText(mtext, bw/2, 30, paint);
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <com.example.myimageview2.MyView
            android:id="@+id/myView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            Text="@string/hello_world"
            Src="@drawable/xh"/>
    
    </LinearLayout>

    二、 在xml资源文件中添加

    public class MyImageView extends LinearLayout {
    
        public MyImageView(Context context) {
            super(context);
        }
    
        public MyImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            int resourceId = -1;
            TypedArray typedArray = context.obtainStyledAttributes(attrs,
                    R.styleable.MyImageView);
            ImageView iv = new ImageView(context);
            TextView tv = new TextView(context);
            int N = typedArray.getIndexCount();
            for (int i = 0; i < N; i++) {
                int attr = typedArray.getIndex(i);
                switch (attr) {
                case R.styleable.MyImageView_Oriental:
                    resourceId = typedArray.getInt(
                            R.styleable.MyImageView_Oriental, 0);
                    this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL
                            : LinearLayout.VERTICAL);
                    break;
                case R.styleable.MyImageView_Text:
                    resourceId = typedArray.getResourceId(
                            R.styleable.MyImageView_Text, 0);
                    tv.setText(resourceId > 0 ? typedArray.getResources().getText(
                            resourceId) : typedArray
                            .getString(R.styleable.MyImageView_Text));
                    break;
                case R.styleable.MyImageView_Src:
                    resourceId = typedArray.getResourceId(
                            R.styleable.MyImageView_Src, 0);
                    iv.setImageResource(resourceId > 0 ?resourceId:R.drawable.ic_launcher);
                    break;   
                }
            }
            addView(iv);
            addView(tv);
            typedArray.recycle();
        }
    }

    在values文件夹下的  attrs.xml中声明属性

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <declare-styleable name="MyImageView">
            <attr name="Text" format="reference|string"></attr>
            <attr name="Oriental" >
                <enum name="Horizontal" value="1"></enum>
                <enum name="Vertical" value="0"></enum>
            </attr>
            <attr name="Src" format="reference|integer"></attr>
        </declare-styleable>
    
    </resources>


     

    在MainActivity中布局文件的使用   必须声明红色部分 才能在自定义的View中使用绿色部分的属性

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
        <com.example.myimageview2.MyImageView
            android:id="@+id/myImageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            uview:Text="这是一个图片说明" 
            uview:Src="@drawable/tw"
            uview:Oriental="Vertical">
        </com.example.myimageview2.MyImageView>
    
    </LinearLayout>
  • 相关阅读:
    Excel的小游戏总结
    借助“URLScan”工具隐藏header头服务器信息
    WinCe设备连接Win10系统
    WinForm 通过HttpWebRequest实现大文件上传
    Sql 动态行转列 pivot
    C# 调用LAKALA接口获取静态二维码数据
    蜗牛星际黑群晖硬盘休眠的设置
    GIT Windows服务端搭建笔记
    C#通过socket判断FTP服务器是否通畅并判断用户名密码是否正确
    C#获取MAC地址
  • 原文地址:https://www.cnblogs.com/bimingcong/p/5807387.html
Copyright © 2020-2023  润新知