在开发一个游戏时候,需要用到这样的效果。
首先定义
private Paint mPaint_weapon = null; // 真实的画笔
private Path mPath_weapon = null; //
// private Point fly_middle;
private ArrayList weapon_Track;
private int weapon_Track_w = 1; // 这里设置刀的宽度哦,想刀宽一点就把这个值调大一点,不要太大哦
private int weapon_color_r = 0xff, weapon_color_g = 0x00, weapon_color_b = 0x00; // 橙色#FF0000 紫色 800080 蓝色00BFFF 淡绿色7CFC00 淡 FFFF00 白色 FFFAFA 红色480000 配置不同的颜色就是不同的刀哦
首先在:
public boolean onTouchEvent(MotionEvent event) {
.....// 此处省略一些字符
case MotionEvent.ACTION_DOWN:
if(mPath_weapon != null)mPath_weapon.reset();
if(weapon_Track != null)weapon_Track.clear();
.....// 此处省略一些字符
case MotionEvent.ACTION_MOVE:
synchronized(weapon_Track){
if(weapon_Track.size() > 7){
weapon_Track.remove(0);
}
weapon_Track.add(new PointF(event.getX(), event.getY()));
}
.....// 此处省略一些字符
最后是核心代码:
private void Draw_warrior_weaponHitOut(Canvas canvas) {
if(DEBUG)Log.v("yuyongjun", "Draw_warrior_weaponHitOut)");
synchronized(weapon_Track)
{
if(weapon_Track.size()>1){
endX = weapon_Track.get(0).x;
endY = weapon_Track.get(0).y;
weapon_Track_w = 1;
mPaint_weapon.setARGB(0xff, weapon_color_r, weapon_color_g, weapon_color_b);
for(int i=0;i
startX = endX;
startY = endY;
controlX = weapon_Track.get(i).x;
controlY = weapon_Track.get(i).y;
endX = (controlX + weapon_Track.get(i+1).x)/2;
endY = (controlY + weapon_Track.get(i+1).y)/2;
mPath_weapon.moveTo(startX, startY);
mPath_weapon.quadTo(controlX, controlY, endX, endY);
//mPaint_weapon.setColor(mBladeColor);
mPaint_weapon.setStrokeWidth(weapon_Track_w++);// ᄏᆳ며■ᄊ
canvas.drawPath(mPath_weapon, mPaint_weapon);
mPath_weapon.reset();
}
startX = endX;
startY = endY;
endX = weapon_Track.get(weapon_Track.size()-1).x;
endY = weapon_Track.get(weapon_Track.size()-1).y;
mPath_weapon.moveTo(startX, startY);
mPath_weapon.lineTo(endX, endY);
mPaint_weapon.setStrokeWidth(weapon_Track_w++);
canvas.drawPath(mPath_weapon, mPaint_weapon);
if(weapon_Track.size() > 3)
{ //这里 去绘制刀的光泽,这里用白色去表示刀的光泽
endX = weapon_Track.get(3).x;
endY = weapon_Track.get(3).y;
mPaint_weapon.setARGB(0xc0, 0xff, 0xff, 0xff);
for(int i=2;i < weapon_Track.size()-1;i++){
startX = endX;
startY = endY;
controlX = weapon_Track.get(i).x;
controlY = weapon_Track.get(i).y;
endX = (controlX + weapon_Track.get(i+1).x)/2;
endY = (controlY + weapon_Track.get(i+1).y)/2;
mPath_weapon.moveTo(startX, startY);
mPath_weapon.quadTo(controlX, controlY, endX, endY);
//mPaint_weapon.setColor(mBladeColor);
mPaint_weapon.setStrokeWidth(i-1);// ᄏᆳ며■ᄊ
canvas.drawPath(mPath_weapon, mPaint_weapon);
mPath_weapon.reset();
}
}
// weapon_Track.remove(0);
}
}
}
至于如何判断击中一个目标 ,就很明显了,这里就不告诉大家了。
如果需要看看这样做出来的效果,可以去下载这个小游戏(苍蝇入侵):http://www.nduoa.com/apk/detail/645882