新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

自定义View-创新互联

一.View结构原理

创新互联基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业服务器托管报价,主机托管价格性价比高,为金融证券行业乐山服务器托管,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。

    Android系统对视图结构的设计采用了组合模式,即View作为所有图形的基类,ViewGoup对View进行扩展为视图容器类。

    View定义了绘图的基本操作:measure(),layout(),draw()。其内部又分别包含了onMeasure(),onLayout(),onDraw()三个子方法。

    1.measure操作

        用于计算视图的长宽,是final类型的。measure函数又会调用onMeasure().

        onMeasure(),视图大小将最终在这里确定。即measure是对onMeasure的包装,子类可以覆

        盖onMeasure()来实现计算视图大小的方式,并通过setMeasuredDimension(width,height)

        保存结果。

    2.layout操作

        设置视图在屏幕中显示的位置,final类型,有以下2个基本操作

      (1)setFrame(l,t,r,b),参数即子视图在父视图中的具体位置,该函数用于保存这些参数。

      (2)onLayout(),在View函数中什么都不做,它是为ViewGroup布局子视图用的。

    3.draw操作

        draw操作利用前2步得到的参数将视图显示在屏幕上。子类也不应该修改该方法,因为其内部

     定义了绘图的基本操作:

        (1)绘制背景;(2)如果要视图显示渐变框,这里会做一些准备工作;(3)绘制视图本身

      即调用onDraw()。ViewGroup不需要实现该函数,因为容器是“没有内容”的,它只需要告诉子    view绘制自己就行了,也就是下面的dispatchDraw()方法;(4)绘制子视图,即

     dispatchDraw()函数。View不需要实现该方法,容器类必须实现。(5)如果需要(应用程序调

     用了setVerticalFadingEdge之类),开始绘制渐变框;(6)绘制滚动条;

        从上面看出自定义View最少应覆盖onMeasure()和onDraw()方法。

二.View类的构造方法

(1)继承已有控件(要实现的控件与已有控件类似时采用该方法)

(2)继承布局文件(要组合控件时用),注意此时不用onDraw方法,它是通过inflater视图然后         addView(view)

(3)继承View类,使用GDI绘制出组件界面

三.自定义View增加属性的2种方法:

(1)在View类中定义。通过构造函数引入的AttributeSet去查找XML布局中的属性名称,然后找到它对应引用的资源ID去找值。

  

    android:id="@+id/myView1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

     Text="@string/hello_world"

     Src="@drawable/xh"/>

        自定义Text和Src属性。

public class MyView extends View {

    public MyView(Context context, AttributeSet attrs) {

        super(context, attrs);

        int resourceId = 0;

        //获取xml中自定义的Text和Src属性

        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);

    }

}

(2)通过XML为View注册属性。与Android提供的标准属性写法一样。

  

    

    

      

      

    

    

  

public class MyImageView extends LinearLayout {

  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();

  }

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:自定义View-创新互联
本文地址:http://www.wjwzjz.com/article/dpedho.html
在线咨询
服务热线
服务热线:028-86922220
TOP