在Android的應用中,有時候我們想只顯示一部分圖像,這時候就要求圖形截圖。
1、任意截取圖像的方法,下面我們詳細介紹一下android中的重要類——Bitmap
public final class
Bitmap
extends Object
implements Parcelable
java.lang.Object
android.graphics.Bitmap
下面是Bitmap的所有應用方法,我們要熟悉記住:
公有方法 |
boolean
compress(Bitmap.CompressFormat format,
int quality, OutputStream stream)
Write a compressed version of the bitmap to the specified outputstream.
Bitmap
copy(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.
void
copyPixelsFromBuffer(Buffer src)
Copy the pixels from the buffer, beginning at the current position, overwriting the bitmap's pixels.
void
copyPixelsToBuffer(Buffer dst)
Copy the bitmap's pixels into the specified buffer (allocated by the caller).
static Bitmap
createBitmap(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 Bitmap
createBitmap(int
width, int height, Bitmap.Config config)
Returns a mutable bitmap with the specified width and height.
static Bitmap
createBitmap(Bitmap source,
int x, int y, int width, int height)
Returns an immutable bitmap from the specified subset of the source bitmap.
static Bitmap
createBitmap(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 Bitmap
createBitmap(Bitmap src)
Returns an immutable bitmap from the source bitmap.
static Bitmap
createBitmap(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 Bitmap
createScaledBitmap(Bitmap src,
int dstWidth, int dstHeight, boolean filter)
Creates a new bitmap, scaled from an existing bitmap.
int
describeContents()
No special parcel contents.
void
eraseColor(int c)
Fills the bitmap's pixels with the specified
Color
.
Bitmap
extractAlpha()
Returns a new bitmap that captures the alpha values of the original.
Bitmap
extractAlpha(Paint paint,
int[] offsetXY)
Returns a new bitmap that captures the alpha values of the original.
final Bitmap.Config
getConfig()
If the bitmap's internal config is in one of the public formats, return that config, otherwise return null.
int
getDensity()
Returns the density for this bitmap.
final int
getHeight()
Returns the bitmap's height
byte[]
getNinePatchChunk()
Returns an optional array of private data, used by the UI system for some bitmaps.
int
getPixel(int x, int y)
Returns the
Color
at
the specified location.
void
getPixels(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 int
getRowBytes()
Return the number of bytes between rows in the bitmap's pixels.
int
getScaledHeight(int targetDensity)
Convenience method that returns the height of this bitmap divided by the density scale factor.
int
getScaledHeight(DisplayMetrics metrics)
Convenience for calling
getScaledHeight(int)
with
the target density of the given
DisplayMetrics
.
int
getScaledHeight(Canvas canvas)
Convenience for calling
getScaledHeight(int)
with
the target density of the given
Canvas
.
int
getScaledWidth(DisplayMetrics metrics)
Convenience for calling
getScaledWidth(int)
with
the target density of the given
DisplayMetrics
.
int
getScaledWidth(int targetDensity)
Convenience method that returns the width of this bitmap divided by the density scale factor.
int
getScaledWidth(Canvas canvas)
Convenience for calling
getScaledWidth(int)
with
the target density of the given
Canvas
.
final int
getWidth()
Returns the bitmap's width
final boolean
hasAlpha()
Returns true if the bitmap's config supports per-pixel alpha, and if the pixels may contain non-opaque alpha values.
final boolean
isMutable()
Returns true if the bitmap is marked as mutable (i.e.
final boolean
isRecycled()
Returns true if this bitmap has been recycled.
void
prepareToDraw()
Rebuilds any caches associated with the bitmap that are used for drawing it.
void
recycle()
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.
void
setDensity(int density)
Specifies the density for this bitmap.
void
setPixel(int x, int y, int color)
Write the specified
Color
into
the bitmap (assuming it is mutable) at the x,y coordinate.
void
setPixels(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.
void
writeToParcel(Parcel p,
int flags)
Write the bitmap and its pixels to the parcel.
這是我們實現截圖圖形的重要方法:
static Bitmap
createBitmap(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文件
然後我們可以通過ClipDrawable獲取要截取的圖像:
public class
ClipDrawable
extends Drawable
implements Drawable.Callback
java.lang.Object
android.graphics.drawable.Drawable
android.graphics.drawable.ClipDrawable
公有方法 |
void
draw(Canvas canvas)
Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).
int
getChangingConfigurations()
Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.
Drawable.ConstantState
getConstantState()
int
getIntrinsicHeight()
Return the intrinsic height of the underlying drawable object.
int
getIntrinsicWidth()
Return the intrinsic width of the underlying drawable object.
int
getOpacity()
Return the opacity/transparency of this Drawable.
boolean
getPadding(Rect padding)
Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.
void
inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
void
invalidateDrawable(Drawable who)
Called when the drawable needs to be redrawn.
boolean
isStateful()
Indicates whether this view will change its appearance based on state.
void
scheduleDrawable(Drawable who, Runnable what,
long when)
A Drawable can call this to schedule the next frame of its animation.
void
setAlpha(int alpha)
Specify an alpha value for the drawable.
void
setColorFilter(ColorFilter cf)
Specify an optional colorFilter for the drawable.
boolean
setVisible(boolean
visible, boolean restart)
Set whether this Drawable is visible.
void
unscheduleDrawable(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.
這就是設置截取比例的。