菜单的画图

package hq.example.drawPicture;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.Printer;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
ImageView image;
Canvas can;
Paint p;
OnTouchListener listen;
int idt;

//这样就可以防止菜单的id重复
final int menuId=11;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView)this.findViewById(R.id.picture);

WindowManager manager= (WindowManager)this.getSystemService(WINDOW_SERVICE);
Display play= manager.getDefaultDisplay();
Bitmap map=Bitmap.createBitmap(play.getWidth(),play.getHeight(), Config.ARGB_8888);
//设置背景颜色
image.setBackgroundColor(Color.WHITE);
can=new Canvas(map);
//设置图片的缓存,把图片存放到map中
image.setImageBitmap(map);
//设置的是画图纸(can)与图片的位置一致
can.setMatrix(image.getImageMatrix());
p=new Paint();

  listen=new MyListen();
  image.setOnTouchListener(listen);

}

class MyListen implements OnTouchListener{


float x1,x2,y1,y2;
@Override
public boolean onTouch(View v, MotionEvent event) {
int action= event.getAction();


switch(action){
case MotionEvent.ACTION_DOWN:
//得到按下的手指
x1=event.getX();
y1=event.getY();

break;
case MotionEvent.ACTION_MOVE:
//画直线
if(idt==R.id.line2){
x2=event.getX();
y2=event.getY();
can.drawLine(x1, y1, x2, y2, p);
v.postInvalidate();
x1=x2;
y1=y2;
image.setBackgroundColor(Color.RED);
//can.drawColor(Color.YELLOW);
p.setColor(Color.BLACK);
}

break;
case MotionEvent.ACTION_UP:
x2=event.getX();
y2=event.getY();
Toast.makeText(MainActivity.this, "lineTouchrrr", Toast.LENGTH_LONG).show();

//画圆
if(idt==R.id.cir2){
image.setBackgroundColor(Color.BLUE);
//can.drawColor(Color.YELLOW);
p.setColor(Color.YELLOW);
p.setStyle(Style.STROKE);
can.drawCircle(x1, y1,y2/2 , p);
v.postInvalidate();
}
//画弧形
//canvas.drawRect(new Rect(15, 15, 140, 70), mPaint);

if(idt==R.id.rectangle2){
image.setBackgroundColor(Color.GREEN);
//can.drawText("黄琴", x1, y1, p);
//can.drawColor(Color.YELLOW);
p.setColor(Color.WHITE);
//RectF 就是一个矩形 的框
RectF rectf=new RectF(x1,y1,x2,y2);
//画的圆
//can.drawOval(rectf, p);
//矩形
//can.drawRect(rectf, p);
//只画边框,由画笔决定的
p.setStyle(Style.STROKE);
//设置笔的粗细,3表示的是什么类型,f表示的是float
p.setStrokeWidth(3f);
//rectf 参考的矩形,画的圆弧是在以参考的矩形,true是否圆弧经过圆心(以圆心画圆),0表示的是画弧的起始位置,90表示的是顺时针跨过多少度
//不使用圆心,填充的面积大于使用圆心的区域
can.drawArc(rectf, 0, 90, true, p);
v.postInvalidate();
}
//画矩形
if(idt==R.id.ju){
//RectF相当于一个框
RectF r=new RectF(x1,y1,x2,y2);
can.drawRect(r, p);
v.postInvalidate();

}
//设置字体的颜色
if(idt==12){
//设置红色
p.setColor(Color.RED);
//设置粗细
p.setStrokeWidth(3f);
can.drawText("黄琴", x1, y1, p);
v.postInvalidate();
}
//设置黄色
if(idt==13){
p.setColor(Color.GREEN);
can.drawText("我要得到我想好的", x2, y2, p);
v.postInvalidate();
}
//设置蓝色
if(idt==14){
p.setColor(Color.BLUE);
can.drawText("you must come on", x1, y2, p);
//iamge的背景颜色的设置,可以覆盖,之前在can的纸上的数据,可以得到新的数据
image.setBackgroundColor(Color.BLACK);
v.postInvalidate();

}
break;

}
return true;
}

}
//菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//在方法中创建菜单
SubMenu sub=menu.addSubMenu(100, menuId, 2, "画笔颜色");
sub.setIcon(R.drawable.pain);
sub.add(100,12,1,"红色");
sub.add(100,13,2,"黄色");
sub.add(100,14,3,"蓝色");
// ////创建子菜单<Menu>
// SubMenu sub=menu.addSubMenu(1,100,1,"我有子菜单");
// sub.add(101,102,2, "我是子菜单");
return true;
}
//菜单点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {

idt=item.getItemId();
switch (idt) {
case R.id.line2:
Toast.makeText(MainActivity.this, "我是直线", Toast.LENGTH_SHORT).show();
image.setOnTouchListener(listen);
//画直线
break;

case R.id.cir2:
Toast.makeText(MainActivity.this, "我是圆", Toast.LENGTH_SHORT).show();
image.setOnTouchListener(listen);
break;
case R.id.rectangle2:
Toast.makeText(MainActivity.this, "我是矩形", Toast.LENGTH_SHORT).show();
image.setOnTouchListener(listen);
break;
//设置画笔的颜色:
case menuId:
image.setOnTouchListener(listen);

}
return super.onOptionsItemSelected(item);
}

}











猜你喜欢

转载自1440434270.iteye.com/blog/2069947