显示刀锋的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. codeblocks 中文编码问题

    参考文章: code::blocks 初使用遇到的问题记录 codeblocks 中文编码问题 string var="汉"; cout<<var<<end ...

  2. TYVJ3680 找妹子

    时间: 1000ms / 空间: 1200KiB / Java类名: Main 背景 本题由 @fjzzq2002 提供,已奖励20金币. 描述 sps是zzq的好伙伴. sps一天叫来了许多个妹子. ...

  3. 【Vijos1250】最勇敢的机器人(并查集,分组背包DP)

    题意:有N个物品,承重上限为M,有K组物品互斥关系,互斥关系有传递性,即1与2互斥,2与3互斥,1与3也互斥 给出每个物品的花费和价值,求承重上限内的最大价值总和 n<=1000,m<=1 ...

  4. MysqL5.7在使用mysqldump命令备份数据库报错:mysqldump: [Warning] Using a password on the command line interface can be insecure.

    在阿里云服务器增加一个shell脚本定时备份数据库脚本执行任务时,测试性的执行了备份命令,如下 [root@iZ2ze503xw2q1fftv5rhboZ mysql_bak]# /usr/local ...

  5. 【shell】shell编程(四)-循环语句

    上篇我们学习了shell中条件选择语句的用法.接下来本篇就来学习循环语句.在shell中,循环是通过for, while, until命令来实现的.下面就分别来看看吧. for for循环有两种形式: ...

  6. 简单说明PHP的垃圾收集机制是怎样的?

    腾讯 对变量有个引用计数,计数到0时变量被销毁. ———————————————————————— 每一种语言都有自己的自动垃圾回收机制,让程序员不必过分关心程序内存分配,但是在OOP中,有些对象需要 ...

  7. springmvc中ajax处理

    1.使用HttpServletResponse处理--不需要配置解析器 @Controller public class AjaxController { @RequestMapping(" ...

  8. 2017 ACM/ICPC Asia Regional Qingdao Online 记录

    题目链接  Qingdao Problem C AC自动机还不会,暂时暴力水过. #include <bits/stdc++.h> using namespace std; #define ...

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

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

  10. NRapid前言

    开发工具 Visual Studio 2017 数据库 SQL Server 2012 相关技术 Asp.net MVC