Android编程动态创建视图View的方法
时间:2022-03-10 15:15
在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进行关联,具体代码 举例如下:
- public class temp extends Activity {
- /** 在Activity中关联视图view */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new DrawView(this));
- }
- /*自定义类*/
- private class DrawView extends View {
- private Paint paint;
- /**
- * Constructor
- */
- public DrawView(Context context) {
- super(context);
- paint = new Paint();
- // set‘s the paint‘s colour
- paint.setColor(Color.GREEN);
- // set‘s paint‘s text size
- paint.setTextSize(25);
- // smooth‘s out the edges of what is being drawn
- paint.setAntiAlias(true);
- }
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- canvas.drawText("Hello World", 5, 30, paint);
- // if the view is visible onDraw will be called at some point in the
- // future
- invalidate();
- }
- }
- }
第二个例子,动态绘图
- public class MyAndroidProjectActivity extends Activity {
- /** Called when the activity is first created. */
- /*
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }*/
- static int times = 1;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new DrawView(this));
- }
- private class DrawView extends View {
- Paint vPaint = new Paint();
- private int i = 0;
- public DrawView(Context context) {
- super(context);
- }
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- System.out.println("this run " + (times++) +" times!");
- // 设定绘图样式
- vPaint.setColor( 0xff00ffff ); //画笔颜色
- vPaint.setAntiAlias( true ); //反锯齿
- vPaint.setStyle( Paint.Style.STROKE );
- // 绘制一个弧形
- canvas.drawArc(new RectF(60, 120, 260, 320), 0, i, true, vPaint );
- // 弧形角度
- if( (i+=10) > 360 )
- i = 0;
- // 重绘, 再一次执行onDraw 程序
- invalidate();
- }
- }
- }
Android编程动态创建视图View的方法,布布扣,bubuko.com
相关推荐
- Android系统编程入门系列之界面Activity交互响应
- 新型横向移动工具原理分析、代码分析、优缺点以及检测方案
- uni-app滚动视图容器(scroll-view)之监听上拉事件
- uniapp h5,app两端复制文本
- Android系统编程入门系列之界面Activity响应丝滑的传统动画
- 【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn
- 诺基亚短信生成!太好玩了
- iOS 跳转App Store进行评分
- 开发一个即时通讯App
- 关闭苹果IOS app自动更新
电脑软件
本类排行
- 1关闭苹果IOS app自动更新
- 2iOS 跳转App Store进行评分
- 3诺基亚短信生成!太好玩了
- 4Android系统编程入门系列之界面Activity响应丝滑的传统动画
- 5uniapp h5,app两端复制文本
- 6uni-app滚动视图容器(scroll-view)之监听上拉事件
- 7新型横向移动工具原理分析、代码分析、优缺点以及检测方案
- 8Android系统编程入门系列之界面Activity交互响应
- 9开发一个即时通讯App
- 10【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn