首页 > 代码库 > Android中图形截取的方式介绍

Android中图形截取的方式介绍

在Android的应用中,有时候我们想只显示一部分图像,这时候就要求图形截图。

1、任意截取图像的方法,下面我们详细介绍一下android中的重要类——Bitmap

public final class

Bitmap

extends Object
implements Parcelable
java.lang.Object
 android.graphics.Bitmap

下面是Bitmap的所有应用方法,我们要熟悉记住:
公有方法
booleancompress(Bitmap.CompressFormat format, int quality, OutputStream stream)
Write a compressed version of the bitmap to the specified outputstream.
Bitmapcopy(Bitmap.Config config, boolean isMutable)
Tries to make a new bitmap based on the dimensions of this bitmap, setting the new bitmap‘s config to the one specified, and then copying this bitmap‘s pixels into the new bitmap.
voidcopyPixelsFromBuffer(Buffer src)
Copy the pixels from the buffer, beginning at the current position, overwriting the bitmap‘s pixels.
voidcopyPixelsToBuffer(Buffer dst)
Copy the bitmap‘s pixels into the specified buffer (allocated by the caller).
static BitmapcreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix.
static BitmapcreateBitmap(int width, int height, Bitmap.Config config)
Returns a mutable bitmap with the specified width and height.
static BitmapcreateBitmap(Bitmap source, int x, int y, int width, int height)
Returns an immutable bitmap from the specified subset of the source bitmap.
static BitmapcreateBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)
Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
static BitmapcreateBitmap(Bitmap src)
Returns an immutable bitmap from the source bitmap.
static BitmapcreateBitmap(int[] colors, int width, int height, Bitmap.Config config)
Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
static BitmapcreateScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
Creates a new bitmap, scaled from an existing bitmap.
intdescribeContents()
No special parcel contents.
voideraseColor(int c)
Fills the bitmap‘s pixels with the specified Color.
BitmapextractAlpha()
Returns a new bitmap that captures the alpha values of the original.
BitmapextractAlpha(Paint paint, int[] offsetXY)
Returns a new bitmap that captures the alpha values of the original.
final Bitmap.ConfiggetConfig()
If the bitmap‘s internal config is in one of the public formats, return that config, otherwise return null.
intgetDensity()

Returns the density for this bitmap.

final intgetHeight()
Returns the bitmap‘s height
byte[]getNinePatchChunk()
Returns an optional array of private data, used by the UI system for some bitmaps.
intgetPixel(int x, int y)
Returns the Color at the specified location.
voidgetPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)
Returns in pixels[] a copy of the data in the bitmap.
final intgetRowBytes()
Return the number of bytes between rows in the bitmap‘s pixels.
intgetScaledHeight(int targetDensity)
Convenience method that returns the height of this bitmap divided by the density scale factor.
intgetScaledHeight(DisplayMetrics metrics)
Convenience for calling getScaledHeight(int) with the target density of the given DisplayMetrics.
intgetScaledHeight(Canvas canvas)
Convenience for calling getScaledHeight(int) with the target density of the given Canvas.
intgetScaledWidth(DisplayMetrics metrics)
Convenience for calling getScaledWidth(int) with the target density of the given DisplayMetrics.
intgetScaledWidth(int targetDensity)
Convenience method that returns the width of this bitmap divided by the density scale factor.
intgetScaledWidth(Canvas canvas)
Convenience for calling getScaledWidth(int) with the target density of the given Canvas.
final intgetWidth()
Returns the bitmap‘s width
final booleanhasAlpha()
Returns true if the bitmap‘s config supports per-pixel alpha, and if the pixels may contain non-opaque alpha values.
final booleanisMutable()
Returns true if the bitmap is marked as mutable (i.e.
final booleanisRecycled()
Returns true if this bitmap has been recycled.
voidprepareToDraw()
Rebuilds any caches associated with the bitmap that are used for drawing it.
voidrecycle()
Free up the memory associated with this bitmap‘s pixels, and mark the bitmap as "dead", meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing.
voidsetDensity(int density)

Specifies the density for this bitmap.

voidsetPixel(int x, int y, int color)
Write the specified Color into the bitmap (assuming it is mutable) at the x,y coordinate.
voidsetPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)
Replace pixels in the bitmap with the colors in the array.
voidwriteToParcel(Parcel p, int flags)
Write the bitmap and its pixels to the parcel.
这是我们实现截图图形的重要方法:
static BitmapcreateBitmap(Bitmap source, int x, int y, int width, int height)
Returns an immutable bitmap from the specified subset of the source bitmap.
第一个参数source就是本来的Bitmap对象,后面的四个参数分别表示截取的范围。

2、还有一种方法,不是任意截取图形,而是从图像的一端(上、下、左、右)截取图像,也可以使用图像截取资源,这种资源需要在res/drawable目录中建立一个xml文件
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="horizontal"
    android:drawable="@drawable/ic_launcher"
    android:gravity="left" >
</clip>

然后我们可以通过ClipDrawable获取要截取的图像:
public class

ClipDrawable

extends Drawable
implements Drawable.Callback
java.lang.Object
   android.graphics.drawable.Drawable
    android.graphics.drawable.ClipDrawable
公有方法
voiddraw(Canvas canvas)
Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).
intgetChangingConfigurations()
Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.
Drawable.ConstantStategetConstantState()
intgetIntrinsicHeight()
Return the intrinsic height of the underlying drawable object.
intgetIntrinsicWidth()
Return the intrinsic width of the underlying drawable object.
intgetOpacity()
Return the opacity/transparency of this Drawable.
booleangetPadding(Rect padding)
Return in padding the insets suggested by this Drawable for placing content inside the drawable‘s bounds.
voidinflate(Resources r, XmlPullParser parser, AttributeSet attrs)
voidinvalidateDrawable(Drawable who)
Called when the drawable needs to be redrawn.
booleanisStateful()
Indicates whether this view will change its appearance based on state.
voidscheduleDrawable(Drawable who, Runnable what, long when)
A Drawable can call this to schedule the next frame of its animation.
voidsetAlpha(int alpha)
Specify an alpha value for the drawable.
voidsetColorFilter(ColorFilter cf)
Specify an optional colorFilter for the drawable.
booleansetVisible(boolean visible, boolean restart)
Set whether this Drawable is visible.
voidunscheduleDrawable(Drawable who, Runnable what)
A Drawable can call this to unschedule an action previously scheduled with scheduleDrawable(Drawable, Runnable, long).
而我们通常所用的Drawable里面有一个方法:
  • The setLevel(int) method allows the client to supply a single continuous controller that can modify the Drawable is displayed, such as a battery level or progress level. Some drawables may modify their imagery based on the current level.
这就是设置截取比例的。



Android中图形截取的方式介绍