显示刀锋的View

package com.wbhuang.myninjia;

import java.util.ArrayList;
import java.util.List; import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager; /**
* @author wbhuang
* 20130821
*/
public class ShadeView extends View {
static final int MAX_POINTS_NUM = 10;
static final float CENTER_RATIO = 0.5f;
static final float SHAPE_RATIO = 0.02f; //点类
static class Vertex {
public float x;
public float y;
public Vertex() {}
public Vertex(float x, float y) {
this.x = x;
this.y = y;
}
} List<Vertex> leftVertexs = new ArrayList<Vertex>(MAX_POINTS_NUM); //刀锋上边线点集
List<Vertex> rightVertexs = new ArrayList<Vertex>(MAX_POINTS_NUM); //刀锋下边线点集
List<Vertex> sampleVertexs = new ArrayList<Vertex>(MAX_POINTS_NUM); //刀锋路径的点集 float shape_width = 10;
private Paint paint;
private static final int gestureColor = Color.rgb(153, 153, 153);
private static final int alpha = 220;
private static final int alpha_full = 255;
private static long last_time;
private static final int MIN_TIME_INTERVAL = 10; public ShadeView(Context context) {
super(context);
paint = new Paint();
paint.setStyle(Paint.Style.FILL); DisplayMetrics metric = new DisplayMetrics();
((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels;
shape_width = width * SHAPE_RATIO;//刀锋长度
} //计算路径上面和下面的点集
private void calcVertexs() {
int len = sampleVertexs.size();
leftVertexs.clear();
rightVertexs.clear();
for (int i = 1; i < len; i++) {
Vertex cur = sampleVertexs.get(i);
Vertex pre = sampleVertexs.get(i-1); Vertex center = new Vertex();
center.x = cur.x - (cur.x - pre.x) * CENTER_RATIO;
center.y = cur.y - (cur.y - pre.y) * CENTER_RATIO; float w = cur.x - pre.x;
float h = cur.y - pre.y;
double angle = Math.atan(h/w); Vertex leftVertex = new Vertex();
Vertex rightVertex = new Vertex(); float centerShapeWidth = shape_width * i / len; if (i == (len - 2))
centerShapeWidth = shape_width;
if (cur.x > pre.x) {
leftVertex.x = (float) (center.x + Math.sin(angle) * centerShapeWidth);
leftVertex.y = (float) (center.y - Math.cos(angle) * centerShapeWidth);
rightVertex.x = (float) (center.x - Math.sin(angle) * centerShapeWidth);
rightVertex.y = (float) (center.y + Math.cos(angle) * centerShapeWidth);
} else {
rightVertex.x = (float) (center.x + Math.sin(angle) * centerShapeWidth);
rightVertex.y = (float) (center.y - Math.cos(angle) * centerShapeWidth);
leftVertex.x = (float) (center.x - Math.sin(angle) * centerShapeWidth);
leftVertex.y = (float) (center.y + Math.cos(angle) * centerShapeWidth);
}
leftVertexs.add(leftVertex);
rightVertexs.add(rightVertex);
}
} @Override
public void onDraw(Canvas canvas) {
Path path = new Path();
if (sampleVertexs.size() > 1) {
calcVertexs();
Vertex begin = sampleVertexs.get(0);
Vertex end = sampleVertexs.get(sampleVertexs.size()-1); path.moveTo(begin.x, begin.y);
for (int i= 0; i < leftVertexs.size(); i++) {
Vertex vertex = leftVertexs.get(i);
path.lineTo(vertex.x, vertex.y);
}
path.lineTo(end.x, end.y);
for (int i = rightVertexs.size() - 1; i > 0; i--) {
Vertex vertex = rightVertexs.get(i);
path.lineTo(vertex.x, vertex.y);
} paint.setColor(Color.WHITE);
paint.setAlpha(alpha_full);
canvas.drawPath(path, paint);
}
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(android.view.MotionEvent event) {
if(event.getPointerCount() == 1){
int action = event.getAction();
if (MotionEvent.ACTION_DOWN == action) {
leftVertexs.clear();
rightVertexs.clear();
sampleVertexs.clear();
} else if (MotionEvent.ACTION_MOVE == action) {
//if (event.getEventTime() - last_time > MIN_TIME_INTERVAL) {
last_time = event.getEventTime();
if (sampleVertexs.size() > MAX_POINTS_NUM) {
sampleVertexs.remove(0);
}
Vertex vertex = new Vertex(event.getX(), event.getY());
sampleVertexs.add(vertex);
//}
} else if (MotionEvent.ACTION_UP == action) {
leftVertexs.clear();
rightVertexs.clear();
sampleVertexs.clear();
}
this.invalidate();
}
return true;
}
}
package com.wbhuang.myninjia;

import android.app.Activity;
import android.os.Bundle; public class NinjiaActivity extends Activity {
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ShadeView(this));
} }

Android上的水果忍者刀锋效果(JAVA实现)的更多相关文章

