在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文件

<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
公有方法
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.

这就是设置截取比例的。

Android中图形截取的方式介绍的更多相关文章

  1. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  2. Android视频播放的两种方式介绍

    1.在Android 中播放视频的方式有两种: 第一种方式是使用MediaPlayer 结合SurfaceView 来播放,通过MediaPlayer来控制视频的播放.暂停.进度等: 通过Surfac ...

  3. android 定位的几种方式介绍

    [地理位置] android 定位的几种方式介绍 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面 www.androidkaifa.com 总结了一下网络 ...

  4. Android笔记——Android中数据的存储方式(一)

    Android中数据的存储方式 对于开发平台来讲,如果对数据的存储有良好的支持,那么对应用程序的开发将会有很大的促进作用. 总体的来讲,数据存储方式有三种:一个是文件,一个是数据库,另一个则是网络.其 ...

  5. Android 中LocalBroadcastManager的使用方式

    Android 中LocalBroadcastManager的使用方式 在android-support-v4.jar中引入了LocalBroadcastManager,称为局部通知管理器,这种通知的 ...

  6. Android DevArt6:Android中IPC的六种方式

    Android中IPC的六种方式 1.使用Bundle 最简单的进程间通信方式:Intent + Bundle: 支持三大组件:Activity.Service.BroadcastReceiver : ...

  7. (转)Android 中LocalBroadcastManager的使用方式

    发表于2个月前(2014-11-03 22:05)   阅读(37) | 评论(0) 0人收藏此文章, 我要收藏 赞0 1月10日 #长沙# OSC 源创会第32期开始报名 摘要 android中广播 ...

  8. Android中如何截取字符串中某个字符之前或之后的字符串

    代码改变世界 Android中如何截取字符串中某个字符之前或之后的字符串 //截取#之前的字符串 String str = "sdfs#d"; str.substring(0, s ...

  9. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

随机推荐

  1. sessionStorage的使用方法

    本篇是关于sessionStorage的使用方法的介绍,简单几行代码,实现sessionStorage,请大家查阅 (1)在需要设置sessionStorage的页面写如下代码可以存入sessionS ...

  2. 【Django】Form组件

    目录 Form组件介绍 常用字段与插件 Form组件中所有内置字段 从数据库中获取数据 校验示例 检验手机号是否合法 方式一(基本操作) 方式二(自定义验证规则) 方式三(利用钩子) 验证密码一致性 ...

  3. 【例题 8-11 UVA-10954】Add All

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 就是合并果子.. 每次都合并最小的就可以啦. 别忘了初始化 [代码] /* 1.Shoud it use long long ? 2 ...

  4. cocos2d-x-lua基础系列教程六(lua-table增删改查)

    lua-table库 1.插入 table.insert () --假设没有设定位置.默认last位置 样例: myTable = { 1, 2, 3 } myTable.insert(myTable ...

  5. 微软推送Win10致全球网络负担增大,中国网友表示毫无压力

        7月29日晚间,微软的Windows10操作系统正式上市发行.微软将面向全球190个国家地区进行windows10的免费升级推送. 眼下, Windows 10 正以疯狂的速度登陆全球各类设备 ...

  6. Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

    if (isset($array[2])){ 抛出错误  Cannot use isset() on the result of an expression (you can use "nu ...

  7. 新技能 get —— 如何校验 md5(windows)

    我们在某资源网站上下载完成指定文件后,尤其是一些下载所需较高时长的大型文件,如何检验下载的文件是否完好,也即如何保证和原始网站上的资源一样.此时就要用到检验码的机制,一般文件的下载界面,通常都会给出此 ...

  8. Vue 的 createElement 函数的参数问题的小笔记

    官方文档的说明. 第二个参数的值是要生成的标签的属性数据.点击查看详情. 第三个参数则是组件标签内的数据,数据里面的内容会渲染在第一个参数的标签内.通常会在此指定各插槽 slot 对应的位置,也可以在 ...

  9. Python数据类型中的字符串类型

    1.换行字符:\n print ('I love python.\nAnd you?') 2.转义字符(\):\\ print ('\\\n\\') 3.制表字符(对齐表格的各列):\t print ...

  10. 修改tomcat小猫图标,设置项目的favicon图标

    修改tomcat小猫图标,设置项目的favicon图标,方式有两种:全局方式和局部方式 1.全局方式: 进入tomcat服务器\webapps\ROOT,然后用自己项目的favicon.ico替换to ...