Android可在全屏幕自由拖动的view

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


[代码] Activity.java

[java]  view plain copy print ?
  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


[java]  view plain copy print ?
  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. /** 
  13.  * 自定义的view,需要覆盖onDraw()方法绘制控件,覆盖onTouchEvent()接收触摸消息 
  14.  */  
  15. public class CustomView extends View {  
  16.    
  17.     private static final int WIDTH = 40;  
  18.        
  19.     private Rect rect = new Rect(00, WIDTH, WIDTH);//绘制矩形的区域  
  20.     private int deltaX,deltaY;//点击位置和图形边界的偏移量  
  21.     private static Paint paint = new Paint();//画笔  
  22.        
  23.     public CustomView(Context context, AttributeSet attrs) {  
  24.         super(context, attrs);  
  25.         paint = new Paint();  
  26.         paint.setColor(Color.RED);//填充红色  
  27.     }  
  28.        
  29.     @Override  
  30.     protected void onDraw(Canvas canvas) {  
  31.         canvas.drawRect(rect, paint);//画矩形  
  32.    
  33.     }  
  34.        
  35.     @Override  
  36.     public boolean onTouchEvent (MotionEvent event) {  
  37.         int x = (int) event.getX();  
  38.         int y = (int) event.getY();  
  39.         switch(event.getAction()) {  
  40.         case MotionEvent.ACTION_DOWN:  
  41.             if(!rect.contains(x, y)) {  
  42.                 return false;//没有在矩形上点击,不处理触摸消息  
  43.             }  
  44.             deltaX = x - rect.left;  
  45.             deltaY = y - rect.top;  
  46.             break;  
  47.         case MotionEvent.ACTION_MOVE:  
  48.         case MotionEvent.ACTION_UP:  
  49.             Rect old = new Rect(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.         return true;//处理了触摸消息,消息不再传递  
  60.     }  
  61.    
  62. }  


[代码] main.xml 布局文件

[html]  view plain copy print ?
  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>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值