import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.MotionEvent;
import android.view.View; /**
* Created by songximing on 15/12/25.
*/
public class DraggableView extends View {
/* (non-Javadoc)
* @see android.view.View#onDraw(android.graphics.Canvas)
*/ protected float currentx = 0;// 拖拽控件的x坐标
protected float currenty = 0;// 拖拽控件的y坐标
private int r = 0;//拖拽球的半径
private boolean state = false;//判断控件是否应该获得焦点
private Paint paint = null;//画笔
private Paint paint1 = null;//画笔
private Paint paintLine = null;//画笔线
private Paint paintText = null;//画文字
private static int ALPHA_1 = 50;//画笔的透明度为半透明
private static int ALPHA_2 = 255;//画笔的透明度不透明
private float downX = 0f;//判断是否移动了x
private float downY = 0f;//判断是否移动了y
private Context context = null;//上下文
private DraggableView.ViewCallBack callBack = null;//回调 public DraggableView(Context context, DraggableView.ViewCallBack callBack) {
super(context);
this.context = context;
this.callBack = callBack;
initPaint();
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawView(canvas, 50, 11);
} @Override
public boolean onTouchEvent(MotionEvent event) { float x = event.getX();//获取点击的横坐标
float y = event.getY();//获取点击的纵坐标 //触摸事件的触发
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN://触摸点击动作
downX = x;
downY = y;
if (!isOption(x, y)) {
state = false;
return false;
} else {
paint.setAlpha(ALPHA_2);
paint1.setAlpha(ALPHA_2);//设置画笔为半透明
this.invalidate();
state = true;
}
break;
case MotionEvent.ACTION_MOVE://触摸移动动作
if (state) {
viewMove(x, y, event);
this.invalidate();
}
break;
case MotionEvent.ACTION_UP://触摸离开动作
paint.setAlpha(ALPHA_1);//设置画笔为半透明
paint1.setAlpha(ALPHA_1);//设置画笔为半透明
this.invalidate();
if (downX == x && downY == y) {
callBack.finishActivity(context);
}
break; }
return true;
} /**
* 画控件
*
* @param canvas 画板
* @param with 控件的宽度比例
* @param heigh 控件的高度比例
*/ private void drawView(Canvas canvas, int with, int heigh) { // if (getWidth() < getHeight()) {
// r = getWidth() / with;
// } else {
// r = getHeight() / heigh;
// }
r = with;
//如果是第一次画,画起始位置
if (currentx == 0 && currenty == 0) {
currentx = getWidth() - r;
currenty = getHeight() - 3 * r;
paint.setAlpha(ALPHA_1);
paint1.setAlpha(ALPHA_1);
}
//画一个圆形bitmap
// Bitmap bt1 = BitmapFactory.decodeResource(getResources(), R.mipmap.ww);
// Bitmap bt2 = zoomImg(bt1, 2 * r, 2 * r);
// Bitmap bt = toRoundBitmap(bt2);
// canvas.drawBitmap(bt, currentx - r, currenty - r, paint);
//可以改为图片资源 //用画笔画一个圆球(不使用字体)
canvas.drawCircle(currentx, currenty, r, paint1);
canvas.drawCircle(currentx, currenty, r - 5, paint);
int l = r / 4;
canvas.drawLine(currentx - l, currenty - l, currentx + l, currenty + l, paintLine);
canvas.drawLine(currentx - l, currenty + l, currentx + l, currenty - l, paintLine); //用画笔画一个圆球(使用字体)
//canvas.drawCircle(currentx, currenty, r, paint1);
//canvas.drawCircle(currentx, currenty, r-5, paint);
//float bottom = paintText.getFontMetrics().bottom;
//float w = paintText.measureText("\uF00D");
//canvas.drawText("\uf00d", currentx - w / 2, currenty + 3 * bottom, paintText);
} /**
* 初试化画笔
*/
private void initPaint() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.rgb(18, 26, 34)); paint1 = new Paint();
paint1.setAntiAlias(true);
paint1.setColor(Color.WHITE); paintLine = new Paint();
paintLine.setColor(Color.WHITE);
paintLine.setStrokeWidth(5);
paintLine.setAntiAlias(true); paintText = new Paint();
paintText = new Paint();
paintText.setColor(Color.WHITE);
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/FontAwesome.ttf");
paintText.setTextSize(50);
paintText.setTypeface(tf); } /**
* 设置滑动的效果
*
* @param x 点击的x坐标轴
* @param y 点击的y坐标轴
* @param event 控件的事件
*/
private void viewMove(float x, float y, MotionEvent event) {
if (x <= r) {
currentx = r;
} else if (x >= getWidth() - r) {
currentx = getWidth() - r;
} else if (y <= r) {
currenty = r;
} else if (y >= getHeight() - r) {
currenty = getHeight() - r;
} else {
currentx = event.getX();
currenty = event.getY();
}
} /**
* 判断是不是在控件可操作的范围之内
*
* @param x 点击的x坐标轴
* @param y 点击的y坐标轴
*/
private boolean isOption(float x, float y) {
if (x > currentx - r && x < currentx + r && y < currenty + r & y > currenty - r)
return true;
else
return false;
} /**
* 回调
*/
public interface ViewCallBack {
public void finishActivity(Context context);
} public Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片 www.2cto.com
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
return newbm;
} /**
* 转换图片成圆形
*
* @param bitmap 传入Bitmap对象
* @return
*/
public Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
} Bitmap output = Bitmap.createBitmap(width,
height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output); final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst_left, dst_top, dst_right, dst_bottom); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
}
}

