引用:http://www.oschina.net/code/snippet_54100_6262

Android中自带的view种类很多,但是有时候不能满足我们的需求,下面介绍一种自定义view的方法,实现了拖动矩形到屏幕任意位置的需求。 标签: Android SDK

代码片段(5)

[图片]程序截图

[代码]Activity.java

01 packagecom.zhuozhuo;
02
03 importandroid.app.Activity;
04 importandroid.os.Bundle;
05
06 publicclassCSDNActivityextendsActivity {
07 /** Called when the activity is first created. */
08 @Override
09 publicvoidonCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.main);
12 }
13
14
15 }

[代码]CustomView.java

01 packagecom.zhuozhuo;
02
03 importandroid.content.Context;
04 importandroid.graphics.Canvas;
05 importandroid.graphics.Color;
06 importandroid.graphics.Paint;
07 importandroid.graphics.Rect;
08 importandroid.util.AttributeSet;
09 importandroid.view.MotionEvent;
10 importandroid.view.View;
11
12 /**
13 * 自定义的view,需要覆盖onDraw()方法绘制控件,覆盖onTouchEvent()接收触摸消息
14 */
15 publicclassCustomViewextendsView {
16
17 privatestaticfinalintWIDTH =40;
18
19 privateRect rect =newRect(0,0, WIDTH, WIDTH);//绘制矩形的区域
20 privateintdeltaX,deltaY;//点击位置和图形边界的偏移量
21 privatestaticPaint paint =newPaint();//画笔
22
23 publicCustomView(Context context, AttributeSet attrs) {
24 super(context, attrs);
25 paint =newPaint();
26 paint.setColor(Color.RED);//填充红色
27 }
28
29 @Override
30 protectedvoidonDraw(Canvas canvas) {
31 canvas.drawRect(rect, paint);//画矩形
32
33 }
34
35 @Override
36 publicbooleanonTouchEvent (MotionEvent event) {
37 intx = (int) event.getX();
38 inty = (int) event.getY();
39 switch(event.getAction()) {
40 caseMotionEvent.ACTION_DOWN:
41 if(!rect.contains(x, y)) {
42 returnfalse;//没有在矩形上点击,不处理触摸消息
43 }
44 deltaX = x - rect.left;
45 deltaY = y - rect.top;
46 break;
47 caseMotionEvent.ACTION_MOVE:
48 caseMotionEvent.ACTION_UP:
49 Rect old =newRect(rect);
50 //更新矩形的位置
51 rect.left = x - deltaX;
52 rect.top = y - deltaY;
53 rect.right = rect.left + WIDTH;
54 rect.bottom = rect.top + WIDTH;
55 old.union(rect);//要刷新的区域,求新矩形区域与旧矩形区域的并集
56 invalidate(old);//出于效率考虑,设定脏区域,只进行局部刷新,不是刷新整个view
57 break;
58 }
59 returntrue;//处理了触摸消息,消息不再传递
60 }
61
62 }

[代码]main.xml 布局文件

1 <?xmlversion="1.0"encoding="utf-8"?>
2 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <com.zhuozhuo.CustomViewandroid:layout_width="fill_parent"
8 android:layout_height="fill_parent"/>
9 </LinearLayout>

[文件]工程打包下载 ~39KB下载(6)

更多相关文章

  1. Android开发学习之电话、短信、联系人
  2. Textview与Button设置圆角
  3. Android(安卓)基于paho的mqtt service的工具类
  4. android 带图片的文本框
  5. android 控制软键盘显示和隐藏
  6. android中 异步消息处理机制及Handler
  7. android源码中常用的Rect方法
  8. Android(安卓)面试题总结(一)
  9. EventBus两个Activity传值

随机推荐

  1. Android实习笔记----调用拨号器,邮件短信
  2. android O 对后台服务的限制
  3. Android(安卓)播放视频的方法+播放测试地
  4. Some Standard Activity Actions
  5. Android十日学习路线
  6. xmlns:android="http://schemas.android.
  7. Use logCat in android native
  8. Android资料整理
  9. android中如何执行java命令
  10. android Intent机制详解