DevStore首页 > DEV资源 > 源码下载 > 源码详情

ScrollView截图涂鸦

 
fzly512 | 2015-11-05 17:06    浏览量(2208)    评论(0)    收藏(11)
功能分类:工具 支持平台:Android 运行环境:Eclipse
开发语言:Java 开发工具:Eclipse 源码大小:3.00MB
下载源码 211 人下载

源码简介

截取ScrollView长图,并在截取的图片上进行涂鸦和保存,下一步则是通过Acache传递涂鸦后的图片进行展示。(代码中没有提供截取ScrollView的方法,只提供了一张长图)主要是通过在imageView外包裹一层ScrollView,通过点击按钮切换判断手势,是传给ScrollView还是ImageView,并且添加了切换画笔颜色、清屏、画笔粗细的功能

源码截图

  • 案例中的长图
  • 截图动画,截取得不大好
DevStore所有源码来自用户上传分享,版权问题及牵扯到商业纠纷均与DevStore无关。

源码片段

public class EditImage extends Activity implements OnClickListener{ 

	private ImageView imageView;
	private Canvas canvas;
	private Paint paint; 
	private Bitmap bm2;
	private Bitmap bm;
	private ImageView imageView2;
	public static int isImage = 0;
	private Button btn;
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.edit_image); 

		initView();
		//加载图片
		bm = BitmapFactory.decodeResource(getResources(), R.drawable.bbbb);
		//用于涂鸦的bitmap
		bm2 = Bitmap.createBitmap(bm).copy(Config.ARGB_8888, true);

		canvas = new Canvas(bm2);
		paint = new Paint();
		paint.setColor(Color.RED);
		paint.setStrokeWidth(6.0f);

		canvas.drawBitmap(bm2,new Matrix(),paint);

		imageView.setImageBitmap(bm);
		imageView2.setImageBitmap(bm2);
		imageView2.setOnTouchListener(new TouchListener());
	}
	/**
	 * 生成按钮、图片组件
	 */
	private void initView() {
		//切换滚动涂鸦按钮
		btn = (Button) findViewById(R.id.btn_touch);
		btn.setOnClickListener(this);
		//设置笔触大小
		Button paintSize = (Button) findViewById(R.id.paint_size);
		paintSize.setOnClickListener(this);
		//下一步
		Button next = (Button) findViewById(R.id.next_selectMan);
		next.setOnClickListener(this);
		Button Cancel = (Button) findViewById(R.id.edit_image_cancel);
		Cancel.setOnClickListener(this);
		imageView = (ImageView) this.findViewById(R.id.imageView); 
		imageView2 = (ImageView) this.findViewById(R.id.imageView2);
	} 
	/**
	 * 触摸事件 绘图
	 * @author Administrator
	 *
	 */
	private final class TouchListener implements OnTouchListener { 
		/**按下时记录的x、y坐标**/
		int startX;
		int startY;
		/** 记录是拖拉照片模式还是放大缩小照片模式 */
		public  int mode = 0;// 初始状态   
		/** 批注照片模式 */
		public static final int MODE_DRAW = 1; 
		/** 放大缩小照片模式 */
		public static final int MODE_ZOOM = 2; 
		/** 用于记录开始时候的坐标位置 */
		private PointF startPoint = new PointF(); 

		@SuppressLint("NewApi")
		@Override 
		public boolean onTouch(View v, MotionEvent event) { 
			switch (event.getAction() & MotionEvent.ACTION_MASK) { 
			// 手指压下屏幕 
			case MotionEvent.ACTION_DOWN: 
				startX = (int) event.getX();
				startY = (int) event.getY();
				mode = MODE_DRAW; 
				// 记录ImageView当前的移动位置 
				startPoint.set(event.getX(), event.getY()); 
				break; 
				// 手指在屏幕上移动,改事件会被不断触发 
			case MotionEvent.ACTION_MOVE: 
				// 拖拉图片 
				if (mode == MODE_DRAW) { 
					// 获取手移动后的坐标
					int stopX = (int) event.getX();
					int stopY = (int) event.getY();
					// 在开始和结束坐标间画一条线
					canvas.drawLine(startX, startY, stopX, stopY, paint);
					// 实时更新开始坐标
					startX = (int) event.getX();
					startY = (int) event.getY();
					if(isClear == false)
						imageView2.setImageBitmap(bm2);
					else
						imageView2.setImageBitmap(bm3);

				} 
				break; 
				// 手指离开屏幕 
			case MotionEvent.ACTION_UP: 
				// 当触点离开屏幕,但是屏幕上还有触点(手指) 
			case MotionEvent.ACTION_POINTER_UP: 
				mode = 0; 
				break; 
				// 当屏幕上已经有触点(手指),再有一个触点压下屏幕 
			case MotionEvent.ACTION_POINTER_DOWN: 
				mode = MODE_ZOOM; 
				break; 
			}
			return true; 
		}
	}
	/**
	 * 点击事件
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_touch:
			if(isImage == 0){
				//进行滚动
				isImage =1;
				btn.setText("注释");
			}else{
				btn.setText("滑动");
				isImage =0;
			}
			break;
		case R.id.paint_size:
			openOptionsMenu();
			break;
		case R.id.next_selectMan:
			//下一步,保存图片,并把图片位置传过去
			ACache ac = ACache.get(this);
			String imageName = null;
			if(isClear){
				ac.put("image", bm3);
			}else
				ac.put("image", bm2);
			Intent i = new Intent();
			i.setClass(this, ShowUpdateImage.class);
			startActivity(i);
			finish();
			break;
		case R.id.edit_image_cancel:
			finish();
			if(isClear)
				isClear = false;
			break;
		default:
			break;
		}

	} 
	/**
	 * 

Title: onDestroy

*

Description: 保存图片

* @see android.app.Activity#onDestroy() */ public static String savePic(Bitmap b) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US); File outfile = new File("/sdcard/image"); // 如果文件不存在,则创建一个新文件 if (!outfile.isDirectory()) { try { outfile.mkdir(); } catch (Exception e) { e.printStackTrace(); } } String fname = outfile + "/" + sdf.format(new Date()) + ".jpg"; FileOutputStream fos = null; try { fos = new FileOutputStream(fname); if (null != fos) { b.compress(Bitmap.CompressFormat.JPEG, 30, fos); fos.flush(); fos.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return fname; } /** * 工具栏 */ @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 1, 1, "清屏"); menu.add(0, 2, 2, "颜色"); menu.add(0, 3, 3, "画笔"); return super.onCreateOptionsMenu(menu); } /**画笔粗细**/ private int whichStrokeWidth = 1; /**当前选择的颜色选项***********/ private int whichColor = 0; private Bitmap bm3; private boolean isClear = false; @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == 1){ //清屏 bm3 = Bitmap.createBitmap(bm).copy(Config.ARGB_8888, true); canvas = new Canvas(bm3); canvas.drawBitmap(bm3, new Matrix(), paint); imageView2.invalidate(); imageView2.setImageBitmap(bm3); isClear = true; }else if(item.getItemId() == 2){ Dialog mDialog = new AlertDialog.Builder(EditImage.this) .setTitle("颜色设置") .setSingleChoiceItems(new String[]{"红色","绿色","蓝色"}, whichColor, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { switch(which){ case 0: //画笔的颜色 paint.setColor(Color.RED); whichColor = 0; break; case 1: //画笔的颜色 paint.setColor(Color.GREEN); whichColor = 1; break; case 2: //画笔的颜色 paint.setColor(Color.BLUE); whichColor = 2; break; } } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .create(); mDialog.show(); }else if(item.getItemId() == 3){ Dialog mDialog = new AlertDialog.Builder(EditImage.this) .setTitle("画笔设置") .setSingleChoiceItems(new String[]{"细","中","粗"}, whichStrokeWidth, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch(which){ case 0: //画笔的粗细 paint.setStrokeWidth( 3.0f); whichStrokeWidth = 0; break; case 1: //画笔的粗细 paint.setStrokeWidth( 6.0f); whichStrokeWidth = 1; break; case 2: //画笔的粗细 paint.setStrokeWidth( 9.0f); whichStrokeWidth = 2; break; } } }) .setPositiveButton("确定", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .create(); mDialog.show(); } return super.onOptionsItemSelected(item); } }
  •   赞(0) 赞 +1 赞(0) 已赞
  •   收藏(11) 收藏 +1 已收藏 取消
  •   下载(211)
评论(0)
  • 推荐
  • 审核
  • 收录
  • 获取活力值

源码上传作者

  • 6 能力值
  • 2 源码
  • 0 评测
  • 1 资料

热门源码