首页 > 移动平台 > 详细

Android画一个随意拖动的圆形

时间:2016-02-17 12:24:32      阅读:444      评论:0      收藏:0      [点我收藏+]

技术分享

  1   2 
  3 
  4 import android.content.Context;
  5 import android.graphics.Bitmap;
  6 import android.graphics.BitmapFactory;
  7 import android.graphics.Canvas;
  8 import android.graphics.Color;
  9 import android.graphics.Matrix;
 10 import android.graphics.Paint;
 11 import android.graphics.PorterDuff;
 12 import android.graphics.PorterDuffXfermode;
 13 import android.graphics.Rect;
 14 import android.graphics.RectF;
 15 import android.view.MotionEvent;
 16 import android.view.View;
 17 
 18 
 19 /**
 20  * Created by songximing on 15/12/25.
 21  */
 22 public class DraggableView extends View {
 23       /* (non-Javadoc)
 24          * @see android.view.View#onDraw(android.graphics.Canvas)
 25          */
 26 
 27     protected float currentx = 0;// 拖拽控件的x坐标
 28     protected float currenty = 0;// 拖拽控件的y坐标
 29     private int r = 0;//拖拽球的半径
 30     private boolean state = false;//判断控件是否应该获得焦点
 31     private Paint paint = null;//画笔
 32     private Paint paintLine = null;//画笔线
 33     private static int ALPHA_1 = 50;//画笔的透明度为半透明
 34     private static int ALPHA_2 = 255;//画笔的透明度不透明
 35     private float downX = 0f;//判断是否移动了x
 36     private float downY = 0f;//判断是否移动了y
 37     private Context context = null;//上下文
 38     private DraggableView.ViewCallBack callBack = null;//回调
 39 
 40     public DraggableView(Context context, DraggableView.ViewCallBack callBack) {
 41         super(context);
 42         this.context = context;
 43         this.callBack = callBack;
 44         initPaint();
 45     }
 46 
 47     @Override
 48     protected void onDraw(Canvas canvas) {
 49         super.onDraw(canvas);
 50         drawView(canvas, 12, 11);
 51     }
 52 
 53     @Override
 54     public boolean onTouchEvent(MotionEvent event) {
 55 
 56         float x = event.getX();//获取点击的横坐标
 57         float y = event.getY();//获取点击的纵坐标
 58 
 59         //触摸事件的触发
 60         switch (event.getAction()) {
 61             case MotionEvent.ACTION_DOWN://触摸点击动作
 62                 downX = x;
 63                 downY = y;
 64                 if (!isOption(x, y)) {
 65                     state = false;
 66                     return false;
 67                 } else {
 68                     paint.setAlpha(ALPHA_2);
 69                     this.invalidate();
 70                     state = true;
 71                 }
 72                 break;
 73             case MotionEvent.ACTION_MOVE://触摸移动动作
 74                 if (state) {
 75                     viewMove(x, y, event);
 76                     this.invalidate();
 77                 }
 78                 break;
 79             case MotionEvent.ACTION_UP://触摸离开动作
 80                 paint.setAlpha(ALPHA_1);//设置画笔为半透明
 81                 this.invalidate();
 82                 if (downX == x && downY == y) {
 83                     callBack.finishActivity(context);
 84                 }
 85                 break;
 86 
 87         }
 88         return true;
 89     }
 90 
 91     /**
 92      * 画控件
 93      *
 94      * @param canvas 画板
 95      * @param with   控件的宽度比例
 96      * @param heigh  控件的高度比例
 97      */
 98 
 99     private void drawView(Canvas canvas, int with, int heigh) {
100 
101         if (getWidth() < getHeight()) {
102             r = getWidth() / with;
103         } else {
104             r = getHeight() / heigh;
105         }
106         //如果是第一次画,画起始位置
107         if (currentx == 0 && currenty == 0) {
108             currentx = getWidth() - r;
109             currenty = getHeight() - 3 * r;
110             paint.setAlpha(ALPHA_1);
111         }
112         //画一个圆形bitmap
113         Bitmap bt1 = BitmapFactory.decodeResource(getResources(), R.mipmap.ww);
114         Bitmap bt2 = zoomImg(bt1, 2 * r, 2 * r);
115         Bitmap bt = toRoundBitmap(bt2);
116         canvas.drawBitmap(bt, currentx - r, currenty - r, paint);
117         //可以改为图片资源
118         /*
119         //也可以改为用画笔画一个圆球
120         canvas.drawCircle(currentx, currenty, r, paint);
121         int l = r / 4;
122         canvas.drawLine(currentx - l, currenty - l, currentx + l, currenty + l, paintLine);
123         canvas.drawLine(currentx - l, currenty + l, currentx + l, currenty - l, paintLine);
124         */
125     }
126 
127     /**
128      * 初试化画笔
129      */
130     private void initPaint() {
131         paint = new Paint();
132         paint.setColor(Color.RED);//设置画笔的颜色
133         paintLine = new Paint();
134         paintLine.setColor(Color.WHITE);
135         paintLine.setStrokeWidth(5);
136     }
137 
138     /**
139      * 设置滑动的效果
140      *
141      * @param x     点击的x坐标轴
142      * @param y     点击的y坐标轴
143      * @param event 控件的事件
144      */
145     private void viewMove(float x, float y, MotionEvent event) {
146         if (x <= r) {
147             currentx = r;
148         } else if (x >= getWidth() - r) {
149             currentx = getWidth() - r;
150         } else if (y <= r) {
151             currenty = r;
152         } else if (y >= getHeight() - r) {
153             currenty = getHeight() - r;
154         } else {
155             currentx = event.getX();
156             currenty = event.getY();
157         }
158     }
159 
160     /**
161      * 判断是不是在控件可操作的范围之内
162      *
163      * @param x 点击的x坐标轴
164      * @param y 点击的y坐标轴
165      */
166     private boolean isOption(float x, float y) {
167         if (x > currentx - r && x < currentx + r && y < currenty + r & y > currenty - r)
168             return true;
169         else
170             return false;
171     }
172 
173     /**
174      * 回调
175      */
176     public interface ViewCallBack {
177         public void finishActivity(Context context);
178     }
179 
180     public Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
181         // 获得图片的宽高
182         int width = bm.getWidth();
183         int height = bm.getHeight();
184         // 计算缩放比例
185         float scaleWidth = ((float) newWidth) / width;
186         float scaleHeight = ((float) newHeight) / height;
187         // 取得想要缩放的matrix参数
188         Matrix matrix = new Matrix();
189         matrix.postScale(scaleWidth, scaleHeight);
190         // 得到新的图片   www.2cto.com
191         Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
192         return newbm;
193     }
194 
195     /**
196      * 转换图片成圆形
197      *
198      * @param bitmap 传入Bitmap对象
199      * @return
200      */
201     public Bitmap toRoundBitmap(Bitmap bitmap) {
202         int width = bitmap.getWidth();
203         int height = bitmap.getHeight();
204         float roundPx;
205         float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
206         if (width <= height) {
207             roundPx = width / 2;
208             top = 0;
209             bottom = width;
210             left = 0;
211             right = width;
212             height = width;
213             dst_left = 0;
214             dst_top = 0;
215             dst_right = width;
216             dst_bottom = width;
217         } else {
218             roundPx = height / 2;
219             float clip = (width - height) / 2;
220             left = clip;
221             right = width - clip;
222             top = 0;
223             bottom = height;
224             width = height;
225             dst_left = 0;
226             dst_top = 0;
227             dst_right = height;
228             dst_bottom = height;
229         }
230 
231         Bitmap output = Bitmap.createBitmap(width,
232                 height, Bitmap.Config.ARGB_8888);
233         Canvas canvas = new Canvas(output);
234 
235         final int color = 0xff424242;
236         final Paint paint = new Paint();
237         final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
238         final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
239         final RectF rectF = new RectF(dst_left, dst_top, dst_right, dst_bottom);
240 
241         paint.setAntiAlias(true);
242 
243         canvas.drawARGB(0, 0, 0, 0);
244         paint.setColor(color);
245 
246         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
247 
248         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
249         canvas.drawBitmap(bitmap, src, dst, paint);
250         return output;
251     }
252 }

调用:

 1  2 
 3 import android.content.Context;
 4 import android.content.Intent;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.RelativeLayout;
 9 
10 public class MainActivity extends AppCompatActivity {
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         RelativeLayout relativeLayout = new RelativeLayout(this);//(RelativeLayout) findViewById(R.id.rl);
16         DraggableView draggableView  = new DraggableView(this,new DraggableView.ViewCallBack(){
17 
18             @Override
19             public void finishActivity(Context context) {
20                 Intent intent = new Intent(MainActivity.this,SecondActivity.class);
21                 startActivity(intent);
22                 overridePendingTransition(R.anim.slide_in, R.anim.no_anim);
23             }
24         });
25         relativeLayout.addView(draggableView);
26 
27         setContentView(relativeLayout);
28     }
29 }

 

Android画一个随意拖动的圆形

原文:http://www.cnblogs.com/weifengzz/p/5194638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!