显示刀锋的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. 【leetcode dp】132. Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...

  2. 标准C程序设计七---31

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  3. Perl语言入门--2--变量与字符串

    一.$ 简单变量:$          以$开头+字母或下划线 区分大小写,变量长度不限,没有关键字,但不能和默认变量重复 1.整数的表示: 12 12e2 #1200 -12 #-12 1_200_ ...

  4. HDU 4920 Matrix multiplication(bitset优化)

    题目链接 Matrix multiplication 求矩阵A和B相乘的结果. 因为答案只要对3取模,所以我们可以通过一些方法来加速计算. 我们对两个矩阵各开两个bitset,分别存储模3余1和模3余 ...

  5. 51NOD 1424 零树

    Discription 有一棵以1为根的树,他有n个结点,用1到n编号.第i号点有一个值vi. 现在可以对树进行如下操作: 步骤1:在树中选一个连通块,这个连通块必须包含1这个结点. 步骤2:然后对这 ...

  6. ExtNet配置webconfig

    <?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访 ...

  7. SystemTap使用技巧 1 - 4 非常重要

    http://blog.csdn.net/wangzuxi/article/details/42849053

  8. Java屏幕截图工具 捕获屏幕

    原文:http://www.open-open.com/code/view/1420037709781 import java.awt.BorderLayout; import java.awt.Co ...

  9. 开源天气预报api整理

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

  10. Regularized least-squares classification(正则化最小二乘法分类器)取代SVM

    在机器学习或者是模式识别其中有一种重要的分类器叫做:SVM .这个被广泛的应用于各个领域.可是其计算的复杂度以及训练的速度是制约其在实时的计算机应用的主要原因.因此也非常非常多的算法被提出来.如SMO ...