package com.game.android.ninjia;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
public class GestureView extends View {
private Paint paint;
private float startX = Float.NaN;
private float startY = Float.NaN;
private float endX = Float.NaN;
private float endY = Float.NaN;
//下层view
//private View viewer;
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 final int shape_length = 40;
//刀锋截短时间
private static final int shape_cut_time = 150;
public static final float getGap(float x0, float y0, float x1, float y1) {
return (float) Math.pow(Math.pow((x0 - x1), 2) + Math.pow((y0 - y1), 2), 0.5);
}
public GestureView(Context context) {
super(context);
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!Float.isNaN(startX) && !Float.isNaN(endY)) {
float gap = getGap(startX, startY, endX, endY);
float w = gap / 10;
//背景shape外侧点高度
float h = w > 7 ? 7 : w;
//填充shape外侧点高度
float h2 = h * 2 / 3;
double length = Math.pow(Math.pow(w, 2) + Math.pow((h), 2), 0.5);
double length2 = Math.pow(Math.pow(w, 2) + Math.pow((h2), 2), 0.5);
double ang1_1 = Math.atan((endY - startY) / (endX - startX));
double ang1_2 = Math.atan(h / w);
double angle1_1 = ang1_1 + ang1_2;
double angle1_2 = ang1_1 - ang1_2;
double ang2_2 = Math.atan(h2 / w);
double angle2_1 = ang1_1 + ang2_2;
double angle2_2 = ang1_1 - ang2_2;
if (endX > startX) {
float xx1 = endX - (float) (length * Math.cos(angle1_1));
float yy1 = endY - (float) (length * Math.sin(angle1_1));
float xx2 = endX - (float) (length * Math.cos(angle1_2));
float yy2 = endY - (float) (length * Math.sin(angle1_2));
float xx12 = endX - (float) (length2 * Math.cos(angle2_1));
float yy12 = endY - (float) (length2 * Math.sin(angle2_1));
float xx22 = endX - (float) (length2 * Math.cos(angle2_2));
float yy22 = endY - (float) (length2 * Math.sin(angle2_2));
Path backPath = new Path();
backPath.moveTo(startX, startY);
backPath.lineTo(xx1, yy1);
backPath.lineTo(endX, endY);
backPath.lineTo(xx2, yy2);
backPath.close();
Path fillPath = new Path();
fillPath.moveTo(startX, startY);
fillPath.lineTo(xx12, yy12);
fillPath.lineTo(endX, endY);
fillPath.lineTo(xx22, yy22);
fillPath.close();
paint.setColor(gestureColor);
paint.setAlpha(alpha);
canvas.drawPath(backPath, paint);
paint.setColor(Color.WHITE);
paint.setAlpha(alpha_full);
canvas.drawPath(fillPath, paint);
} else {
float xx1 = endX + (float) (length * Math.cos(angle1_1));
float yy1 = endY + (float) (length * Math.sin(angle1_1));
float xx2 = endX + (float) (length * Math.cos(angle1_2));
float yy2 = endY + (float) (length * Math.sin(angle1_2));
float xx12 = endX + (float) (length2 * Math.cos(angle2_1));
float yy12 = endY + (float) (length2 * Math.sin(angle2_1));
float xx22 = endX + (float) (length2 * Math.cos(angle2_2));
float yy22 = endY + (float) (length2 * Math.sin(angle2_2));
Path backPath = new Path();
backPath.moveTo(startX, startY);
backPath.lineTo(xx1, yy1);
backPath.lineTo(endX, endY);
backPath.lineTo(xx2, yy2);
backPath.close();
Path fillPath = new Path();
fillPath.moveTo(startX, startY);
fillPath.lineTo(xx12, yy12);
fillPath.lineTo(endX, endY);
fillPath.lineTo(xx22, yy22);
fillPath.close();
paint.setColor(gestureColor);
paint.setAlpha(alpha);
canvas.drawPath(backPath, paint);
paint.setColor(Color.WHITE);
paint.setAlpha(alpha_full);
canvas.drawPath(fillPath, paint);
}
}
}
@Override
public boolean onTouchEvent(android.view.MotionEvent event) {
if(event.getPointerCount() == 1){
int action = event.getAction();
if (MotionEvent.ACTION_DOWN == action) {
startX = event.getX();
startY = event.getY();
} else if (MotionEvent.ACTION_MOVE == action) {
endX = event.getX();
endY = event.getY();
//刀锋截短时间,则截短至一半
if ((event.getEventTime() - event.getDownTime()) > shape_cut_time) {
if(Math.abs(endX - startX) > shape_length && Math.abs(endY - startY) > shape_length){
startX = (float) (startX + (endX - startX) * 0.5);
startY = (float) (startY + (endY - startY) * 0.5);
}
}
invalidate();
} else if (MotionEvent.ACTION_UP == action) {
startX = Float.NaN;
startY = Float.NaN;
endX = Float.NaN;
endY = Float.NaN;
invalidate();
}
}
//该view消费了event,所以下层的view必须dispatchTouchEvent才能获得事件
//MotionEvent newEvent = MotionEvent.obtain(event);
//viewer.dispatchTouchEvent(newEvent);
return true;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
水果忍者刀锋在android上的实现
共27个文件
class:8个
png:6个
xml:3个
4星 · 超过85%的资源 需积分: 50 76 下载量 65 浏览量
2012-01-12
01:08:23
上传
评论 1
收藏 53KB RAR 举报
温馨提示
个人研究出来的 效果肯定没有游戏里面的好 但是我觉得让高手改进下应该没问题 注意只是水果忍者的刀锋!
资源推荐
资源详情
资源评论
收起资源包目录
Ninjia.rar (27个子文件)
Ninjia
bin
classes.dex 7KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
classes
com
game
android
ninjia
R$layout.class 409B
R$id.class 403B
R$drawable.class 422B
R$attr.class 355B
R.class 566B
GestureView.class 4KB
R$string.class 442B
NinjiaActivity.class 634B
resources.ap_ 10KB
Ninjia.apk 15KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 178B
drawable-mdpi
ic_launcher.png 3KB
layout
main.xml 624B
assets
gen
com
game
android
ninjia
R.java 751B
proguard.cfg 1KB
src
com
game
android
ninjia
NinjiaActivity.java 369B
GestureView.java 7KB
.project 842B
.classpath 364B
project.properties 361B
AndroidManifest.xml 743B
共 27 条
- 1
superjovie
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页