Bitmap setPixels

最近用到Bitmap的各种专场,需要了解Pixels(get set);记录一下相关知识


setPixels:

    /**
     * <p>Replace pixels in the bitmap with the colors in the array. Each element
     * in the array is a packed int prepresenting a non-premultiplied ARGB
     * {@link Color}.</p>
     *
     * @param pixels   The colors to write to the bitmap
     * @param offset   The index of the first color to read from pixels[]
     * @param stride   The number of colors in pixels[] to skip between rows.
     *                 Normally this value will be the same as the width of
     *                 the bitmap, but it can be larger (or negative).
     * @param x        The x coordinate of the first pixel to write to in
     *                 the bitmap.
     * @param y        The y coordinate of the first pixel to write to in
     *                 the bitmap.
     * @param width    The number of colors to copy from pixels[] per row
     * @param height   The number of rows to write to the bitmap
     *
     * @throws IllegalStateException if the bitmap is not mutable
     * @throws IllegalArgumentException if x, y, width, height are outside of
     *         the bitmap's bounds.
     * @throws ArrayIndexOutOfBoundsException if the pixels array is too small
     *         to receive the specified number of pixels.
     */
    public void setPixels(int[] pixels, int offset, int stride,
            int x, int y, int width, int height) {
        checkRecycled("Can't call setPixels() on a recycled bitmap");
        if (!isMutable()) {
            throw new IllegalStateException();
        }
        if (width == 0 || height == 0) {
            return; // nothing to do
        }
        checkPixelsAccess(x, y, width, height, offset, stride, pixels);
        nativeSetPixels(mNativeBitmap, pixels, offset, stride,
                        x, y, width, height, mIsPremultiplied);
    }


			bitmap = BitmapFactory.decodeFile("mnt/sdcard/001.png");
			bw = bitmap.getWidth();
			bh = bitmap.getHeight();
			lineNum = bh / lineH;
			pixels = new int[bw * bh];
			bitmapTem = Bitmap.createBitmap(bw, bh, Config.ARGB_8888);
			bitmap.getPixels(pixels, 0, bw, 0, 0, bw, bh);


 

1、 IllegalStateException if the bitmap is not mutable 这个异常需要在 bitmap数组副本(bitmapTem) 上setPixels

2、x,y 是bitmapTem数组的拷贝下标位置

3、width 、height 是每次拷贝pixels[] 元素个数

4、x+width < bitmapTem.width();y+height <bitmapTem.height();

5、offset 是pixels[]读取的开始位置

下面是个图片专场例子

package com.example.bitmaptest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends Activity {

	Bitmap bitmap, bitmapTem;
	int pixels[];
	int bw, bh;
	int speed = 10;
	int lineNum;
	int lineH = 50;
	PixelsTest pixelsTest;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		pixelsTest = new PixelsTest(this);
		setContentView(pixelsTest);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			speed += 10;
			if (speed < bw) {
				for (int i = 0; i < lineNum; i++) {
					if (i % 2 == 0) {

						bitmapTem.setPixels(pixels, i * lineH * bw, bw, 0, i * lineH,
								speed, lineH);
					}
					pixelsTest.invalidate();
				}

			}

		}
		return super.onTouchEvent(event);
	}

	class PixelsTest extends View {

		public PixelsTest(Context context) {
			super(context);
			bitmap = BitmapFactory.decodeFile("mnt/sdcard/001.png");
			bw = bitmap.getWidth();
			bh = bitmap.getHeight();
			lineNum = bh / lineH;
			pixels = new int[bw * bh];
			bitmapTem = Bitmap.createBitmap(bw, bh, Config.ARGB_8888);
			bitmap.getPixels(pixels, 0, bw, 0, 0, bw, bh);

		}

		@Override
		protected void onDraw(Canvas c) {
			c.drawColor(Color.TRANSPARENT);
			c.drawBitmap(bitmapTem, 0, 0, null);
		}

	}

}

点击屏幕图片专场

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值