  1. Cocos2d-x 水果忍者划痕效果

    网上找的一个关于水果忍者划痕的,效果还算凑合.其原理就是基于OpenGL绘制直线,因为版本号过老,此处笔者改动了一些方法,粘贴后可直接使用 适用于Cocos2d-x 2.2.1 .h文件里须要添�的代 ...

  2. JavaScript实现的水果忍者游戏,支持鼠标操作

    智能手机刚刚普及时,水果忍者这款小游戏可谓风靡一时.几年过去了,现在,让我们用纯JavaScript来实现这个水果忍者游戏,就算是为了锤炼我们的JavaScript开发技能吧. 大家可以通过这个链接在 ...

  3. Android上dip、dp、px、sp等单位说明(转)

    dip  device independent pixels(设备独立像素). 不同设备不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖像素. 在 ...

  4. Android上dip、dp、px、sp等单位说明

    Android上dip.dp.px.sp等单位说明 dip  device independent pixels(设备独立像素). 不同设备不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA ...

  5. 前端优秀作品展示,JavaScript 版水果忍者

    <水果忍者>是一款非常受喜欢的手机游戏,刚看到新闻说<水果忍者>四周年新版要上线了.网页版的切水果游戏由百度 JS 小组开发,采用 vml + svg 绘图,使用了 Rapha ...

  6. [转]收集android上开源的酷炫的交互动画和视觉效果:Interactive-animation

    原文链接:http://www.open-open.com/lib/view/open1411443332703.html 描述:收集android上开源的酷炫的交互动画和视觉效果. 1.交互篇 2. ...

  7. 最牛逼android上的图表库MpChart(三) 条形图

    最牛逼android上的图表库MpChart三 条形图 BarChart条形图介绍 BarChart条形图实例 BarChart效果 最牛逼android上的图表库MpChart(三) 条形图 最近工 ...

  8. 最牛逼android上的图表库MpChart(二) 折线图

    最牛逼android上的图表库MpChart二 折线图 MpChart折线图介绍 MpChart折线图实例 MpChart效果 最牛逼android上的图表库MpChart(二) 折线图 最近工作中, ...

  9. 最牛逼android上的图表库MpChart(一) 介绍篇

    最牛逼android上的图表库MpChart一 介绍篇 MpChart优点 MpChart是什么 MpChart支持哪些图表 MpChart效果如何 最牛逼android上的图表库MpChart(一) ...

随机推荐

  1. 实验三 kali下metasploit的漏洞攻击实践

    一.实验内容 1.使用kali进行靶机的漏洞扫描,利用metasploit选择其中的一个漏洞进行攻击,并获取权限. 2.分析攻击的原理以及获取了什么样的权限. 二.实验要求 1.熟悉kali原理和使用 ...

  2. 淘金(bzoj 3131)

    Description 小Z在玩一个叫做<淘金者>的游戏.游戏的世界是一个二维坐标.X轴.Y轴坐标范围均为1..N.初始的时候,所有的整数坐标点上均有一块金子,共N*N块.    一阵风吹 ...

  3. android开发里跳过的坑——camera调用setDisplayOrientation设置预览显示旋转无效

    问题原因,在surfaceview没有设置给camera之前调用了,所以,这个方法一定要在camera.setPreviewDisplay(surfaceHolder)这个之后,启动相机预览之前调用.

  4. 装B技能GET起来!Apple Pay你会用了吗?

    科技圈儿有一个自带光环的品牌 它每次一有任何动静 不用宣传 也不用刻意营销 消息还是能传天下 2月18日 你敢说你的朋友圈儿没有被下面这个词儿刷屏? Apple Pay 这不,我就跟着凑凑热闹,开个小 ...

  5. 转:CEF嵌入到单文档mfc

    1.下载: http://www.magpcss.net/cef_downloads/下载cef binary 1.1364.1123 windows.zip(可能要FQ,百度goagent教程,最好 ...

  6. 开源天气预报api整理

    高德天气:https://lbs.amap.com/api/webservice/guide/api/weatherinfo/? github上对开源api的整理:https://github.com ...

  7. bootstrap -- col-sm-6 和 col-xs-6

  8. svn 创建分支、切换分支 及 合并分支 操作

    关联远程仓库: 右键  ---  点击 ' SVN Checkout...' 生成 打开trunk目录,在trunk目录下新建两个文本文件A.java,B.java: 打开A.java输入以下内容: ...

  9. CA与数字证书的自结

    1.CA CA(Certificate Authority)是数字证书认证中心的简称,是指发放数字证书.管理数字证书.废除数字证书的权威机构. 2.数字证书 如果向CA申请数字证书的单位为A.则他申请 ...

  10. Dell服务器相关操作

    1. Raid相关: # 删除已有的Raid配置 MegaCli64 -CfgLdDel -LALL -aALL # 获取设备ID和槽号 MegaCli64 -PDList -aALL|egrep ' ...