How to intercept images in Android

Source: Internet
Author: User

How to intercept images in Android

In Android applications, sometimes we want to display only a part of the image. In this case, the image is required.

1. Any image capturing method. Next we will introduce in detail the important class in android-Bitmap

Public final classBitmapextends Object
Implements Parcelable
Java. lang. Object
Android. graphics. Bitmap

Below are all the application methods of Bitmap. We should be familiar with them:
Public Method
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 specifiedColor.
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) ReturnsColorAt 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 callinggetScaledHeight(int)With the target density of the givenDisplayMetrics.
Int GetScaledHeight (Canvas canvas) Convenience for callinggetScaledHeight(int)With the target density of the givenCanvas.
Int GetScaledWidth (DisplayMetrics metrics) Convenience for callinggetScaledWidth(int)With the target density of the givenDisplayMetrics.
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 callinggetScaledWidth(int)With the target density of the givenCanvas.
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 in 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 specifiedColorInto 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.
This is an important way to implement graphics:
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.
The first parameter source is the original Bitmap object, and the following four parameters represent the truncated range.

2. There is also a way to intercept images instead of any image, but from one end of the image (top, bottom, left, right). You can also use the image to intercept resources, for this type of resource, you need to create an xml file in the res/drawable directory.
     

Then we can use ClipDrawable to obtain the image to be captured:
Public classClipDrawableextends Drawable
Implements Drawable. Callback
Java. lang. Object
Android. graphics. drawable. Drawable
Android. graphics. drawable. ClipDrawable
Public Method
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 previusly scheduledscheduleDrawable(Drawable, Runnable, long).
The Drawable method we usually use is as follows:
  • ThesetLevel(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. this is to set the truncation ratio.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.