如何在自定义视图中查找像素颜色?

时间:2011-12-09 05:16:05

标签: android colors bitmap pixel

我使用onDraw方法

准备了一个自定义视图

我的观点类是,

public class MyAlphabetDraw extends View {

    Paint mPaint = new Paint();
    public static Bitmap myBitmap;

    public MyAlphabetDraw(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        mPaint.setDither(true);
        mPaint.setColor(0xFFFFFFFF);
        mPaint.setTextSize(50);

    }

    public void onDraw(Canvas canvas) {
        // canvas.drawColor(Color.BLUE);
        myBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
                Bitmap.Config.ARGB_8888);
                System.out.println("bitmaps ---"+myBitmap);
                canvas.drawText("Android", 50, 280,mAlphaInner);

        }

}

这里我使用

将视图转换为位图
myBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
                    Bitmap.Config.ARGB_8888);

获取位图后,使用

转换为像素
private int intArray[];

intArray = new int[myBitmap.getWidth() * myBitmap.getHeight()];

        // copy pixel data from the Bitmap into the 'intArray' array
        myBitmap.getPixels(intArray, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),myBitmap.getHeight());

        // replace the red pixels with yellow ones
        for (int i = 0; i < intArray.length; i++) {
                    System.out.println("color is--"+i+" "+intArray[i]);
            if (intArray[i] == 0xFFFFFFFF) {

                intArray[i] = 0xFFFF0000;
            }
        }

如果condition.sop打印的颜色值始终为“0”,则控制不进入。我犯错误的地方......请我

1 个答案:

答案 0 :(得分:1)

你的问题的答案是你只会在你的代码中获得0,你从未使用任何绘图更新你的位图。所有操作都与创建位图有关。

你必须像myBitmap.setPixel(0,0,color)一样调用apis。

现在,如果您想使用canvas来写入Bitmap。您必须创建一个新画布。这是sudo代码:

Canvas bmpCanvas = new Canvas(myBitmap);
bmpCanvas.drawText("Android", 50, 280,mAlphaInner);

// change pixel values in the bitmap like you are doing above 
// or you should use bmpCanvas to change the values
// and after that

canvas.drawBitmap(myBitmap, 0, 0, null);

在这里你有完整的实现,希望代码遍历将帮助你:

package com.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyAlphabetDraw extends View {
    Paint mPaint = new Paint();
    Paint mAlphaInner = new Paint();
    public static Bitmap myBitmap;
    public static Canvas bmpCanvas;
    private int intArray[];

    public MyAlphabetDraw(Context context) {
        super(context);
        init();
    }

    public MyAlphabetDraw(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyAlphabetDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    void init() {
        mPaint.setDither(true);
        mPaint.setColor(0xFFFFFFFF);
        mPaint.setTextSize(100);
        mPaint.setStrokeWidth(10);
        mAlphaInner.setDither(true);
        mAlphaInner.setColor(0xFF0000FF);
        mAlphaInner.setTextSize(98);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (myBitmap == null) {
            myBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
                    Bitmap.Config.ARGB_8888);
            bmpCanvas = new Canvas(myBitmap);
            intArray = new int[myBitmap.getWidth() * myBitmap.getHeight()];
        }

        if (bmpCanvas != null) {
            bmpCanvas.drawCircle(100, 100, 100, mPaint);
            bmpCanvas.drawCircle(100, 100, 90, mAlphaInner);

            // Code to get copy pixels into int array
            myBitmap.getPixels(intArray, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),
                    myBitmap.getHeight());

            // Sample code if you put false the image will have white border
            // if true image will have red color for white pixels
            if (true) {
                for (int i = 0; i < intArray.length; i++) {
                    if (intArray[i] == 0xFFFFFFFF) {
                        intArray[i] = 0xFFFF0000;
                    }
                }
                myBitmap.setPixels(intArray, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),
                        myBitmap.getHeight());
            }
        }

        if (myBitmap != null)
            canvas.drawBitmap(myBitmap, 0, 0, null);
    }
}