Android中图形截取的方式介绍

在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.

这就是设置截取比例的。

时间: 2024-08-17 23:36:52

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

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

我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效率.如果学过JavaWeb的朋友,首先可能想到的是数据库.当然了数据库是一个方案,那么是否还有其他的解决方案呢?今天我们在讲下Android笔记——Android中数据的存储方式(一) 提到的除了SharedPreferences和Files(文本文件)以外的其他几种数据储存方式:xml文件.SQL

android 定位的几种方式介绍

[地理位置] android 定位的几种方式介绍 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面 www.androidkaifa.com 总结了一下网络中现有对于介绍android定位的几种方式,希望对大家有帮助: android 定位一般有四种方法,这四种方式分别是:GPS定位,WIFI定准,基站定位,AGPS定位, (1) Android GPS:需要GPS硬件支持,直接和卫星交互来获取当前经纬度,这种方式需要手机支持GPS模块(现在大部分的智

Android 中LocalBroadcastManager的使用方式

Android中BroadcastReceiver主要用途有 发送通知,更新UI或者数据,应用程序间相互通信,监听系统状态(比如开机,网络等) Android中BroadcasetReceiver的注册方式 manifest清单文件中的全局注册 按照生命周期,在Service或者Activity中使用代码注册 manifest的注册方式  <receiver android:name="com.sample.test.MyBroadcastReciever">       

使用Android中API建议的方式实现SQLite数据库的增、删、改、查的操作

package com.examp.use_SQLite.dao; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.examp

(转)Android 中LocalBroadcastManager的使用方式

发表于2个月前(2014-11-03 22:05)   阅读(37) | 评论(0) 0人收藏此文章, 我要收藏 赞0 1月10日 #长沙# OSC 源创会第32期开始报名 摘要 android中广播的作用非常大,对程序的运行起着非常重要的作用 LocalBroadcastManager Android中BroadcastReceiver主要用途有 发送通知,更新UI或者数据,应用程序间相互通信,监听系统状态(比如开机,网络等) Android中BroadcasetReceiver的注册方式 m

Android中API建议的方式实现SQLite数据库的增、删、改、查的操作

package com.examp.use_SQLite.dao; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.examp

49.Android中线程同步异步方式小结

同步和异步的区别 首先以一个常见的开发场景来区别一下同步和异步的区别,比如我们要获取一张网络图片并完成显示.在这个场景中我们需要开启两个线程,一个是子线程—即下载图片的线程:另外是主 UI 线程—即图片下载完成后进行显示的线程.针对这个场景分别用两幅实现的流程图来区分同步和异步. 从图中可以看到,二者的区别在于:同步时当前主线程会阻塞,直到子线程通知主线程为止(先不考虑ANR):而异步的时候主线程可以继续干其它的事情,当子线程完成任务的时候通知一下主线程就可以了,类似于接口回调或消息队列的思想.

android中处理XML的方式

http://www.cnblogs.com/zhangdongzi/archive/2011/04/14/2016434.html 放在assets目录 http://www.cnblogs.com/luxiaofeng54/archive/2011/03/15/1984617.html assets及res目录 http://blog.csdn.net/hanjingjingpan/article/details/8812770 res下的xml目录 http://www.cnblogs.c

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

1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.lang.Object   ? android.view.View   ? android.widget.TextView 直接子类: Button, CheckedTextView, Chronometer, DigitalClock, EditText 间接子类: AutoCompleteTextV