zoukankan      html  css  js  c++  java
  • Android编程动态创建视图View的方法

    Android开 发中,在Activity中关联视图View是一般使用setContentView方法,该方法一种参数是使用XML资源直接创 建:setContentView (int layoutResID),指定layout中的一个XML的ID即可,这种方法简单。另一个方法是 setContentView(android.view.View),参数是指定一个视图View对象,这种方法可以使用自定义的视图类。

    在一些场合中,需要对View进行一些定制处理,比如获取到Canvas进行图像绘制,需要重载View::onDraw方法,这时需要对View 进行派生一个类,重载所需要的方法,然后使用setContentView(android.view.View)与Activity进行关联,具体代码 举例如下:

    1. public class temp extends Activity {  
    2.     /** 在Activity中关联视图view */  
    3.     @Override  
    4.     public void onCreate(Bundle savedInstanceState) {  
    5.         super.onCreate(savedInstanceState);  
    6.         setContentView(new DrawView(this));  
    7.     }  
    8.     /*自定义类*/  
    9.     private class DrawView extends View {  
    10.         private Paint paint;  
    11.         /** 
    12.          * Constructor 
    13.          */  
    14.         public DrawView(Context context) {  
    15.             super(context);  
    16.             paint = new Paint();  
    17.             // set's the paint's colour   
    18.             paint.setColor(Color.GREEN);  
    19.             // set's paint's text size   
    20.             paint.setTextSize(25);  
    21.             // smooth's out the edges of what is being drawn   
    22.             paint.setAntiAlias(true);  
    23.         }  
    24.   
    25.         protected void onDraw(Canvas canvas) {  
    26.             super.onDraw(canvas);  
    27.             canvas.drawText("Hello World"530, paint);  
    28.             // if the view is visible onDraw will be called at some point in the   
    29.             // future   
    30.             invalidate();  
    31.         }  
    32.     }  
    33. }  

    第二个例子,动态绘图

    1. public class MyAndroidProjectActivity extends Activity {  
    2.     /** Called when the activity is first created. */  
    3.     /* 
    4.     public void onCreate(Bundle savedInstanceState) { 
    5.         super.onCreate(savedInstanceState); 
    6.         setContentView(R.layout.main); 
    7.     }*/  
    8.     static int times = 1;  
    9.        
    10.     /** Called when the activity is first created. */  
    11.     @Override  
    12.     public void onCreate(Bundle savedInstanceState) {  
    13.         super.onCreate(savedInstanceState);  
    14.         setContentView(new DrawView(this));  
    15.    
    16.     }  
    17.     private class DrawView extends View {  
    18.         Paint vPaint = new Paint();  
    19.         private int i = 0;  
    20.         public DrawView(Context context) {  
    21.             super(context);  
    22.   
    23.         }  
    24.   
    25.         protected void onDraw(Canvas canvas) {  
    26.             super.onDraw(canvas);  
    27.             System.out.println("this run " + (times++) +" times!");  
    28.   
    29.             // 设定绘图样式   
    30.             vPaint.setColor( 0xff00ffff ); //画笔颜色   
    31.             vPaint.setAntiAlias( true );   //反锯齿   
    32.             vPaint.setStyle( Paint.Style.STROKE );  
    33.   
    34.             // 绘制一个弧形   
    35.             canvas.drawArc(new RectF(60120260320), 0, i, true, vPaint );  
    36.   
    37.             // 弧形角度   
    38.             if( (i+=10) > 360 )  
    39.                 i = 0;  
    40.   
    41.             // 重绘, 再一次执行onDraw 程序   
    42.             invalidate();  
    43.         }  
    44.     }  
    45.           
    46. }  

  • 相关阅读:
    [转载] set IDENTITY_INSERT on 和 off 的设置
    固定GridView的表头和某几列
    图形文件格式小常识
    MVP——ModelViewerPresenter [ZT]
    VB.NET 中图形旋转任意角度 [ZT]
    工厂方法模式和抽象工厂模式之比较 [Z]
    Visual C# 语言概念数据类型(C# 与 Java)
    LINQ 中使用 Distinct 的 Compare过滤重复的字段
    How to load AJAX content into current Colorbox window?
    解决FTP服务器FileZilla server中文乱码问题
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3694785.html
Copyright © 2011-2022 走看看