调用:

 import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout relativeLayout = new RelativeLayout(this);//(RelativeLayout) findViewById(R.id.rl);
DraggableView draggableView = new DraggableView(this,new DraggableView.ViewCallBack(){ @Override
public void finishActivity(Context context) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.no_anim);
}
});
relativeLayout.addView(draggableView); setContentView(relativeLayout);
}
}

Android画一个随意拖动的圆形的更多相关文章

  1. 张高兴的 UWP 开发笔记:用 Thumb 控件仿制一个可拖动 Button

    在 WPF 上可用的控件拖动方法在 UWP 上大多没用,那干脆用 Thumb 仿制一个吧. 关于 Thumb 控件的教程也不多,毕竟在 WPF 控件拖动有很多种方法, Thumb 就显得很鸡肋了.下面 ...

  2. [uwp]自定义Behavior之随意拖动

    由于最近有需求,所以自定义了一个随意拖动元素的Behavior. 当然在使用这个自定义的Behavior时,有个小假设:拖动元素必须是Canvas容器的子元素. 实现原理比较简单低效: 监听被拖动元素 ...

  3. 手把手带你画一个动态错误提示 Android自定义view

    嗯..再差1篇就可以获得持之以恒徽章了,今天带大家画一个比较简单的view. 转载请注明出处:http://blog.csdn.net/wingichoy/article/details/504771 ...

  4. 手把手带你画一个漂亮蜂窝view Android自定义view

    上一篇做了一个水波纹view  不知道大家有没有动手试试呢点击打开链接 这个效果做起来好像没什么意义,如果不加监听回调 图片就能直接替代.写这篇博客的目的是锻炼一下思维能力,以更好的面多各种自定义vi ...

  5. CentOS6下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具)

    1.先添加几个RPM下载源 1.1)安装RPMforge的CentOS6源     [root@AY130611215205Z ~]# wget -c http://pkgs.repoforge.or ...

  6. [Android] 给图像加入相框、圆形圆角显示图片、图像合成知识

        前一篇文章讲述了Android触屏setOnTouchListener实现突破缩放.移动.绘制和加入水印,继续我的"随手拍"项目完毕给图片加入相框.圆形圆角显示图片和图像合 ...

  7. 用 D3.js 画一个手机专利关系图, 看看苹果,三星,微软间的专利纠葛

    前言 本文灵感来源于Mike Bostock 的一个 demo 页面 原 demo 基于 D3.js v3 开发, 笔者将其使用 D3.js v5 进行重写, 并改为使用 ES6 语法. 源码: gi ...

  8. 用PS画一个齿轮

    以前只会画圆画方,这没技术含量.今天学了一个稍难一点的,画一个齿轮.图形有圆也有方.以下描述如何画出来的. 一.打开PS准备一画布,画一矩形并且填充颜色. 二.编辑->自由变换(CTRL+T), ...

  9. 深夜,用canvas画一个时钟

    深夜,用canvas画一个时钟 查看demo 这几天准备阿里巴巴的笔试,可以说已经是心力交瘁,自从阿里和蘑菇街的内推被刷掉之后,开始越来越怀疑起自己的能力来,虽然这点打击应该是微不足道的.毕竟校招在刚 ...

随机推荐

  1. android 隔几秒再执行

    今天做项目,需要前面的方法执行完等待2秒在关闭当前页面.之前使用的是Thread.sleep(2000)发现根本没有作用.经过多次尝试,发现需要使用以下方法才能实现: new Thread(){ pu ...

  2. NPOI导入导出Excel

    .net mvc利用NPOI导入导出excel 注意:如何导出的提交方式ajax导出是失效的! 解决方案是:js处理l两个表单的提交  代码:  第一步. 在页面里面加入2个隐藏的iframe, 如下 ...

  3. POJ1671 动态规划

    POJ1671 问题重述: 本题求解一首N行诗可能的押韵结构的数目.所谓押韵结构,指的是指定的行数之间必须押韵.例如一首3行诗的押韵结构可以是aaa, aab, aba, baa, abc 5种(aa ...

  4. php 简单分页

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. DAO 基础学习笔记

    一.DAO 1.概念: (1)Date Access Object(数据存取对象) (2)位于业务逻辑和持久化数据之间 (3)实现对持久化数据的访问 (4)类---> DAO --->数据 ...

  6. Form表单插件jquery.form.js

    常常使用到这个插件,但是老忘记怎么使用,现在对大家写的进行一定的整合. 使用插件实例: 一般的使用方法 <!-- 引入jquery文件 --> <script src="h ...

  7. nRF51822 SDK初体验

    作为两家BLE芯片大厂之一,nordic不像TI那么开放,nordic的开发资料是很难找的. 今天有幸得到nordic的BLE芯片nRF51822的SDK,看了一下.   首先,nordic号称协议栈 ...

  8. Friendly number

    Friendly number Long numbers can be made to look nicer, so let’s write some code to do just that. Yo ...

  9. 转:SVN 出现This client is too old to work with working copy...错误

    本地进行SVN客户端版本更新,但是之前一些代码是用的旧svn客户端提交的,这时候进行代码更新或者提交代码可能会出现错误,我这边是NetBeans中提交代码就出现了以下错误:This client is ...

  10. mysqli connect database and print

    <?php $connect = mysqli_connect('localhost','root','','intertrading') or die('Unale to connect'); ...