自定义View控件
2013-10-31 11:27
281 查看
DrawView.java
activity_main.xml
MainActivity.java
/* * 自定义控件 * */ package com.frank.control; import android.content.Context; import android.graphics.*; import android.util.AttributeSet; import android.view.*; public class DrawView extends View { public float currentX = 40; public float currentY = 50; //定义、创建画笔 Paint p = new Paint(); public DrawView(Context context) { super(context); } public DrawView(Context context,AttributeSet set) { super(context,set); } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); //设置画笔颜色 p.setColor(Color.RED); //绘制一个小圆 canvas.drawCircle(currentX,currentY,15,p); } //为该组件的触碰事件重写事件处理方法 @Override public boolean onTouchEvent(MotionEvent event) { currentX = event.getX(); currentY = event.getY(); //通知当前组件重绘自己 super.invalidate(); //返回true表明该处理方法已经处理该事件 return true; } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:id="@+id/root" > </RelativeLayout>
MainActivity.java
package com.frank.userdfinedcontrol; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.*; //引用自己定义控件包名 import com.frank.control.*; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RelativeLayout rl = (RelativeLayout)super.findViewById(R.id.root); final DrawView draw = new DrawView(this); draw.setMinimumWidth(300); draw.setMinimumHeight(500); rl.addView(draw); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
相关文章推荐
- The requested URL returned error:406 while accessing https
- 【Map】Map.Entry的使用
- [Usaco2011 Nov]Cow Steeplechase奶牛越野跑
- STM32系统时钟 CAN UART
- ubuntu+moinmoin+nginx+uwsgi
- mybatis 插入日期类型精确到秒的有关问题
- 例题第2章
- 如何把SketchUP里创建的模型正确的导入到CityEngine中?
- 差分近似图像导数算子之Laplace算子
- Android Handler机制
- ubuntu 包下载
- Opencv学习_3 (Opencv读取视频 &视频进度控制 &写视频)
- 事件学习
- ld -l选项注意事项
- <<The Pratice of Programming>>编程风格之命名
- 关于oracle dbresp.pl进行耗尽CPU的解决办法
- 整理sar的基本用法
- Cocos2d-x ——Action (动作)简介
- OpenRisc-53-debugging the OpenRISC 1200
- Linux驱动编写的方法学习