在一个重写的View,主要绘制在View的OnDraw()方法中,
首先:先绘制出几个点,当然是任意多
最后一个点和第一个点闭合在一起之后,画点结束
然后可以随意拖动达到裁减的效果
当然裁剪的功能没有实现
代码如下
View--------:
--**************************************************************
public class MyView extends View{
public int doWhat = 0;
ArrayList<HashMap<String,Float>> list = new ArrayList<HashMap<String,Float>>();
public MyView(Context context) {
super(context);
setBackgroundResource(R.drawable.youxibiajii);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
for (int i = 0; i < list.size(); i++) {
canvas.drawCircle(list.get(i).get("x"), list.get(i).get("y")
, 10, paint);
if(i>0){
canvas.drawLine(list.get(i-1).get("x"), list.get(i-1).get("y"),
list.get(i).get("x"), list.get(i).get("y"), paint);
}
}
}
*******************************************************************************************
Activity-------******************************************************************
public class MainActivity extends Activity {
public static float ScreenW,ScreenH;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics dis = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dis);
ScreenW = dis.widthPixels;
ScreenH = dis.heightPixels;
final MyView view = new MyView(this);
setContentView(view);
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent event) {
float startX = event.getRawX();
float startY = event.getRawY();
if(view.doWhat == 0){//画点
switch(event.getAction()){
case KeyEvent.ACTION_DOWN:
HashMap<String,Float> map = new HashMap<String, Float>();
if(view.list.size()>2&&
(startX>view.list.get(0).get("x")-20&&startY>view.list.get(0).get("y")-20)
&&(startX<view.list.get(0).get("x")+20&&startY<view.list.get(0).get("y")+20)){
map.put("x", view.list.get(0).get("x"));
map.put("y", view.list.get(0).get("y"));
view.list.add(map);
view.doWhat = 1;
break;
}
map.put("x", startX);
map.put("y", startY);
view.list.add(map);
break;
}
}
else{//拖动
for (int i = 0; i < view.list.size(); i++) {
if((startX>view.list.get(i).get("x")-15&&startY>view.list.get(i).get("y")-15)
&&(startX<view.list.get(i).get("x")+15&&startY<view.list.get(i).get("y")+15)){
if(i==0){
view.list.get(i).put("x", startX);
view.list.get(i).put("y", startY);
view.list.get(view.list.size()-1).put("x", startX);
view.list.get(view.list.size()-1).put("y", startY);
}
else{
view.list.get(i).put("x", startX);
view.list.get(i).put("y", startY);
}
break;
}
}
}
view.invalidate();
return true;
}
});
}