显示刀锋的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. 【树状数组区间修改单点查询】HDU 4031 Attack

    http://acm.hdu.edu.cn/showproblem.php?pid=4031 [题意] 有一个长为n的长城,进行q次操作,d为防护罩的冷却时间,Attack表示区间a-b的墙将在1秒后 ...

  2. 大数(bzoj 4542)

    /* 想了半天莫队,不知道咋转移,需要动下脑子. 有一个很显然的结论是如果(l,r)是P的倍数,那么s[l...n]%P=s[r+1...n]%P. 根据这个东西,我们预处理出所有的后缀%P的余数,接 ...

  3. nginx配置 location root alias

    语法规则: location [=|~|~*|^~] /uri/ { … } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url做编码,因 ...

  4. 解决三星 BIOS 模式没有 Fast Bios Mode选项 U盘动项问题

    今天想分下盘,进入biso竟然没有网上说 Fast Bios Mode选项,最后自己误打误撞竟然设置成功了,太不可思议了官方这么说:开机按F2进入BIOS设置,找到Boot选项下Boot Device ...

  5. 带你学Node系列之express-CRUD

    前言 hello,小伙伴们,我是你们的pubdreamcc,本篇博文出至于我的GitHub仓库node学习教程资料,欢迎小伙伴们点赞和star,你们的点赞是我持续更新的动力. GitHub仓库地址:n ...

  6. Executors

    提供了工厂方法: Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, Thread ...

  7. 抽球游戏(fwt)

    地址:https://nanti.jisuanke.com/t/26017 分析: 现在是给定p,求是否存在这样的数列c,我们可以让p进行fwt变换,然后把点值都三次方根,然后再把得到的点值ufwt成 ...

  8. Java对象的死亡

    在堆里面存放着Java世界中几乎所有的对象实例,垃圾收集器在对堆进行回收前,第一件事情就是要确定这些对象之中哪些还“存活”着,哪些已经“死去”(即不可能再被任何途径使用的对象). 一,引用计数算法 给 ...

  9. 用systemtap跟踪打印动态链接库的所有c++函数调用过程

    http://gmd20.blog.163.com/blog/static/168439232015475525227/             用systemtap跟踪打印动态链接库的所有c++函数 ...

  10. ios下读取jason中的nsstring时间并本地化成中文gmt时间显示上午下午

    https://developer.apple.com/library/ios/qa/qa1480/_index.html - (NSDate *)dateFromString:(NSString * ...