实现自定义view(1):可在全屏幕自由拖动的view

转自:http://blog.csdn.net/mylzc/article/details/6740877

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

下图为运行效果图:



废话不多说,直接上代码

Activity.java

[java] view plaincopy
  1. package com.zhuozhuo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class CSDNActivity extends Activity {  
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.     }  
  13.       
  14.       
  15. }  

CustomView.java 自定义的view,需要覆盖onDraw()方法绘制控件,覆盖onTouchEvent()接收触摸消息

[java] view plaincopy
  1. package com.zhuozhuo;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Rect;  
  8. import android.util.AttributeSet;  
  9. import android.view.MotionEvent;  
  10. import android.view.View;  
  11.   
  12. public class CustomView extends View {  
  13.   
  14.     private static final int WIDTH = 40;  
  15.       
  16.     private Rect rect = new Rect(00, WIDTH, WIDTH);//绘制矩形的区域  
  17.     private int deltaX,deltaY;//点击位置和图形边界的偏移量  
  18.     private static Paint paint = new Paint();//画笔  
  19.       
  20.     public CustomView(Context context, AttributeSet attrs) {  
  21.         super(context, attrs);  
  22.         paint = new Paint();  
  23.         paint.setColor(Color.RED);//填充红色  
  24.     }  
  25.       
  26.     @Override  
  27.     protected void onDraw(Canvas canvas) {  
  28.         canvas.drawRect(rect, paint);//画矩形  
  29.   
  30.     }  
  31.       
  32.     @Override  
  33.     public boolean onTouchEvent (MotionEvent event) {  
  34.         int x = (int) event.getX();  
  35.         int y = (int) event.getY();  
  36.         switch(event.getAction()) {  
  37.         case MotionEvent.ACTION_DOWN:  
  38.             if(!rect.contains(x, y)) {  
  39.                 return false;//没有在矩形上点击,不处理触摸消息  
  40.             }  
  41.             deltaX = x - rect.left;  
  42.             deltaY = y - rect.top;  
  43.             break;  
  44.         case MotionEvent.ACTION_MOVE:  
  45.         case MotionEvent.ACTION_UP:  
  46.             Rect old = new Rect(rect);  
  47.             //更新矩形的位置  
  48.             rect.left = x - deltaX;  
  49.             rect.top = y - deltaY;  
  50.             rect.right = rect.left + WIDTH;  
  51.             rect.bottom = rect.top + WIDTH;  
  52.             old.union(rect);//要刷新的区域,求新矩形区域与旧矩形区域的并集  
  53.             invalidate(old);//出于效率考虑,设定脏区域,只进行局部刷新,不是刷新整个view  
  54.             break;  
  55.         }  
  56.         return true;//处理了触摸消息,消息不再传递  
  57.     }  
  58.   
  59. }  
main.xml 布局文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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.CustomView android:layout_width="fill_parent"  
  8.     android:layout_height="fill_parent"/>  
  9. </LinearLayout>  

工程打包下载
原文地址:https://www.cnblogs.com/walccott/p/4957610.html