- 浏览: 14613 次
- 性别:
- 来自: 广州
最近访客 更多访客>>
文章分类
最新评论
-
enjoylrx:
你好,可以发工程文件或者贴上string.xml可以吗我的em ...
图片缩放、平移、剪切
盯了两天代码才实现的功能我不得不把他写上博客,共享!主要是实现从SD卡文件夹中读取一个图片,然后实现缩小放大,平移、剪切功能!直接上代码.
package demo.com; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.Window; import android.view.WindowManager; import android.widget.EditText; import demo.utils.PictureView; public class PictureDispose extends Activity{ private PictureView pictureView = null; //图片路径 public static String photo_path = null; //旋转缩放的变量 float angle = 0.0f; float scale = 1.0f; //图片平移的xy坐标 int down_x = 0; int down_y = 0; int move_x = 0; int move_y = 0; int up_x = 0; int up_y = 0; //屏幕宽与高 public static int screen_width; public static int screen_height; //第一个menu菜单选项 protected static final int MENU_CUT = Menu.FIRST; //剪切标志 public static boolean cut = false; public static boolean cut_start = false; //剪切图片的名字 public static String cut_name = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //无title requestWindowFeature(Window.FEATURE_NO_TITLE); //全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getPhotoPath(); screenSize(); this.pictureView = new PictureView(this, photo_path, screen_width, screen_height); cut = false; cut_start = false; setContentView(pictureView); scale = pictureView.getScale(); } public void getPhotoPath(){ Intent intent = getIntent(); Bundle b = intent.getExtras(); String photoURL = b.getString("photoURL"); photo_path = photoURL; } //按照屏幕大小缩放图片 public void screenSize(){ DisplayMetrics displayMetric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetric); screen_height = displayMetric.heightPixels; screen_width = displayMetric.widthPixels; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(!cut_start){ //向左旋转 if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){ angle++; pictureView.setAngle(angle); } //向右旋转 if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){ angle--; pictureView.setAngle(angle); } //放大 if(keyCode == KeyEvent.KEYCODE_DPAD_UP){ if(scale < 2.9f) scale += 0.1f; else scale = 3.0f; System.out.println(scale); pictureView.setScale(scale); } //缩小 if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){ if(scale > 0.2f) scale -= 0.1f; else scale = 0.1f; System.out.println(scale); pictureView.setScale(scale); } //返回键 if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() ==0){ if(cut){ cut = false; cut_start = false; }else{ this.finish(); } return true; } } return super.onKeyDown(keyCode, event); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); //如果是剪切的话就执行下面代码 if(cut){ int change_x = (int)event.getX(); int change_y = (int)event.getY(); if((change_x > pictureView.getleft()-25)//如果触点选中了左边框 && change_x < pictureView.getleft()+25 && (change_y > pictureView.gettop()) && (change_y < pictureView.getbottom())){ pictureView.setLeft(change_x); }else if((change_x > pictureView.getright()-25)//如果触点选中了右边框 && (change_x < pictureView.getright()+25) && (change_y > pictureView.gettop()) && (change_y < pictureView.getbottom())){ pictureView.setRight(change_x); }else if((change_y > pictureView.gettop()-25)//如果触点选中了上边框 && (change_y < pictureView.gettop()+25) && (change_x > pictureView.getleft()) && (change_x < pictureView.getright())){ pictureView.setTop(change_y); }else if((change_y > pictureView.getbottom()-25)//如果触点选中了下边框 && (change_y < pictureView.getbottom()+25) && (change_x > pictureView.getleft()) && (change_x < pictureView.getright())){ pictureView.setBottom(change_y); }else if((change_x < 88) && (change_y > (screen_height-35))){ //显示输入对话框 setDialog(); //取消剪切框 cut = false; cut_start = false; }else if((change_x > (screen_width-88)) && (change_y > (screen_height-35))){ //取消剪切框 cut = false; cut_start = false; }else if(!cut_start){ switch(action){ case MotionEvent.ACTION_DOWN: down_x = (int)event.getX(); down_y = (int)event.getY(); break; case MotionEvent.ACTION_MOVE: move_x = (int)event.getX(); move_y = (int)event.getY(); pictureView.setX(move_x-down_x+up_x); pictureView.setY(move_y-down_y+up_y); break; case MotionEvent.ACTION_UP: up_x = pictureView.getX(); up_y = pictureView.getY(); break; } } }else if(!cut_start){ switch(action){ case MotionEvent.ACTION_DOWN: down_x = (int)event.getX(); down_y = (int)event.getY(); break; case MotionEvent.ACTION_MOVE: move_x = (int)event.getX(); move_y = (int)event.getY(); pictureView.setX(move_x-down_x+up_x); pictureView.setY(move_y-down_y+up_y); break; case MotionEvent.ACTION_UP: up_x = pictureView.getX(); up_y = pictureView.getY(); break; } } return super.onTouchEvent(event); } //弹出对话框函数 public void setDialog(){ final EditText edit = new EditText(this); //弹出输入框,接收输入文件名 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.cuttitle) .setIcon(android.R.drawable.ic_dialog_info).setView( edit); builder.setPositiveButton(R.string.ok, new DialogInterface .OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub cut_name = edit.getText().toString(); dialog.dismiss(); cut = false; cut_start = true; } }).setNegativeButton(R.string.cancel, new DialogInterface .OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub cut = true; cut_start = false; dialog.dismiss(); } }).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, MENU_CUT, 0, R.string.cut); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch(item.getItemId()){ case MENU_CUT: cut = true; cut_start = false; break; } return super.onOptionsItemSelected(item); } public int getScreen_width() { return screen_width; } public void setScreen_width(int screen_width) { PictureDispose.screen_width = screen_width; } public int getScreen_height() { return screen_height; } public void setScreen_height(int screen_height) { PictureDispose.screen_height = screen_height; } public static String getCut_name() { return cut_name; } public static void setCut_name(String cut_name) { PictureDispose.cut_name = cut_name; } }
package demo.utils; import java.io.File; import java.io.IOException; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PaintFlagsDrawFilter; import android.graphics.Rect; import android.view.View; import demo.com.PictureDispose; import demo.com.R; public class PictureView extends View implements Runnable{ //位图 Bitmap bm = null; //Matrix对象 private Matrix matrix = new Matrix(); //图像平移的xy坐标 private int x, y; //图像缩放,旋转 private float scale = 1.0f; private float angle = 0.0f; //图片的宽和高 private int w, h; //设置去除图像锯齿标志 private static PaintFlagsDrawFilter pdf = null; //绘制矩形的paint Paint paint = null; PictureDispose pictureDispose = new PictureDispose(); //矩形的边框大小 private int left = 0; private int right = 0; private int top = 0; private int bottom = 0; //剪切图片保存和放弃按钮 Bitmap abandon = null; Bitmap save = null; //缩放、剪切等临时用到图片 Bitmap bm2 = null; //文件处理类对象 FileUtils fileUtils = new FileUtils(); public PictureView(Context context, String path, int screenwidth, int screenheight) { super(context); // TODO Auto-generated constructor stub paint = new Paint(); //获得位图 bm = BitmapFactory.decodeFile(path); w = bm.getWidth(); h = bm.getHeight(); //如果高太高,按照图片高与屏幕高的比例缩小 float scaleWidth = ((float)screenheight)/h; float scaleHeight = ((float)screenwidth)/w; scale = Math.min(scaleWidth, scaleHeight); //设置矩形的初始化大小 left = screenwidth/3; top = screenheight/3; right = 2*screenwidth/3; bottom = 2*screenheight/3; new Thread(this).start(); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //重置Matrixs matrix.reset(); //旋转图片 matrix.postRotate(angle); //缩放图片 matrix.postScale(scale, scale); //根据原始图片和matrix创建新视图 bm2 = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true); //绘制新视图 PictureView.drawImage(canvas, bm2, x, y); if(PictureDispose.cut){ paint.setAntiAlias(true); //设置颜色 paint.setARGB(255, 255, 0, 0); //设置透明值 paint.setAlpha(200); //设置矩形为空心 paint.setStyle(Paint.Style.STROKE); //设置空心外框大小 paint.setStrokeWidth(3); //矩形 Rect rect = new Rect(left, top, right, bottom); canvas.drawRect(rect, paint); //获取剪切保存,舍弃两个图片按钮 Resources res = getResources(); save = BitmapFactory.decodeResource(res, R.drawable.save); abandon = BitmapFactory.decodeResource(res, R.drawable.abandon); canvas.drawBitmap(save, 0, pictureDispose.getScreen_height()-35, null); canvas.drawBitmap(abandon, pictureDispose.getScreen_width()-88, pictureDispose.getScreen_height()-35, null); } //开始剪切图片 if(PictureDispose.cut_start){ //真正剪切框的大小为 int left_cut = left; int right_cut = right; int top_cut = top; int bottom_cut = bottom; //判断剪切框是否越界 if(left < x){ left_cut = x; } if(right > (x+bm2.getWidth())){ right_cut = x+bm2.getWidth(); } if(top < y){ top_cut = y; } if(bottom > (y+bm2.getHeight())){ bottom_cut = y+bm2.getHeight(); } System.out.println(x); System.out.println(y); System.out.println((bm2.getWidth())); System.out.println((bm2.getHeight())); System.out.println(left); System.out.println(top); System.out.println(right); System.out.println(bottom); System.out.println(left_cut); System.out.println(top_cut); System.out.println(right_cut-left_cut); System.out.println(bottom_cut-top_cut); int cut_width = right_cut-left_cut; int cut_height = bottom_cut-top_cut; Matrix m = new Matrix(); m.postTranslate(0, 0); Bitmap cuted_bitmap = Bitmap.createBitmap(bm2, left_cut-x, top_cut-y, cut_width, cut_height); //选择剪切区域 //Canvas c = new Canvas(); // //c.drawBitmap(bm2, left_cut, top_cut, null); //c.clipRect(left_cut, top_cut, right_cut, bottom_cut); //c.save(); // //System.out.println("1254"); Paint mPaint = new Paint(); mPaint.setColor(Color.BLACK); mPaint.setAntiAlias(true); canvas.drawRect(0, 0, PictureDispose.screen_width, PictureDispose.screen_height, mPaint); //在剪切的地方画图 canvas.drawBitmap(cuted_bitmap, left, top, null); //c.restore(); } } public static void drawImage(Canvas canvas, Bitmap bitmap, int x, int y){ //消除图片锯齿 pdf = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG); //消除锯齿 canvas.setDrawFilter(pdf); //绘制图像 canvas.drawBitmap(bitmap, x, y, null); } @Override public void run() { // TODO Auto-generated method stub while(!Thread.currentThread().isInterrupted()){ try{ Thread.sleep(100); }catch(InterruptedException e){ Thread.currentThread().interrupt(); } //使用postInvaate在线程直接更新界面 postInvalidate(); } } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public float getScale() { return scale; } public void setScale(float scale) { this.scale = scale; } public float getAngle() { return angle; } public void setAngle(float angle) { this.angle = angle; } public void setW(int w) { this.w = w; } public void setH(int h) { this.h = h; } public int getW() { return w; } public int getH() { return h; } public int getleft() { return left; } public void setLeft(int left) { this.left = left; } public int getright() { return right; } public void setRight(int right) { this.right = right; } public int gettop() { return top; } public void setTop(int top) { this.top = top; } public int getbottom() { return bottom; } public void setBottom(int bottom) { this.bottom = bottom; } }
相关推荐
在开发图形用户界面时,我们经常需要对PictureBox中的图片进行缩放和平移操作,以满足不同场景的需求。本篇文章将详细探讨如何利用PictureBox控件实现图片的缩放和平移功能,并通过已封装的类来演示其实现方式。 ...
在图像处理领域,图片平移是一项基础且重要的技术,它涉及到图像的几何变换。这个项目“图片平移技术的开发”显然旨在实现一种能够允许用户选择图像并输入平移偏移量来移动图像的功能。接下来,我们将深入探讨图片平...
总结一下,实现“Android上传头像自定义(剪切、平移,缩放)”功能,主要涉及以下几个步骤: 1. 使用`Intent`实现拍照或从相册选取图片。 2. 加载图片并创建可变`Bitmap`对象。 3. 使用`Matrix`处理图片的裁剪、平...
在Android开发中,实现类似微信上传头像的功能是一项常见的需求,它涉及到图片的选取、预览、裁剪、平移、缩放等多个步骤。以下将详细介绍这个过程中的关键知识点。 首先,用户通常需要从手机相册中选择一张图片。...
在Android开发中,用户经常需要上传头像,而为了提供良好的用户体验,往往需要实现自定义的头像编辑功能,如剪切、平移和缩放。这个任务涉及到多个Android UI组件和技术,包括ImageView、Bitmap、Matrix以及手势检测...
在这个话题中,我们将深入探讨“VC 图像几何变换”涵盖的几个核心概念:图像平移、垂直水平镜像、图像转置、图像缩放和图像旋转。 首先,我们来看**图像平移**。图像平移是将图像在二维空间内沿着x轴和y轴移动的...
`Matrix`对象可以用来对图像进行平移、旋转、缩放等操作。首先创建一个`Matrix`对象,然后调用`postRotate()`方法设定旋转角度。最后,使用`Bitmap.createBitmap()`方法,结合`Matrix`对象来创建一个新的旋转后的`...
5. **图片处理**:在剪切过程中,可能会涉及到图片的旋转、缩放、平移等操作。Android提供了`Bitmap.createBitmap()`和`Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height, Matrix matrix, ...
此外,`Matrix`类用于处理图像的缩放、平移和旋转,以便用户在屏幕上自由调整图片。 2. **手势识别**:Android提供了`ScaleGestureDetector`类来检测用户的缩放手势,这样用户就可以通过双指捏合来放大或缩小图片。...
总结来说,Android图片缩放、旋转和剪切是Android应用开发中的基本技能,它们涉及到Bitmap、Matrix、Canvas等多个核心组件的使用。熟练掌握这些技术,可以帮助我们构建出更加丰富和用户友好的图片处理功能。
1. 像素级:HSV增强、旋转、平移、缩放、剪切、透视、翻转等 2. 图片级:MixUp、Cutout、CutMix、Mosaic、Copy-Paste等 3. 基本图片处理方法:将图像的最长边缩放到640,短边填充到640等方法。 可供使用者完成开发...
核心是使用ImageView显示图片,配合Matrix进行缩放、平移操作。监听手势事件,根据手势调整Matrix,然后使用Bitmap.createBitmap()根据Matrix截取图片。 4. **保存裁剪结果**:使用Bitmap的compress()方法将裁剪后...
通过计算鼠标位置与图像的关系,可以实现图像的平移和局部放大。 4. **剪切功能** 图片剪切涉及图形处理,可以使用`WriteableBitmap`类实现。首先,创建一个可写入的位图,然后根据剪切框的坐标和大小,从原始图像...
2. **Matrix**:Matrix用于对二维图形进行变换,如平移、旋转、缩放等。在图片剪切中,我们可以通过调整Matrix来改变图片的位置和大小,以适应裁剪框的需求。 3. **ImageView**:显示图片的控件,可以配合ScaleType...
仿射变换是一种更复杂的几何变换,包括平移、缩放、旋转以及剪切等操作的组合。它可以通过一个2x3的变换矩阵来表示,可以实现非平行线保持其相对位置不变的变形。在OpenCV中,使用`getAffineTransform`生成变换矩阵...
这通常涉及到对Bitmap的处理,如绘制、缩放和平移。需要实现手势检测来处理用户的拖动和缩放操作,并实时更新裁剪框的位置和大小。 6. **裁剪逻辑**:根据用户在裁剪界面选定的区域,裁剪Bitmap。可以使用Matrix和...
这个类提供了旋转、缩放、平移等多种变换操作。首先,根据接收到的裁剪参数创建一个`AffineTransform`对象,然后使用`AffineTransformOp`类的`createTransformedImage`方法将原始图像转换为裁剪后的图像。最后,使用...
另外,`setTransform()`方法也可以同时设置缩放和平移。 5. **旋转图片**: `rotation`属性用于设置对象的旋转角度,单位为度。例如: ```actionscript imageSprite.rotation = 90; // 逆时针旋转90度 ``` ...
该图片裁剪器基于gl-react-native 安装 npm i -S react-native-image-cropper 要么 yarn add react-native-image-cropper 要求 gl-react您需要安装gl-react。 npm i -S gl-react gl-react-native您需要安装gl-...