2010-10-02 21 views

Respuesta

6

Si desea recortar igualmente el exterior de la imagen, que debe salir el atributo ScaleType para una ImageView: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

En particular, está interesado en la opción "centerCrop". Recorta parte de la imagen que es más grande que el tamaño definido.

He aquí un ejemplo de hacer esto en el diseño XML:

<ImageView android:id="@+id/title_logo" 
      android:src="@drawable/logo" 
      android:scaleType="centerCrop" android:padding="4dip"/> 
+0

es tan tonto que no agregaron más opciones de cultivo que el centro de cultivo ... –

3
int targetWidth = 100; 
int targetHeight = 100; 
RectF rectf = new RectF(0, 0, 100, 100);//was missing before update 
Bitmap targetBitmap = Bitmap.createBitmap(
targetWidth, targetHeight,Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(targetBitmap); 
Path path = new Path(); 
path.addRect(rectf, Path.Direction.CW); 
canvas.clipPath(path); 
canvas.drawBitmap(
sourceBitmap, 
new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()), 
new Rect(0, 0, targetWidth, targetHeight), 
null); 
ImageView imageView = (ImageView)findViewById(R.id.my_image_view); 
imageView.setImageBitmap(targetBitmap); 
+1

Establece el camino al rect "rectf", pero no lo define. ¿Cómo se calculan las dimensiones del recorte para recortar correctamente el centro del mapa de bits? Es decir. no clip de la esquina afuera. – MarkPowell

+0

El enfoque para usar Bitmap.createBitmap que toma una imagen de origen debe ser mucho más eficiente a menos que necesite modificar el mapa de bits aún más. Como crea un mapa de bits inmutables, no tiene que hacer una copia. (a menos que la fuente sea mutable) –

+0

¿Qué es rectf? por favor responda –

13

El androide gestor de Contacto EditContactActivity utiliza Intent("com.android.camera.action.CROP")

Este es un ejemplo de código:

Intent intent = new Intent("com.android.camera.action.CROP"); 
// this will open all images in the Galery 
intent.setDataAndType(photoUri, "image/*"); 
intent.putExtra("crop", "true"); 
// this defines the aspect ration 
intent.putExtra("aspectX", aspectY); 
intent.putExtra("aspectY", aspectX); 
// this defines the output bitmap size 
intent.putExtra("outputX", sizeX); 
intent.putExtra("outputY", xizeY); 
// true to return a Bitmap, false to directly save the cropped iamge 
intent.putExtra("return-data", false); 
//save output image in uri 
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
+1

Si tengo una imagen en la tarjeta SD/myimage.jpg, ¿cómo puedo poner el URI en la intención de CROP? ? es decir. Quiero recortar una región de la imagen de la tarjeta SD/myimage.jpg. –

+0

#Nguyen, tienes lo que comentaste. –

+0

tienes * ... –

7

Try esto:

ImageView ivPeakOver=(ImageView) findViewById(R.id.yourImageViewID); 

Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.yourImageID); 
int width=(int)(bmp.getWidth()*peakPercent/100); 
int height=bmp.getHeight(); 

Bitmap resizedbitmap=Bitmap.createBitmap(bmp,0,0, width, height); 
ivPeakOver.setImageBitmap(resizedbitmap); 

De los Documentos:

static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) 

Devuelve un mapa de bits inmutable desde el subconjunto especificado de la fuente de mapa de bits.

+0

¡Agradable! Todavía es una tontería que no se pueda hacer con un simple atributo en ImageView, pero supongo que esto debería ser lo suficientemente eficiente. –

33

De Bitmap.createBitmap: "Devuelve un mapa de bits inmutable desde el subconjunto especificado del mapa de bits fuente El nuevo mapa de bits puede ser el mismo objeto como fuente, o se haya hecho una copia Se inicializa con la misma densidad que el.. mapa de bits original ".

Pase un mapa de bits y defina el rectángulo desde el que se creará el nuevo mapa de bits.

// Take 10 pixels off the bottom of a Bitmap 
Bitmap croppedBmp = Bitmap.createBitmap(originalBmp, 0, 0, originalBmp.getWidth(), originalBmp.getHeight()-10); 
+1

Para mí solo funcionó si hice 'originalBmp.getWidth() - 1'. Parece que todas las dimensiones de recorte deben caer completamente dentro del original, de lo contrario, se devuelve el original. –

Cuestiones relacionadas